JPanel 填充所有 JFrame 空间

发布于 2024-12-27 11:33:50 字数 987 浏览 2 评论 0原文

我编写了这段代码来显示面板中的一组颜色:

import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Palette {

    public static void main(String[] args) {
        JFrame frame = new JFrame("Panel");

        palette.add(new Color(255, 0, 0));
        palette.add(new Color(0, 255, 0));
        palette.add(new Color(0, 0, 255));

        int width = 100;
        int height = 250;
        int x = 0;
        for (Color color : palette) {
            JPanel panel = new JPanel();

            panel.setSize(width, height);
            panel.setLocation(x, 750);
            panel.setBackground(new java.awt.Color(color.getColor()));

            frame.add(panel);

            x+=width;
        }
        frame.pack();

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(1000, 1000);
        frame.setVisible(true);
    }

}

第两个面板位于正确的位置且尺寸正确。但最后一个将所有框架填充为蓝色。怎么了?

I have write this code for displaying some set of colors from panel:

import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Palette {

    public static void main(String[] args) {
        JFrame frame = new JFrame("Panel");

        palette.add(new Color(255, 0, 0));
        palette.add(new Color(0, 255, 0));
        palette.add(new Color(0, 0, 255));

        int width = 100;
        int height = 250;
        int x = 0;
        for (Color color : palette) {
            JPanel panel = new JPanel();

            panel.setSize(width, height);
            panel.setLocation(x, 750);
            panel.setBackground(new java.awt.Color(color.getColor()));

            frame.add(panel);

            x+=width;
        }
        frame.pack();

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(1000, 1000);
        frame.setVisible(true);
    }

}

Two first panels in the right place and with right dimensions. But the last one fills all frame in blue color. What's wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

[浮城] 2025-01-03 11:33:50

您必须使用适当的布局管理器。默认情况下,JFrame 有一个 BorderLayout

查看 LayoutManager 教程:

http://docs.oracle.com/ javase/tutorial/uiswing/layout/visual.html

You have to use the appropriate Layout Manager. A JFrame by default has a BorderLayout.

Check out this tutorial for LayoutManagers:

http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

格子衫的從容 2025-01-03 11:33:50
  1. 您尚未在框架上设置 LayoutManager。我不确定 Swing 中默认的 LayoutManager 是什么,但当您调用 pack() 时,LayoutManager 很可能只是按 Z 顺序堆叠组件。
  2. 您应该使用 setPreferredSize(Dimension d),而不是 setSize(int x, int y) - 我记得 LayoutManager 更喜欢这个。
  1. You haven't set a LayoutManager on the frame. I'm not sure what the default LayoutManager is in Swing, but it's likely the LayoutManager is just stacking the components in Z order when you call pack().
  2. You should use setPreferredSize(Dimension d), not setSize(int x, int y) - from what I recall the LayoutManagers prefer this.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文