JPanel 不会调整大小
我的代码:
public MyConstructor() {
view = new JPanel(new GridLayout(3, 1));
header = new JPanel(new GridLayout(2, 1));//2 ROWS 1 COLUMN
foot = new JLabel("Copyright...");
content = new JPanel();
info = new JLabel("");
logo = new JLabel() {
BufferedImage img;
@Override
public void paint(Graphics g) {
try {
img = ImageIO.read(new File("logo.jpg"));
} catch (IOException e) {
}
g.drawImage(img, 0, 0, null);
}
};
window.add(view);
header.add(logo);
header.add(info);
view.add(header);
view.add(content);
view.add(foot);
window.setLocation(width / 2, 100);
window.setSize(width, height);
window.setPreferredSize(new Dimension(width, height));
content.setSize(window.getWidth(), height-70);
content.setPreferredSize(new Dimension(window.getWidth(), height-70));
}
“窗口”是框架...该类没有扩展 JFrame 我的类将成为其他类的超类,子类继承公共内容 JPanel。在我的超级类中,我尝试设置 GridLayout 的 3 个部分的宽度和高度,徽标和信息组件的高度加起来为 70...我已经设置了其他组件(视图、标题、信息) ,logo) 私有,以便子类无法访问它们...
当应用程序运行时,会显示一个用于登录的窗口,该窗口会正确显示并调整大小。一旦登录创建了子类之一的实例,登录窗口就会隐藏站点 setVisible(false)
当显示新窗口时,JFrame 的大小是正确的,但页眉、内容和页脚不是正确的大小。 我尝试设置每个组件的大小和首选大小,但仍然不起作用...我也尝试调用重绘和验证/重新验证
有什么想法吗?
My Code:
public MyConstructor() {
view = new JPanel(new GridLayout(3, 1));
header = new JPanel(new GridLayout(2, 1));//2 ROWS 1 COLUMN
foot = new JLabel("Copyright...");
content = new JPanel();
info = new JLabel("");
logo = new JLabel() {
BufferedImage img;
@Override
public void paint(Graphics g) {
try {
img = ImageIO.read(new File("logo.jpg"));
} catch (IOException e) {
}
g.drawImage(img, 0, 0, null);
}
};
window.add(view);
header.add(logo);
header.add(info);
view.add(header);
view.add(content);
view.add(foot);
window.setLocation(width / 2, 100);
window.setSize(width, height);
window.setPreferredSize(new Dimension(width, height));
content.setSize(window.getWidth(), height-70);
content.setPreferredSize(new Dimension(window.getWidth(), height-70));
}
"window" is the frame...the class is not extending JFrame
My class is going to be a super class to others, the subclasses inherit the public content JPanel. In my super class i'm trying to set the width and height of the 3 sections of my GridLayout, the logo and info components add up to 70 for their height...I've set the other components (view,header,info,logo) private so that subclasses can't access them...
When the application runs a window is shown for the login, this displays and resizes properly. once logged in an instance of one of the subclasses is created, the login window is then hidden site setVisible(false)
when the new window is shown however, the JFrame is the correct size but the header,content and footer are not the currect sizes.
I've tried setting the size and preferred size each of the components but still not working...I've also tried calling repaint and validate/revalidate
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
听起来你的设计还可以改进。为什么这些变量需要是超类的属性?为什么不只拥有一个构造所需面板的方法和一个添加它们的构造函数,以便为每个实例获得新的面板?
事实上,为什么不直接创建页眉和页脚类并重用它们,而不是为了获得相同的页眉和页脚而必须对框架进行子类化呢?
It sounds like your design could be improved. Why do these variables need to be attributes of a superclass? Why not just have a method that constructs the panels that you need and a constructor that adds them so that you get fresh panels for each instance?
In fact, why not just create the header and footer classes and reuse them instead of having to subclass a frame just to get the same header and footer?
为什么要覆盖标签来绘制图像?
Why are you overriding the label to paint an image?
尝试在将组件添加到容器之前设置首选大小,并“自下而上”添加组件。否则,请尝试调用
pack()
、revalidate()
、repaint()
等进行调整。还要阅读布局管理器,您没有正确使用它们。
另外,Swing 也很糟糕。尝试一下 Netbeans,它会让事情变得更容易忍受。当您需要手动放置东西并调整其大小时,它也很有帮助。
Try setting preferred sizes before adding components to containers, and adding components "bottom-up". Otherwise, try calling
pack()
,revalidate()
,repaint()
etc.to adjust things.Read up on Layout Managers, too, you're not using them correctly.
Also, Swing sucks. Try Netbeans, it makes it a little bit more bearable. It helps a lot when you need to manually place and resize things, too.