2014-03-06 22 views
0

我是完全新的JAVA,我試圖做一個簡單的應用程序,並且我沒有辦法讓我的JOptionPane正確顯示,我想我失蹤了東西:這裏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,因爲它就像一個鼠標點擊後消失歡迎屏幕)

回答

4

JMenu對象不以這種方式操作。要獲得JMenu事件,您需要實現MenuListener而不是ActionListener。 ActionListener對JMenuItem有用。

希望這會有所幫助。