简体   繁体   English

将JFrame按钮声明为静态变量时,JFrame按钮会很奇怪

[英]JFrame buttons act weird when declaring it to a static variable

So, I am having some really weird stuff going on here. 所以,我在这里发生了一些非常奇怪的事情。

So, my entire class is this: 所以,我的全班都是这样的:

public class Test extends JFrame {

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                new Test();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}


public static void addComponentsToPane(Container pane) {

    JButton button;
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();

button = new JButton("Button 1");
if (shouldWeightX) {
c.weightx = 0.5;
}
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
pane.add(button, c);

button = new JButton("Button 2");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 0;
pane.add(button, c);

button = new JButton("Button 3");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 2;
c.gridy = 0;
pane.add(button, c);

button = new JButton("Long-Named Button 4");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 50;    
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 1;
pane.add(button, c);

button = new JButton("5");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 20; 
c.weighty = 1.0; 
c.anchor = GridBagConstraints.PAGE_END; 
c.insets = new Insets(10,0,0,0); 
c.gridx = 0;
c.gridwidth = 3;
c.gridy = 3;
pane.add(button, c);

}

private JFrame createAndShowGUI(JFrame frame) {
    frame = new JFrame("GridBagLayoutDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    addComponentsToPane(frame.getContentPane());
    frame.pack();
    frame.setVisible(true);
    return frame;
}

JFrame frame = null;
public Test() {

    createAndShowGUI(frame);
    addComponentsToPane(frame.getContentPane());
}

}

So anyway, I am going to focus on this bit: 所以无论如何,我将专注于这一点:

 JFrame frame = null;
public Test() {

    createAndShowGUI(frame);
    addComponentsToPane(frame.getContentPane());
}

Which, produces this result (which works perfectly fine). 哪个,产生这个结果(完全正常)。 在此输入图像描述 However, when I use this code: 但是,当我使用此代码时:

 JFrame frame = null;
public Test() {

    frame = createAndShowGUI(frame);
    addComponentsToPane(frame.getContentPane());
}

Which results in buttons 4 and 5 somehow duplicating themselves with a set size at the top of the screen. 这导致按钮4和5以某种方式在屏幕顶部以设置大小重复自身。 Also, buttons 4 and 5 appear to have different sizes. 此外,按钮4和5似乎具有不同的尺寸。 Like this: 像这样: 在此输入图像描述 (And when I make the window smaller) (当我把窗户做得更小时) 在此输入图像描述

What is causing this? 是什么造成的? All I am doing is setting a variable from null or a JFrame, which should do nothing. 我所做的就是从null或JFrame设置一个变量,它应该什么都不做。

The first method throws a NullPointerException at the addComponentsToPane method because the frame attribute is never initialized and the second method adds the buttons twice to the frame. 第一种方法在addComponentsToPane方法中抛出NullPointerException,因为frame属性从未初始化,第二种方法将按钮两次添加到帧中。 I have rewritten the code below and it works fine. 我已经重写了下面的代码,它工作正常。 You should also have a look at variable shadowing , because your frame attribute gets shadowed by the frame local variable in the addComponentsToPane method whick is rarely a good idea. 你还应该看一下变量阴影 ,因为你的frame属性被addComponentsToPane方法中的frame局部变量所遮蔽,这很少是一个好主意。

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                new Test();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

public static void addComponentsToPane(Container pane) {

    JButton button;
    pane.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();

    button = new JButton("Button 1");
    if (true) {
        c.weightx = 0.5;
    }
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridx = 0;
    c.gridy = 0;
    pane.add(button, c);

    button = new JButton("Button 2");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.5;
    c.gridx = 1;
    c.gridy = 0;
    pane.add(button, c);

    button = new JButton("Button 3");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 0.5;
    c.gridx = 2;
    c.gridy = 0;
    pane.add(button, c);

    button = new JButton("Long-Named Button 4");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipady = 50;
    c.weightx = 0.0;
    c.gridwidth = 3;
    c.gridx = 0;
    c.gridy = 1;
    pane.add(button, c);

    button = new JButton("5");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.ipady = 20;
    c.weighty = 1.0;
    c.anchor = GridBagConstraints.PAGE_END;
    c.insets = new Insets(10, 0, 0, 0);
    c.gridx = 0;
    c.gridwidth = 3;
    c.gridy = 3;
    pane.add(button, c);

}

private void createAndShowGUI() {
    frame = new JFrame("GridBagLayoutDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

JFrame frame = null;

public Test() {

    createAndShowGUI();
    addComponentsToPane(frame.getContentPane());
    frame.pack();
    frame.setVisible(true);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM