Java JPanel 不适用于 setSize 和 setPrefferedSize

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21649366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:51:49  来源:igfitidea点击:

JPanel not working with setSize & setPrefferedSize

javaswingjpanel

提问by

Please explain why it does not work, also could you post a solution to solve this issue. Thank you very mucho in advance.

请解释为什么它不起作用,你也可以发布一个解决方案来解决这个问题。非常感谢您提前。

public class Run extends JFrame{

    /** Fields **/
    static JPanel jpanel;
    private int x, y;

    /** Constructor **/
    public Run() {
        /** Create & Initialise Things **/
        jpanel = new JPanel();
        x = 400; y = 400;

        /** JPanel Properties **/
        jpanel.setBackground(Color.red);
        jpanel.setPreferredSize(new Dimension(20, 30));


        /** Add things to JFrame and JPanel **/
        add(jpanel);

        /** JFrame Properties **/
        setTitle("Snake Game");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setCursor(null);
        setResizable(false);
        setSize(new Dimension(x,y));
        setLocationRelativeTo(null);
        setVisible(true);
    }

    /** Set the Cursor **/
    public void setCursor() {
        setCursor (Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    }

    /** Main Method **/
    public static void main(String [ ] args) {
        Run run = new Run();
        run.setCursor();
    }
}

采纳答案by MadProgrammer

The problem is, JFrameuses a BorderLayout, which will try and size the content to fit the parent container. While BorderLayoutwill try and use the preferred size as a hint, if the available space is greater or less then, it will automatically adjust to allow the center content (the default position) to fill the entire available space of the parent container.

问题是,JFrame使用 a BorderLayout,它将尝试调整内容的大小以适合父容器。虽然BorderLayout会尝试使用首选大小作为提示,但如果可用空间更大或更小,它将自动调整以允许中心内容(默认位置)填充父容器的整个可用空间。

You could try using a FlowLayoutor GridBagLayoutwhich is more likely to honour the preferred size in more situations

您可以尝试使用FlowLayoutorGridBagLayout在更多情况下更有可能符合首选尺寸

Take a look at How to layout components on containersfor more details

查看如何在容器上布局组件以获取更多详细信息

回答by Christian

You can use pack()method. From Java Docs:

您可以使用pack()方法。来自 Java 文档:

public void pack():Causes this Window to be sized to fit the preferred size and layouts of its subcomponents....

public void pack():使此窗口的大小适合其子组件的首选大小和布局....



You should use this method at the end of the constructor:

您应该在构造函数的末尾使用此方法:

...
setLocationRelativeTo(null);
setVisible(true);
pack();

Edit:

编辑:

If you want the JFrame to keep a size and a JPanel to also keep a size. You can try the following:

如果您希望 JFrame 保持一个大小并且一个 JPanel 也保持一个大小。您可以尝试以下操作:

  • Create a JPanel and add it to the JFrame. Note that this panel will be resized
  • Create a second JPanel and add it to the previous JPanel, so it will keep its size.
  • 创建一个 JPanel 并将其添加到 JFrame。请注意,此面板将调整大小
  • 创建第二个 JPanel 并将其添加到前一个 JPanel,因此它将保持其大小。

Something like this:

像这样的东西:

public Run()
{
    /** Create & Initialise Things **/
    jpanel = new JPanel();
    JPanel jpanel2 = new JPanel();
    x = 400;
    y = 400;

    /** JPanel Properties **/
    jpanel2.setBackground(Color.red);
    jpanel2.setPreferredSize(new Dimension(50, 50));
    jpanel.add(jpanel2);        

    /** Add things to JFrame and JPanel **/
    add(jpanel);

    /** JFrame Properties **/
    ...
}

Edit2:You can also try absolute positioning:

Edit2:您也可以尝试绝对定位

public Run()
{
    /** Create & Initialise Things **/
    jpanel = new JPanel();
    JPanel jpanel2 = new JPanel();
    x = 400;
    y = 400;

    jpanel.setLayout(null);

    Insets insets = jpanel2.getInsets();
    Dimension size = jpanel2.getPreferredSize();
    jpanel2.setBounds(125 + insets.left, 100 + insets.top, size.width, size.height);

    /** JPanel Properties **/
    jpanel2.setBackground(Color.red);
    jpanel.add(jpanel2);

    /** Add things to JFrame and JPanel **/
    add(jpanel);

    /** JFrame Properties **/
    ...
}

回答by nicktalbot

Try this:

尝试这个:

jpanel.setPreferredSize(new Dimension(20, 30));
jpanel.setMinimumSize(new Dimension(20, 30));
jpanel.setMaximumSize(new Dimension(20, 30));

I wouldn't recommend it, but it should fix the size (as long as the frame's layout manager doesn't ignore the values). You need to learn how to use layout managers.

我不推荐它,但它应该修复大小(只要框架的布局管理器不忽略这些值)。您需要学习如何使用布局管理器。