如何从框架的左侧制作垂直的按钮线?

时间:2018-10-18 17:34:59

标签: java swing awt layout-manager

一个框架,其中包括面板,其中按钮位于该框架左侧的一列中:

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);

3 个答案:

答案 0 :(得分:1)

这是用于制作that screenshot的确切代码。

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);
    }
}

答案 1 :(得分:0)

panelMain.setLayout(borderLayout);
...
panelMain.add(panelLeft);
panelMain.add(panelRight);

阅读Layout Managers上的Swing教程。

它将向您展示如何使用BorderLayout

添加每个组件时,需要指定“约束”。像BorderLayout.LINE_STARTBorderLayout.CENTER

以下内容也是错误的:

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

阅读GridLayout上的部分。 GridLayout不需要约束。

答案 2 :(得分:-2)

我不知道您是否需要一些特定的布局,但是如果您想要更多控制,可以看一下我的示例。请注意,我必须自己处理一些尺寸。另外,如果您需要更多控制权,可以随时使用null作为布局。

enter image description here

import java.awt.FlowLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;

import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;
import javax.swing.SwingUtilities;

public class NewJFrame extends javax.swing.JFrame {
    private JPanel jPanel1;
    private JButton jButton1;
    private JButton jButton2;
    private JButton jButton3;
    private JPanel jPanel2;
    private JButton jButton4;
    private JPanel ButtonsPanel;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                NewJFrame inst = new NewJFrame();
                inst.setLocationRelativeTo(null);
                inst.setVisible(true);
            }
        });
    }

    public NewJFrame() {
        super();
        initGUI();
    }

    private void initGUI() {
        try {
            setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

            jPanel1 = new JPanel();
            getContentPane().add(jPanel1, BorderLayout.CENTER);
            jPanel1.setLayout(null);

            ButtonsPanel = new JPanel();
            FlowLayout ButtonsPanelLayout = new FlowLayout();
            jPanel1.add(ButtonsPanel);
            ButtonsPanel.setLayout(ButtonsPanelLayout);
            ButtonsPanel.setBounds(12, 23, 104, 215);
            ButtonsPanel.setBorder(
            BorderFactory.createEtchedBorder(BevelBorder.LOWERED));

            jButton1 = new JButton();
            ButtonsPanel.add(jButton1);
            jButton1.setText("jButton1");
            jButton1.setPreferredSize(new java.awt.Dimension(85, 23));

            jButton2 = new JButton();
            ButtonsPanel.add(jButton2);
            jButton2.setText("jButton2");
            jButton2.setPreferredSize(new java.awt.Dimension(85, 23));

            jButton3 = new JButton();
            ButtonsPanel.add(jButton3);
            jButton3.setText("jButton3");
            jButton3.setPreferredSize(new java.awt.Dimension(85, 23));

            jButton4 = new JButton();
            ButtonsPanel.add(jButton4);
            jButton4.setText("jButton4");
            jButton4.setPreferredSize(new java.awt.Dimension(85, 23));

            jPanel2 = new JPanel();
            jPanel1.add(jPanel2);
            jPanel2.setLayout(null);
            jPanel2.setBounds(122, 23, 250, 215);
            jPanel2.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED));

            pack();
            setSize(400, 300);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}