简体   繁体   English

从外部类创建一个JPanel并将其附加到主Jframe

[英]Create a JPanel from an external class and append it to the primary Jframe

I am working on my first java GUI and already get some good help here, thank you all! 我正在第一个Java GUI上工作,并且已经在这里获得了一些很好的帮助,谢谢大家!

I have a primary class from which i want to control and handle my additional programs like tcp connections etc. So i created a JFrame, appended a JMenu and the main area should change its content depending on what JMenuItem is klicked, thats a simple explain. 我有一个主类,我想从中控制和处理我的其他程序,如tcp连接等。因此,我创建了一个JFrame,附加了JMenu,并且主要区域应根据所敲击的JMenuItem来更改其内容,这就是一个简单的解释。 The content i want to load in this main area are JPanels which are running my additional applications. 我要在此主要区域中加载的内容是正在运行其他应用程序的JPanels。 when i click ex. 当我单击前。 the menuitem "Neu" i want to load a specific JPanel in the main area depending on the clicked JMenuItem. 菜单项“ Neu”,我想根据单击的JMenuItem在主区域中加载特定的JPanel。 how i will bring this to work? 我将如何使其工作?

Here is the error 这是错误

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at MyFrame$1.actionPerformed(MyFrame.java:48)
        at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
        at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
        at javax.swing.AbstractButton.doClick(Unknown Source)
        at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
        at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown
Source)
        at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
        at java.awt.Component.processMouseEvent(Unknown Source)
        at javax.swing.JComponent.processMouseEvent(Unknown Source)
        at java.awt.Component.processEvent(Unknown Source)
        at java.awt.Container.processEvent(Unknown Source)
        at java.awt.Component.dispatchEventImpl(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Window.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
        at java.awt.EventQueue.access$400(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Sour
ce)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Sour
ce)
        at java.awt.EventQueue$4.run(Unknown Source)
        at java.awt.EventQueue$4.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Sour
ce)
        at java.awt.EventQueue.dispatchEvent(Unknown Source)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.run(Unknown Source)

Here is the Code in one file 这是一个文件中的代码

import javax.swing.*;
import java.awt.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

public class MyFrame extends JFrame { 

private static Container contentContainer;

    public static void main(String[] args) {

        new MyFrame();

    }

    public MyFrame() {

        setTitle("MyFrame");
        setSize(300, 200);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setJMenuBar(createMenu());

        // I GUESS HERE ARE MY TROUBLES //

        MyPanel panel = makePanel(new String("Test oO"));        
        contentContainer = this.getContentPane();

        // I GUESS HERE ARE MY TROUBLES //

        setVisible(true);

    }

    public static JMenuBar createMenu() {

        JMenuBar menuBar = new JMenuBar();
        JMenu menu = new JMenu("Menu");
        JMenuItem menuItem = new JMenuItem("Neu");
        menuItem.setMnemonic(KeyEvent.VK_E);
        menuItem.setToolTipText("Neu");
        menuItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                MyPanel dynamicPanel = makePanel(new String("Test haha"));
                contentContainer.add(dynamicPanel);                
            }
        });
        menu.add(menuItem);
        menuBar.add(menu);
        return menuBar;

    }

    public static JDialog makeDialog(String title, String content) {

        JDialog meinJDialog = new JDialog();
        meinJDialog.setVisible(true);
        return meinJDialog;   

    }  

    public static MyPanel makePanel(String config) {

        MyPanel panel = new MyPanel(config);
        return panel;

    }  

}

class MyPanel extends JPanel {

    public MyPanel(String config) {

        setBackground(new Color(77,81,84));
        JButton testButton = new JButton(config);
        add(testButton);

    }

}

Thanks a lot and sorry for my poor language and coding skills :P 非常感谢,对不起,我的语言和编码能力不佳:P

Change: 更改:

Container contentContainer = this.getContentPane(); // Local variable!

To: 至:

contentContainer = this.getContentPane(); // Class attribute!

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

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