我正在为我的迷你游戏添加一个按钮,但我不知道如何换行.我想在按钮和文本之间有一个空格,这里是代码:
JPanel panel1 = new JPanel();
JLabel label1 = new JLabel("Welcome to the Wall Game!");
JLabel label2 = new JLabel("Click the button to read the instructions!");
JLabel space = new JLabel("");
JButton button1 = new JButton("Start");
button1.setText("Start!");
label1.setFont(font1);
panel1.add(label1); //adds in all the labels to panels
panel1.add(label2);
panel1.add(space);
panel1.add(button1);
this.add(panel1); //adds the panel
它在欢迎消息中以单独的行显示的内容,但由于某种原因,按钮位于label2旁边有人知道怎么做?
顺便说一下,你需要导入javax.swing.*;在一开始,如果你还不知道.
感谢任何知道的人.
解决方法:
JPanel默认使用FlowLayout,显然不能满足您的需求.您可以使用GridBagLayout.
就像是…