JPanel 在另一个里面
我的 JPanel 位于另一个 JPanel 中时遇到问题。我不知道为什么,但结果是一个简单的正方形,但尺寸不正确。这是为什么?
import java.awt.Color;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class jj extends JFrame {
private JPanel painel3;
private JPanel painel5;
private Container container;
public jj() {
container = getContentPane();
container.setLayout(null);
painel5 = new JPanel();
painel5.setBackground(Color.red);
painel5.setBounds(120, 110, 100, 120);
painel3 = new JPanel();
painel3.setBackground(Color.white);
painel3.add(painel5);
painel3.setBounds(50, 50, 290, 220);
container.add(painel3);
// frame
setSize(1000, 900);
setLocation(200, 50);
setResizable(false);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new jj();
}
}
I have a problem with a JPanel inside another one. I don't know why, but the result is a simple square, but the dimensions aren't correct. Why is that?
import java.awt.Color;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class jj extends JFrame {
private JPanel painel3;
private JPanel painel5;
private Container container;
public jj() {
container = getContentPane();
container.setLayout(null);
painel5 = new JPanel();
painel5.setBackground(Color.red);
painel5.setBounds(120, 110, 100, 120);
painel3 = new JPanel();
painel3.setBackground(Color.white);
painel3.add(painel5);
painel3.setBounds(50, 50, 290, 220);
container.add(painel3);
// frame
setSize(1000, 900);
setLocation(200, 50);
setResizable(false);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new jj();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您还需要将 panel3 的布局设置为 null,否则将使用默认的 FlowLayout:
panel3.setLayout(null);
You need to set the layout for panel3 also to null otherwise the default
FlowLayout
is used:panel3.setLayout(null);
一些额外的建议。学习使用布局管理器。他们可能有一点学习曲线,但这绝对是值得的。不错的教程: http://download.oracle.com/javase/tutorial/ uiswing/layout/using.html
同样根据 Java 标准,类名应以大写字母开头。这样做将帮助其他人更好地阅读您的代码。
A couple of additional recommendation. Learn to use LayoutManagers. They might have a slight learning curve but it will definitely be worth it. Nice tutorial: http://download.oracle.com/javase/tutorial/uiswing/layout/using.html
Also according to the Java Standards, class names should start with a capital letter. Doing this will help others read your code better.
更好的是避免使用 null 布局和 setBounds/setSize,而是让布局管理器帮助您布局 GUI。您可以在这里阅读它们:在容器中布置组件
Even better though is to avoid use of null layouts and setBounds/setSize but rather let layout managers help you in laying out your GUI. You can read up on them here: Laying out components in a container
在添加 painel5 面板之前将 painel3 的布局设置为 null。
painel3.setLayout(null);
painel3.add(painel5);
我建议使用布局管理器。
Set the layout of painel3 to null before adding the painel5 panel.
painel3.setLayout(null);
painel3.add(painel5);
I recommend to use LayoutManagers.