BoxLayout使用垂直和水平胶水

时间:2017-10-11 15:43:58

标签: java swing user-interface

以下是我从Creating two buttons at bottom left/right corner获得的代码 我已经包括了我的尝试,(添加了jpanel2)

import java.awt.BorderLayout;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class ButtonsLeftAndRight {
    private JFrame frame;
    private JPanel pane;
    private JButton button1;
    private JButton button2;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new ButtonsLeftAndRight()::createAndShowGui);
    }

    public void createAndShowGui() {
        frame = new JFrame(getClass().getSimpleName());

        pane = new JPanel();
        pane.setLayout(new BoxLayout(pane, BoxLayout.LINE_AXIS));

        button1 = new JButton("Button1");
        button2 = new JButton("Button2");

        pane.add(button1);
        pane.add(Box.createHorizontalGlue());
        pane.add(button2);

        frame.add(pane, BorderLayout.SOUTH);
        JButton b1 = new JButton ("A");
        JButton b2 = new JButton ("B");
        JButton b3 = new JButton ("C");
        JPanel panel2 = new JPanel();
        panel2.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        panel2.add(b1);
        panel2.add(Box.createVerticalGlue());
        panel2.add(b2);
        panel2.add(Box.createVerticalGlue());
        panel2.add(b3);
        frame.add(panel2, BorderLayout.CENTER);
        frame.setSize(500, 500);
        frame.setVisible(true);
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

问题是BoxLayout无法共享,但我需要以某种方式做到这一点,以便我可以获得垂直框

我尝试创建BoxLayout GUI,其中涉及使用按钮和JLabels。在下面的ASCII B代表按钮,J代表JLabel。我也只是在按钮和JLabels

之间留出一点空间
__________________________
|                        | 
|      B       J         |
|      B       J         |
|      B       J         |
|          J             |
|                        |
|Button1         Button2 |
|________________________|

2 个答案:

答案 0 :(得分:1)

你可以有很多JPanel个,每个都有不同的布局。

对于这种情况,我可以设想GridBagLayout面板上有JFrame按钮和标签,这些按钮和标签将以BorderLayout.CENTER BoxLayout对齐为中心,按钮位于底部有import java.awt.BorderLayout; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.SwingUtilities; import javax.swing.border.EmptyBorder; public class ButtonsLeftAndRight { private JFrame frame; private JPanel bottomPane; private JPanel centerPane; private JButton button1; private JButton button2; public static void main(String[] args) { SwingUtilities.invokeLater(new ButtonsLeftAndRight()::createAndShowGui); } public void createAndShowGui() { frame = new JFrame(getClass().getSimpleName()); bottomPane = new JPanel(); bottomPane.setLayout(new BoxLayout(bottomPane, BoxLayout.LINE_AXIS)); button1 = new JButton("Button1"); button2 = new JButton("Button2"); bottomPane.add(button1); bottomPane.add(Box.createHorizontalGlue()); bottomPane.add(button2); frame.add(bottomPane, BorderLayout.SOUTH); JButton b1 = new JButton("A"); JButton b2 = new JButton("B"); JButton b3 = new JButton("C"); centerPane = new JPanel(); centerPane.setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 0; centerPane.add(b1, gbc); gbc.gridx = 2; centerPane.add(new JLabel("Label 1"), gbc); gbc.gridx = 0; gbc.gridy++; centerPane.add(b2, gbc); gbc.gridx = 2; centerPane.add(new JLabel("Label 2"), gbc); gbc.gridx = 0; gbc.gridy++; centerPane.add(b3, gbc); gbc.gridx = 2; centerPane.add(new JLabel("Label 3"), gbc); gbc.gridx = 1; gbc.gridy++; centerPane.add(new JLabel("Centered Label"), gbc); frame.add(centerPane, BorderLayout.CENTER); frame.add(bottomPane, BorderLayout.SOUTH); frame.pack(); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } 方向

gridx = 2

我们在gridx = 1中放置了标签,因此我们可以在JLabel位置设置底部标签,这样可以在JButton和{{1}之间提供额外的空间空格等于底部标签的长度。

这为我们在调整大小之前和之后提供了以下GUI:

enter image description here

enter image description here

当然可能还有其他一些方法,但这是我认为的第一个

注意

  • 您添加了setVisible(true)两次,一次就够了
  • frame.setSize(500, 500); vs frame.pack();使用其中一个,而不是两个。

答案 1 :(得分:0)

尝试在添加jbutton和jlabel之间进行此操作。

container.add(Box.createRigidArea(new Dimension(5,0)));