JOptionPane没有显示(null参数,从内部类调用)

时间:2014-03-06 07:42:28

标签: java swing actionlistener joptionpane jmenu

我是JAVA的新手,我正在尝试创建一个简单的应用程序,我无法正确显示JOptionPane,我想我错过了一些东西:

这是代码:

import java.awt.Color;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;


public class Frame  extends JFrame
{       
    private static final long serialVersionUID = 1L;
    final static int WIDTH = 400;
    final static int HEIGHT = 400;

    public Frame()
    {
        super("Convert to Dxf alpha ver. - survey apps 2014");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(WIDTH, HEIGHT);
        setResizable(false);
        setLocation(100, 100);
        setBackground(Color.WHITE);
        setVisible(true);
        WelcomePanel welcomePanel = new WelcomePanel(this);
        add(welcomePanel);          
    }

    public void createMenuPanel()
    {
        MenuPanel menu = new MenuPanel();
        setJMenuBar(menu.createMenu(this));
    }
}

class MenuPanel extends JPanel implements ActionListener
{
    private JMenuItem open,save,close;
    private JMenu file,about;
    private Frame frame;
    private static final long serialVersionUID = 1L;

    public JMenuBar createMenu(Frame frame)
    {
        this.frame = frame;
        JMenuBar menuBar = new JMenuBar();          
        file = new JMenu("File");
        about = new JMenu("About");         
        menuBar.add(file);
        menuBar.add(about);         
        open = new JMenuItem("Open");
        save = new JMenuItem("Save");
        close = new JMenuItem("Close");

        file.add(open);
        file.add(save);
        file.addSeparator();
        file.add(close);

        open.addActionListener(this);
        save.addActionListener(this);
        close.addActionListener(this);
        about.addActionListener(this);

        return menuBar;
    }

    public MenuPanel()
    {
        setVisible(true);
        setOpaque(true);
        setBackground(Color.WHITE);
        setSize(Window.WIDTH,Window.HEIGHT);
    }   

    @Override
    public void actionPerformed(ActionEvent e)
    {
    Object source = e.getSource();

        if(source == open)
        {
            frame.dispose();
        }

        if(source == save)
        {           
        }

        if(source == close)
        {       
        }
        if(source == about)
        {
            JOptionPane.showMessageDialog(frame, "EasyDxfCreator - alpha version");
        }
    }

}

(我跳过了WelcomeFrame,因为它就像一个点击鼠标后消失的欢迎屏幕)

1 个答案:

答案 0 :(得分:4)

JMenu不以这种方式运作。要获取JMenu事件,您需要实现MenuListener而不是ActionListener。 ActionListener适用于JMenuItem。

希望这有帮助。