java – 为什么我的Jpanel borderlayout不按预期工作?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 为什么我的Jpanel borderlayout不按预期工作?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2833字,纯文字阅读大概需要5分钟。
内容图文
![java – 为什么我的Jpanel borderlayout不按预期工作?](/upload/InfoBanner/zyjiaocheng/805/911e1a51500b4b1986766581748c50e8.jpg)
我在JPanel,JLabel和JButton中添加了2个元素.我希望它们彼此叠加,所以我使用BorderLayout.NORTH和SOUTH添加它们.
我遇到的问题是JLabel JButton并排坐在彼此旁边,而不是像预期的那样彼此叠加.这个问题可能与我使用borderlayout将其他面板添加到框架的事实有关,如下所示.
这是一个SSCE来演示我的问题.您会注意到,如果展开窗口,您将看到JLabel左侧的JButton.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Question {
/**
* @param args
*/
public static void main(String[] args) {
gui();
}
public static void gui() {
final JFrame theFrame = new JFrame();
theFrame.setSize(550, 290);
theFrame.setLocationRelativeTo(null);
// options for the JComboBox
String[] options = { ("1"), ("2"), ("3") };
final JPanel thePanel = new JPanel();
JLabel aMessage = new JLabel(
"<html>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus tempus<br> lacus eu ante vestibulum tincidunt. Donec venenatis rhoncus justo sit amet <br>gravida. Pellentesque habitant morbi tristique senectus et netus et malesuada<br>est ac mattis. Donec lobortis rhoncus quam. In at vulputate ipsum<br>to fix. Phasellus tempus lacus eu ante vestibulum tincidunt. Donec venenatis rhoncus<br>turpis quam sagittis arcu, non pulvinar purus leo ege.<br><br><br></html>");
aMessage.setVisible(true);
JButton theButton = new JButton();
theButton
.setText("<HTML><FONT color=\"#000099\"><U>Click here</U></FONT>"
+ " if you would like to view some file.</HTML>");
theButton.setBorderPainted(false);
theButton.setOpaque(false);
theButton.setBackground(Color.WHITE);
theButton.setVisible(true);
thePanel.add(aMessage, BorderLayout.NORTH);
thePanel.add(theButton, BorderLayout.SOUTH);
// create second panel
final JPanel secondPanel = new JPanel();
JLabel bMessage = new JLabel("Here's another Jlabel");
final JComboBox cBox = new JComboBox(options);
// would be nice to get bmessage + cBox left aligned
secondPanel.add(bMessage);
secondPanel.add(cBox);
// create third panel
final JPanel thirdPanel = new JPanel();
JLabel cMessage = new JLabel("Here's a third message");
String newString = ("Hello");
JTextField stringInput = new JTextField(newString);
stringInput.setPreferredSize(new Dimension(250, 20));
thirdPanel.add(cMessage);
thirdPanel.add(stringInput);
JButton lastButton = new JButton("A Button");
thirdPanel.add(cMessage);
thirdPanel.add(stringInput);
thirdPanel.add(lastButton);
theFrame.add(thePanel, BorderLayout.NORTH);
theFrame.add(thirdPanel, BorderLayout.SOUTH);
theFrame.add(secondPanel, BorderLayout.CENTER);
theFrame.setVisible(true);
}
}
解决方法:
你在哪里设置thePanel JPanel的布局?答:你没有,所以它默认使用FlowLayout.解决方案:通过setLayout(…)将布局设置为您想要的布局
例如.,
thePanel.setLayout(new BorderLayout());
内容总结
以上是互联网集市为您收集整理的java – 为什么我的Jpanel borderlayout不按预期工作?全部内容,希望文章能够帮你解决java – 为什么我的Jpanel borderlayout不按预期工作?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。