简体   繁体   中英

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!

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. The content i want to load in this main area are JPanels which are running my additional applications. when i click ex. the menuitem "Neu" i want to load a specific JPanel in the main area depending on the clicked JMenuItem. 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

Change:

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

To:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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