简体   繁体   English

如何将扩展的jpanel添加到jframe

[英]how to add an extended jpanel to jframe

I wrote a class that extends JPanel. 我写了一个扩展JPanel的类。 Here is the code: 这是代码:

public class MedicalMonitorPanel extends JPanel{
    public MedicalMonitorPanel() {
        super();
        initComponents();
    }
    public void initComponents(){
        //layout settings
    }
}

Now I want to add my panel to a jframe: 现在,我想将面板添加到jframe中:

public class MedicalMonitorDisplay extends JFrame{
    MedicalMonitorPanel panel;

    public MedicalMonitorDisplay(){
    panel = new MedicalMonitorPanel();
    initComponents();
}
    private void initComponents(){
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    setLayout(new BoxLayout(getContentPane(), BoxLayout.PAGE_AXIS));
    getContentPane().add(panel);
    getContentPane().validate();
}
    public static void main(String[] args){
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            new MedicalMonitorDisplay().setVisible(true);
        }
    });
}
}

But I got this exception: 但是我有一个例外:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at java.awt.Container.addImpl(Unknown Source)
    at java.awt.Container.add(Unknown Source)
    at javax.swing.JFrame.addImpl(Unknown Source)
    at java.awt.Container.add(Unknown Source)
    at MedicalMonitorDisplay.initComponents(MedicalMonitorDisplay.java:53)
    at MedicalMonitorDisplay.<init>(MedicalMonitorDisplay.java:40)
    at MedicalMonitorDisplay$1.run(MedicalMonitorDisplay.java:63)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)

which reports there is something wrong with the add() method. 哪个报告说add()方法有问题。 What's wrong? 怎么了?

This makes no sense whatsoever: 这毫无意义:

add(panel, getContentPane());

Delete this line and start over. 删除此行并重新开始。

Instead you should add the panel to the contentPane via: 相反,您应该通过以下方式将面板添加到contentPane:

getContentPane().add(panel);

If this still doesn't work, then tell the details -- what goes wrong if you try this? 如果仍然无法解决问题,请告诉细节-尝试这样做会出什么问题?

Also, you can't call revalidate on the contentPane unless you cast it first to JPanel. 另外,除非先将其转换为JPanel,否则您不能在contentPane上调用revalidate。 But you shouldn't need to revalidate the contentPane anyway since you'll call pack() on the JFrame after adding all components, and that will suffice. 但是无论如何您都不需要重新验证contentPane,因为在添加所有组件之后您将在JFrame上调用pack() ,这样就足够了。

Try using SwingUtilities instead of EventQueue . 尝试使用SwingUtilities而不是EventQueue

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

I would also follow Hovercraft Full Of Eels 's suggestion of modifying your code to add panel to the JFrame in the way he indicates. 我还将遵循Hovercraft Full Of Eels的建议,即修改您的代码以按照他指示的方式将panel添加到JFrame


EDIT 编辑

The problem was caused because of a missing instantiation of the MedicalMonitorPanel class when using a MedicalMonitorPanel array. 该问题是由于使用MedicalMonitorPanel数组时缺少MedicalMonitorPanel类的实例引起的。

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

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