如何通过动作监听器将JComboBox添加到JPanel?

时间:2019-05-22 20:04:21

标签: java combobox jpanel panel jcombobox

我试图通过单击按钮将JComboBox添加到另一个类的JPanel中。我已经有动作监听器和组合框设置,但是我需要将其添加到JPanel中。

我尝试通过在动作监听器中执行private MainPanel panel;然后在内部执行panel.add(comboBox);来引用我的JPanel,但这没有用。当我这样做时,它会给出以下信息:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at com.turtywurty.calculator.main.Toolbar$1.actionPerformed(Toolbar.java:79)
    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.plaf.basic.BasicButtonListener.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$500(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$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    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$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    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)

这是我的工具栏,是我想用来添加组合框的按钮。

package com.turtywurty.calculator.main;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JToolBar;

import com.turtywurty.calculator.panels.MainPanel;

public class Toolbar extends JToolBar
{
    private MainPanel panel;
    private JButton btn1;
    private JButton btn2;
    private JButton btn3;
    private JButton btn4;
    private JButton btn5;
    private JButton btn6;
    private JButton btn7;
    private JButton btn8;
    double pi = Math.PI;
    double sixSqrtTwo = (6*Math.sqrt(2));
    double fourDThree = 4/3;
    double oneDThree = 1/3;

    public Toolbar() 
    {
        btn1 = new JButton("2D Shapes");
        btn2 = new JButton("3D Shapes");
        btn3 = new JButton("Equations");
        btn4 = new JButton("Convert Units");
        btn5 = new JButton("Basic Math");
        btn6 = new JButton("Algebra");
        btn7 = new JButton("Settings");
        btn8 = new JButton("Help");

        setLayout(new FlowLayout());

        add(btn1);
        add(btn2);
        add(btn3);
        add(btn4);
        add(btn5);
        add(btn6);
        add(btn7);
        add(btn8);

        btn1.addActionListener(new ActionListener() 
        {       
            @Override
            public void actionPerformed(ActionEvent e) 
            {
                HashMap<String, Integer> hm = new HashMap<String, Integer>();
                hm.put("Square", 0);
                hm.put("Rectangles", 1);
                hm.put("Triangle", 2);
                hm.put("Circle", 3);
                hm.put("Eclipse", 4);
                hm.put("Heptagon", 5);
                hm.put("Hexagon", 6);
                hm.put("Octagon", 7);
                hm.put("Parallelogram", 8);
                hm.put("Pentagon", 9);
                hm.put("Rhombus", 10);
                hm.put("Trapezoid", 11);
                hm.put("Semi Circle", 12);
                hm.put("Nonagon", 13);
                hm.put("Kite", 14);
                hm.put("Trapezium", 15);
                hm.put("Decagon", 16);
                System.out.println(hm.toString());
                JComboBox comboBox = new JComboBox();
                //TODO Add comboBox to MainPanel.java(extends JPanel)
            }
        });
    }
}

这是我要将组合框添加到的类:

package com.turtywurty.calculator.panels;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class MainPanel extends JPanel
{   
    public MainPanel() 
    {
        setLayout(new FlowLayout());
        add(new JScrollPane(), BorderLayout.CENTER);
        setVisible(true);
        Dimension dim = new Dimension();
        dim.width = 1200;
        setPreferredSize(dim);
    }
}

我希望结果是当我按下按钮时,组合框出现在另一个面板上。

0 个答案:

没有答案