2014-02-16 78 views
0

我有這個程序在Java中,當你點擊查看錶並查看所有 一個窗口出現時,我需要隱藏第一個和關閉第二上,事件上設置窗口

第一個變得可見

我不知道如何從這些類中引用一個對象,因爲我已經創建了它們,如 new Main();和新的View(); :)

這是一個類:

package CarManager; 

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

import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 

public class Main extends JFrame { 

    private static final long serialVersionUID = 1L; 
    static int width = 400; 
    static int height = width/16 * 9; 
    static String title = "Car Manager"; 
    JButton viewTables = new JButton("View tables"); 
    JButton clients = new JButton("Clients"); 
    JButton search = new JButton("Search"); 
    JButton viewCars = new JButton("View all"); 
    JButton viewRent = new JButton("Rent a car"); 
    JButton viewBuy = new JButton("Buy a car"); 
    JButton viewAccessory = new JButton("Accessory"); 

    public Main() { 

     setLayout(null); 
     setLocationRelativeTo(null); 
     setTitle(title); 
     setSize(width, height); 
     setResizable(false); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     setVisible(true); 

     JLabel background = new JLabel(new ImageIcon("res\\background2.jpg")); 
     add(background); 
     background.setSize(width, height); 
     add(viewTables); 
     add(clients); 
     add(search); 
     viewTables.setBounds(20, 20, 110, 30); 
     clients.setBounds(20, 70, 110, 30); 
     search.setBounds(20, 120, 110, 30); 

     viewTables.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       add(viewCars); 
       viewCars.setBounds(260, 20, 110, 20); 

       add(viewRent); 
       viewRent.setBounds(260, 50, 110, 20); 

       add(viewBuy); 
       viewBuy.setBounds(260, 80, 110, 20); 

       add(viewAccessory); 
       viewAccessory.setBounds(260, 110, 110, 20); 
      } 
     }); 

     viewCars.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       new View(); 
       setVisible(false); 
      } 
     }); 

    } 

    public static void main(String args[]) { 
     new Main(); 
    } 
} 

,這是出現的第二個窗口:

package CarManager; 

import javax.swing.ImageIcon; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 

public class View extends JFrame { 

    private static final long serialVersionUID = 1L; 
    int width = 400; 
    int height = width/16 * 9; 
    String title = "View all Cars"; 

    public View() { 
     setLayout(null); 
     setLocationRelativeTo(null); 
     setTitle(title); 
     setSize(width, height); 
     setResizable(false); 
     setDefaultCloseOperation(DISPOSE_ON_CLOSE); 
     setVisible(true); 
     JLabel background = new JLabel(new ImageIcon("res\\background2.jpg")); 
     add(background); 
     background.setSize(width, height); 
    } 
} 

所有我需要的是當我關閉第二窗口的第一個成爲可見再

回答

1
import javax.swing.ImageIcon; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import java.awt.event.*; 


class View extends JFrame { 

private static final long serialVersionUID = 1L; 
int width = 400; 
int height = width/16 * 9; 
String title = "View all Cars"; 

    public View() { 
    setLayout(null); 
    setLocationRelativeTo(null); 
    setTitle(title); 
    setSize(width, height); 
    setResizable(false); 
    setDefaultCloseOperation(DISPOSE_ON_CLOSE); 
    setVisible(true); 
    JLabel background = new JLabel(new ImageIcon("res\\background2.jpg")); 
    add(background); 
    background.setSize(width, height); 
    WindowListener listener = new WindowAdapter() { 


    public void windowClosing(WindowEvent w) { 

      new Main(); 

     } 

     }; 

    addWindowListener(listener); 
     } 
    } 

請運行程序view.java我添加Windows適配器以獲得Windows事件wh EN窗口關閉然後main()的窗口顯示

+0

沒有解釋的代碼並沒有告訴OP什麼。 –

+0

只是要小心,只要先前的實例仍然存在(但只是不可見),您不會創建Main()的新實例。 – GameDroids

0

你可以創建自己的WindowAdapter的:

public class MyWindowAdapter extends WindowAdapter { 

    private Main mainFrame;   

    public MyWindowAdapter(Main mainFrame) { // when creating an instance of this WindowAdapter, tell it with which Main Window you are working with 
     this.mainFrame = mainFrame; 
    } 

    public void windowClosing(WindowEvent e) { 
     mainFrame.setVisible(true);     // when this WindowAdapter registers a closing operation it will set the Main Window visible again 
    } 
} 

那麼當你箱子你在actionPerformed()註冊您自己WindowAdapter的一個新的實例,告訴窗口新View適配器什麼你的Main實例看起來像。

viewCars.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      View view = new View();       // your new View 
      view.addWindowListener(new MyWindowAdapter(Main.this)); // register the WindowAdapter to your new View 
      setVisible(false); 
     } 
    }); 
+0

確定它的工作原理,但在關閉第二個窗口後,第一個窗口應該是可見的,但按鈕不可見,我必須將鼠標懸停在它們上方以顯示它們,爲什麼是這樣以及如何修復它? – Shago

+0

嗯,那在我的測試中沒有發生。無論如何,你可以嘗試在主窗口強制重繪:在'MyWindowAdapter' - 'windowClosing()'add(mainFrame.setVisible(true);')下面的'mainFrame.repaint();'行。這會通知「Main」窗口儘快重新繪製其組件。雖然這不應該是必要的...告訴我,如果它的工作 – GameDroids

+0

沒有結果,我不知道爲什麼 – Shago