标签:java line-breaks swing
我正在为我的迷你游戏添加一个按钮,但我不知道如何换行.我想在按钮和文本之间有一个空格,这里是代码:
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.
有关详细信息,请查看Laying Out Components Within a Container和How to Use 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);
举个例子
标签:java,line-breaks,swing 来源: https://codeday.me/bug/20190608/1201519.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。