MCG*_*Guy 4 java swing line-breaks
我正在为我的迷你游戏添加一个按钮,但我不知道如何换行.我想在按钮和文本之间有一个空格,这里是代码:
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
Run Code Online (Sandbox Code Playgroud)
它在欢迎消息中以单独的行显示的内容,但由于某种原因,按钮位于旁边label2是否有人知道如何?
顺便说一下,import javax.swing.*;如果你还不知道的话,你需要一开始.感谢任何知道的人.
JPanelFlowLayout默认使用,显然不符合您的需求.你可以GridBagLayout改用.
查看在容器中布置组件以及如何使用GridBagLayout获取更多详细信息
就像是...
JPanel panel1 = new JPanel();
JLabel label1 = new JLabel("Welcome to the Wall Game!");
JLabel label2 = new JLabel("Click the button to read the instructions!");
JButton button1 = new JButton("Start");
button1.setText("Start!");
Font font1 = label1.getFont().deriveFont(Font.BOLD, 24f);
label1.setFont(font1);
panel1.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
panel1.add(label1, gbc); //adds in all the labels to panels
panel1.add(label2, gbc);
gbc.insets = new Insets(30, 0, 0, 0);
panel1.add(button1, gbc);
Run Code Online (Sandbox Code Playgroud)
举个例子
| 归档时间: |
|
| 查看次数: |
830 次 |
| 最近记录: |