2013-03-21 148 views
0

尊敬的朋友:我有一個主應用程序,其中包含一個Menu,並且在此Menu的每一箇中都有一個JMenuItem。我希望當我點擊其中一個JMenuItem時,我應該可以打開一個new JFrame,它將執行特定的任務。這JFrame應該在不同的類,實現ActionListener,不在同一類包含主要方法很多例子,我已經看到在互聯網和http://stackoverflow.com不給兩個不同類的解決方案。當我在相同的班級中嘗試相同的方法或者實施ActionListener時,它可以工作,但不像我說的2個不同的班級。我需要這樣的原因是因爲Menu中有很多JMenuItems,並且每個JMenuItem都處理大量的流程。如果我要將所有內容放在一個文件中,那麼它不再是面向對象的編程,而是一個非常長的文件。一個例子如下所示。然而,下面的例子並不適合我。有人能指出我做錯了什麼。預先感謝。如何在單擊JMenuItem時打開新的JFrame窗口

實現主要方法的主要類。

public class SwendaEye{ 

    public static void main(String[]args){ 
    FrameandComp frame = new FrameandComp(); 
    JFrame win; 

    win = frame.mainFrame(); 
    JMenuBar bar; 
    bar = new JMenuBar(); 
    win.setJMenuBar(bar); 

    JMenu swenda = new JMenu("SWENDAEYE");// adding Swenda menu to the bar. 
    bar.add(swenda); 

       JMenuItem open = new JMenuItem("Open"); 
       swenda.add(open); 
       JMenuItem exit = new JMenuItem("Exit"); 
       swenda.add(exit); 

    JMenu tools = new JMenu("Tools");// adding Tools menu to the bar. 
    bar.add(tools); 
       JMenuItem convertIP = new JMenuItem("Convert IP Address"); 
       tools.add(convertIP); 
       JMenuItem convertDomain = new JMenuItem("Convert Domain Name"); 
       tools.add(convertDomain); 
       convertDomain.addActionListener(new Domain()); 

     win.setVisible(true); 
    } 
} 

這是動作監聽類從上面的類

public class Domain implements ActionListener{ 

    public void actionPerformed(ActionEvent e) 
    { 
     if("Convert Domain Name".equals(e.getActionCommand())){ 

      JFrame awindow = new JFrame(); 
      awindow.setSize(200,400); 
      awindow.getContentPane().setBackground(Color.DARK_GRAY); 
      awindow.setTitle("Convert"); 
      awindow.setDefaultCloseOperation(1); 

     } 

    } 

} 

單獨在這個例子中,我只展示了轉換域名JMenuItem。 和你回答之前請不要告訴我JOptionPane,因爲我基本上需要在這個窗口中做很多事情,比如表格,圖像等等。再次感謝。

+0

在這裏,我們再次...請看到這個問題:http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-bad-practice – 2013-03-21 21:07:30

回答

3

您需要設置aWindow可見。

+0

老兄你的男人,我知道這是非常愚蠢的,非常感謝。 – 2013-03-21 20:13:17