JPanel 不会调整大小

发布于 2024-10-05 00:13:25 字数 1322 浏览 2 评论 0原文

我的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

随风而去 2024-10-12 00:13:25

听起来你的设计还可以改进。为什么这些变量需要是超类的属性?为什么不只拥有一个构造所需面板的方法和一个添加它们的构造函数,以便为每个实例获得新的面板?

事实上,为什么不直接创建页眉和页脚类并重用它们,而不是为了获得相同的页眉和页脚而必须对框架进行子类化呢?

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?

云雾 2024-10-12 00:13:25

为什么要覆盖标签来绘制图像?

  1. 这就是 setIcon() 方法的作用,
  2. 您永远不应该在自定义绘画代码中读取文件,因为此方法会被多次调用
  3. ,自定义绘画在必要时是在 PaintComponent() 方法中完成的。

Why are you overriding the label to paint an image?

  1. that is what the setIcon() method is for
  2. you should never read a file in the custom painting code since this method is invoked multiple times
  3. custom painting, when necessary, is done in the paintComponent() method.
长伴 2024-10-12 00:13:25

尝试在将组件添加到容器之前设置首选大小,并“自下而上”添加组件。否则,请尝试调用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文