首页 > 解决方案 > 如何从框架的左侧制作一条垂直的按钮?

问题描述

包含面板的框架,其中按钮位于框架左侧的列中:

https://i.stack.imgur.com/IUk4C.png

我试图用真正的 Java 代码制作这张图片,但失败了。

我找不到这个问题的正确解决方案......

    BorderLayout borderLayout = new BorderLayout();
    GridBagLayout gridBagLayout = new GridBagLayout();
    GridLayout gridLayout = new GridLayout();
    CardLayout cardLayout = new CardLayout();
    JPanel panelMain = new JPanel();
    JPanel panelLeft = new JPanel();
    JPanel panelInnerLeft = new JPanel();
    JPanel panelRight = new JPanel();

    panelMain.setLayout(borderLayout);
    panelLeft.setLayout(gridBagLayout);
    panelInnerLeft.setLayout(gridLayout);
    panelRight.setLayout(cardLayout);

    button1 = new JButton("Button 1");
    button2 = new JButton("Button 2");
    button3 = new JButton("Button 3");
    button4 = new JButton("Button 4");

    panelInnerLeft.add(button1,gridLayout);
    panelInnerLeft.add(button2,gridLayout);
    panelInnerLeft.add(button3,gridLayout);
    panelInnerLeft.add(button4,gridLayout);

    panelLeft.add(panelInnerLeft);
    panelMain.add(panelLeft);
    panelMain.add(panelRight);
    add(panelMain);

标签: javaswingawtlayout-manager

解决方案


这是用于制作该屏幕截图的确切代码。

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;

public class CompoundLayout01 {

    private JComponent ui = null;

    CompoundLayout01() {
        initUI();
    }

    public void initUI() {
        if (ui!=null) return;

        ui = new JPanel(new BorderLayout(4,4));
        ui.setBorder(new TitledBorder("BorderLayout"));

        CardLayout cardLayout = new CardLayout();
        JPanel cards = new JPanel(cardLayout);
        cards.setBorder(new TitledBorder("CardLayout"));
        ui.add(cards);

        JPanel lineStart = new JPanel(new GridBagLayout());
        lineStart.setBorder(new TitledBorder("GridBagLayout"));
        // will appear on the left, in a LTR text orientation locale
        ui.add(lineStart, BorderLayout.LINE_START);

        JPanel buttonsCentered = new JPanel(new GridLayout(0, 1, 10, 10));
        buttonsCentered.setBorder(new TitledBorder("GridLayout"));
        // as single component added w/no constraint, will be centered
        lineStart.add(buttonsCentered); 
        for (int ii=1; ii<5; ii++) {
            JButton b = new JButton("Button " + ii);
            buttonsCentered.add(b);
        }
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                CompoundLayout01 o = new CompoundLayout01();

                JFrame f = new JFrame(o.getClass().getSimpleName());
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

推荐阅读