以下是我从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 |
|________________________|
答案 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:
当然可能还有其他一些方法,但这是我认为的第一个
setVisible(true)
两次,一次就够了frame.setSize(500, 500);
vs frame.pack();
使用其中一个,而不是两个。答案 1 :(得分:0)
尝试在添加jbutton和jlabel之间进行此操作。
container.add(Box.createRigidArea(new Dimension(5,0)));