使用 BoxLayout 面板构建 Java GUI 时,如何在同一行添加放置两个摆动组件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16013347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I add put two swing components on the same line when constructing a Java GUI with BoxLayout panels?
提问by hasherr
I need to be able to put two components onto one line, repeat this several times with several other labels and text fields, but have everything stacked on top of each other nice and neat. I'll post my code below.
我需要能够将两个组件放在一行上,用其他几个标签和文本字段重复此操作几次,但让所有内容都整齐地堆叠在一起。我将在下面发布我的代码。
package madLibs;
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class MadLibsGUI {
public static void main(String[] args) {
MadLibsGUI main = new MadLibsGUI();
main.start();
}
public void start() {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JButton madLibButton = new JButton("Lib it!");
JLabel title = new JLabel("Welcome to mad libs! \n Put in your words and press the 'Lib It' button to play!");
JLabel nameLabel = new JLabel("Name: ");
JLabel verbLabel1 = new JLabel("Verb: ");
JLabel adjLabel = new JLabel("Adjective: ");
JLabel verbLabel2 = new JLabel("Verb: ");
JLabel nounLabel = new JLabel("Noun: ");
JTextField nameTxt = new JTextField(20);
JTextField verbTxt1 = new JTextField(20);
JTextField adjTxt = new JTextField(20);
JTextField verbTxt2 = new JTextField(20);
JTextField nounTxt = new JTextField(20);
frame.getContentPane().add(BorderLayout.SOUTH, madLibButton);
frame.getContentPane().add(BorderLayout.NORTH, title);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setBackground(Color.green);
frame.getContentPane().add(panel);
panel.add(nameLabel, nameTxt);
panel.add(verbLabel1);
panel.add(verbTxt1);
panel.add(adjLabel);
panel.add(adjTxt);
panel.add(verbLabel2);
panel.add(verbTxt2);
panel.add(nounLabel);
panel.add(nounTxt);
frame.setSize(500, 500);
frame.setVisible(true);
}
}
回答by Guillaume Polet
Here is one way to do it using GridBagLayout
:
这是使用的一种方法GridBagLayout
:
There are plenty of other ways to solve this.
有很多其他方法可以解决这个问题。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class MadLibsGUI {
public void start() {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JButton madLibButton = new JButton("Lib it!");
JLabel title = new JLabel("Welcome to mad libs! \n Put in your words and press the 'Lib It' button to play!");
JLabel nameLabel = new JLabel("Name: ");
JLabel verbLabel1 = new JLabel("Verb: ");
JLabel adjLabel = new JLabel("Adjective: ");
JLabel verbLabel2 = new JLabel("Verb: ");
JLabel nounLabel = new JLabel("Noun: ");
JTextField nameTxt = new JTextField(20);
JTextField verbTxt1 = new JTextField(20);
JTextField adjTxt = new JTextField(20);
JTextField verbTxt2 = new JTextField(20);
JTextField nounTxt = new JTextField(20);
frame.getContentPane().add(BorderLayout.SOUTH, madLibButton);
frame.getContentPane().add(BorderLayout.NORTH, title);
panel.setLayout(new GridBagLayout());
panel.setBackground(Color.green);
frame.getContentPane().add(panel);
GridBagConstraints left = new GridBagConstraints();
left.anchor = GridBagConstraints.EAST;
GridBagConstraints right = new GridBagConstraints();
right.weightx = 2.0;
right.fill = GridBagConstraints.HORIZONTAL;
right.gridwidth = GridBagConstraints.REMAINDER;
panel.add(nameLabel, left);
panel.add(nameTxt, right);
panel.add(verbLabel1, left);
panel.add(verbTxt1, right);
panel.add(adjLabel, left);
panel.add(adjTxt, right);
panel.add(verbLabel2, left);
panel.add(verbTxt2, right);
panel.add(nounLabel, left);
panel.add(nounTxt, right);
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
MadLibsGUI main = new MadLibsGUI();
main.start();
}
});
}
}
And the result:
结果:
回答by rockstar
Another simple example using GridBagLayout .
另一个使用 GridBagLayout 的简单示例。
Requirement is to have two components side by side . Using a BorderLayout each component can have only one area in the component i.e NORTH,SOUTH ,EAST or WEST etc .
要求是有两个并排的组件。使用 BorderLayout 每个组件在组件中只能有一个区域,即 NORTH、SOUTH、EAST 或 WEST 等。
This is fixed by using a GridBagLayout
这是通过使用 GridBagLayout 修复的
JPanel panel1 = new JPanel(new GridBagLayout());
JPanel content1 = new JPanel(new GridLayout(0, 1));
Border border = BorderFactory.createTitledBorder("Workflow Files");
content1.setBorder(border);
ButtonGroup group = new ButtonGroup();
JRadioButton aRadioButton = new JRadioButton("4 slices");
content1.add(aRadioButton);
group.add(aRadioButton);
aRadioButton = new JRadioButton("8 slices", true);
content1.add(aRadioButton);
group.add(aRadioButton);
aRadioButton = new JRadioButton("12 slices");
content1.add(aRadioButton);
group.add(aRadioButton);
aRadioButton = new JRadioButton("16 slices");
content1.add(aRadioButton);
group.add(aRadioButton);
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 0;
c.gridy = 0;
panel1.add(content1, c);
JPanel content2 = new JPanel(new GridLayout(0,1));
border = BorderFactory.createTitledBorder("Topology Files");
content2.setBorder(border);
JCheckBox aCheckbox = new JCheckBox("Achivoes");
content2.add(aCheckbox);
aCheckbox = new JCheckBox("Grass");
content2.add(aCheckbox);
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 0;
panel1.add(content2, c);