我需要能够将两个组件放在一行上,用其他几个标签和文本字段重复几次,但要让所有东西都整齐地堆叠在一起。我将在下面发布我的代码。
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);
}
}