1

首先,我知道使用setLayout(null)不是最好的方法,但该程序的目标是尝试编写一个我自己可调整大小的 GUI。

首先是三个类(拆分的原因GroundFrame.javaHomeScreen.java以后会有多个windows):

启动.java

public class Start {

    public static void main(String[] arg){
        new GroundFrame();  
    }
}

GroundFrame.java

public class GroundFrame extends JFrame{

    static final long       serialVersionUID = 0;
    Container               contentPane;
    HomeScreen              homeScreen;

    public GroundFrame(){
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

    private void createAndShowGUI(){
        createBasicFrame();
        createPaneAndAddHome();
        setVisible(true);
    }

    private void createBasicFrame(){
        addComponentListener(new ComponentListener() {
            public void componentShown(ComponentEvent e) {}
            public void componentResized(ComponentEvent e) {
                homeScreen.resizeHome(getContentPane().getSize());
            }
            public void componentMoved(ComponentEvent e) {}
            public void componentHidden(ComponentEvent e) {}
        });

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        contentPane = getContentPane();
        contentPane.setPreferredSize(new Dimension(600, 400));
        contentPane.setLayout(null);
        pack();
    }

    private void createPaneAndAddHome(){
        homeScreen = new HomeScreen(contentPane.getSize());
        contentPane.add(homeScreen);
    }
}

HomeScreen.java

public class HomeScreen extends JPanel{

    static final long       serialVersionUID = 0;
    private JLabel          labelHeader, labelFooter;
    private JTextField      textField;
    private int             x, y;

    public HomeScreen(final Dimension dimension){
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowPanel(dimension);
            }
        });
    }

    private void createAndShowPanel(Dimension dimension){
        labelHeader = new JLabel("HEADER");
        textField = new JTextField();
        labelFooter = new JLabel("FOOTER");
        setupPanel(dimension);
    }

    private void setupPanel(Dimension dimension){
        x = dimension.width;
        y = dimension.height;
        setBounds(0,0,x,y);
        setLayout(null);
        setupTitle(new Dimension(x,y));
    }

    private void setupTitle(Dimension dimension){
        x = dimension.width;
        y = dimension.height;
        labelHeader.setBounds((int)(x*0.1), (int)(y*0.1), (int)(x*0.8), (int)(y*0.3));
        labelHeader.setOpaque(true);
        labelHeader.setBackground(Color.cyan);
        labelHeader.setFont(new Font("Arial", Font.PLAIN, (int)(y*0.25)));

        textField.setBounds((int)(x*0.25), (int)(y*0.45), (int)(x*0.5), (int)(y*0.1));
        textField.setFont(new Font("Arial", Font.PLAIN, (int)(y*0.08)));

        labelFooter.setBounds((int)(x*0.1), (int)(y*0.6), (int)(x*0.8), (int)(y*0.3));
        labelFooter.setOpaque(true);
        labelFooter.setBackground(Color.cyan);
        labelFooter.setFont(new Font("Arial", Font.PLAIN, (int)(y*0.25)));

        add(labelHeader);
        add(labelFooter);
        add(textField);
    }

    public void resizeHome(Dimension dimension){
        setupPanel(dimension);
    }
}

现在一切都按照我的想法进行,但我得到了一个NullpointerException后续行HomeScreen.java

labelHeader.setBounds((int)(x*0.1), (int)(y*0.1), (int)(x*0.8), (int)(y*0.3));

这是错误信息

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at gui.HomeScreen.setupTitle(HomeScreen.java:45)
    at gui.HomeScreen.setupPanel(HomeScreen.java:39)
    at gui.HomeScreen.resizeHome(HomeScreen.java:64)
    at gui.GroundFrame$2.componentResized(GroundFrame.java:43)
    at java.awt.AWTEventMulticaster.componentResized(AWTEventMulticaster.java:159)
    at java.awt.Component.processComponentEvent(Component.java:6331)
    at java.awt.Component.processEvent(Component.java:6285)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Window.processEvent(Window.java:2022)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:703)
    at java.awt.EventQueue.access$000(EventQueue.java:102)
    at java.awt.EventQueue$3.run(EventQueue.java:662)
    at java.awt.EventQueue$3.run(EventQueue.java:660)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:676)
    at java.awt.EventQueue$4.run(EventQueue.java:674)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:673)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)

尽管如此,程序继续运行,什么也没发生。毕竟,错误是在程序启动时出现的,因此在没有执行手动调整大小的地方(错误消息表明错误是从HomeScreens中调用的resizeHome())。为什么会这样?为什么程序继续运行?GUI 本身的构建是否可能是ComponentListeners的第一次调用componentResized()

4

1 回答 1

3

这是因为您SwingUtilities#invokeLater在构造函数中使用

SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        createAndShowGUI();
    }
});

您创建的是 UI 中的竞争条件。

基本上,homeScreen.resizeHome在被调用componentResized之前(从)HomeScreen#createAndShowPanel被调用,因此意味着lablelHeader尚未被初始化......

您当前的事件顺序看起来像...

  • 主要的
  • new GrounFrame()
  • invokeLater GroundFrame#createAndShowGUI
  • ...(返回主目录)
  • GroundFrame#createBasicFrame(加ComponentListener, pack)
  • GroundFrame#createPaneAndAddHome
  • new HomeScreen
  • invokeLater HomeScreen#createAndShowPanel...立即返回...
  • GrounFrame#setVisible(true)
  • GrounFrame#ComponentListener#componentResized
  • HomeScreen#resizeHome
  • HomeScreen#setupPanel
  • Null Pointer Exception
  • HomeScreen#createAndShowPanel

您应该将您的 UI 与您的 EDT 同步main

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            new GroundFrame();
        }
    });
}

并删除其他引用SwingUtilities#invokeLater

于 2012-10-23T22:30:41.643 回答