2014-10-02 69 views
0

IM做節目,我已經不知道如何做這件事的工作: 我有一臺主機:的Java的JInternalFrame

package itneizapenoitseg; 

public class mainFrame extends JFrame { 

    private dbconnection database = new dbconnection(); // DB connetion data (login , passw ..) 
    private JDesktopPane desktop = new JDesktopPane(); // Need this to make Frame inside Frame 

    public mainFrame() { 
     super(""); 
     setLayout(null); 
     setSize(850, 700); 
     setLocation(500, 280); 
     Login login = new Login(database);// I want to perform a login 
     desktop.add(login); 
     try { 
      login.setSelected(true); 
     } 
     catch (Exception e) { 
      // TODO: handle exception 
     } 
     setContentPane(desktop); 
     ///////////////////////////////////////// 

     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setVisible(true); 

    } 

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

登錄類:

package itneizapenoitseg; 

import... // 

public class Login extends JInternalFrame { 

    // User and password fields 
    JTextField usernameForm = new JTextField(15); 
    JPasswordField passwordForm = new JPasswordField(15); 
    JButton login = new JButton("Login"); 

    public Login(dbconnection database) { 

     super("Login", true, true, true, true); 

     setSize(300, 200); 

     // The login panel 
     JPanel panel = new JPanel(); 
     panel.setLayout(null); 
     JLabel username = new JLabel("username :"); 
     JLabel password = new JLabel("password :"); 

     // Position 
     username.setBounds(70, 10, 80, 11); 
     password.setBounds(70, 55, 80, 17); 
     usernameForm.setBounds(70, 30, 150, 20); 
     passwordForm.setBounds(70, 75, 150, 20); 
     login.setBounds(105, 100, 80, 20); 

     // Addings elements to display panel 
     panel.add(usernameForm); 
     panel.add(passwordForm); 
     panel.add(login); 
     panel.add(password); 
     panel.add(username); 
     getContentPane().add(panel); 

     setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);// Doens't work 
     setVisible(true); 
     actionLogin(database); 
    } 

    // When pressing Login button... 

    private void actionLogin(dbconnection database) { 
     login.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       //TODO Have to change to MySql query 
       String usname = usernameForm.getText(); 
       String passw = new String(passwordForm.getPassword()); 

       // Checking credencial 
       if(usname.equals("test") && passw.equals("1234")){ 
        // Here i want to call a mainFrame funcion (createGUI) 
        dispose();Destroy this panel 
       } else{ 
        JOptionPane.showMessageDialog(null, "Username or password wrong!"); 
        usernameForm.setText(""); 
        passwordForm.setText(""); 
        usernameForm.requestFocus(); 
       } 
      } 
     }); 

    } 
} 

我想,當有人成功登錄,調用mainFrame的函數createGui或通知mainFrame。我的意圖是讓mainFrame爲空,直到sombody成功登錄並且接下來顯示它的內容。

回答

0

this SO question非常相似。

您可以撥打((mainFrame) SwingUtilities.getWindowAncestor((Component)(e.getSource()))).createGUI();

希望有所幫助。

編輯: 我剛剛測試了一個例子:

private void setHandlers() { 
    jButton2.addActionListener(new ActionListener() { 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      ((mainFrame) SwingUtilities.getWindowAncestor((Component)(e.getSource()))).showAlert(); 
     } 
    }); 
} 
public void showAlert() { 
    JOptionPane.showConfirmDialog(this, "Confirming !!"); 
} 

還有另一種選擇(即不常用的 - ATLEAST由我):

((mainFrame) SwingUtilities.getAncestorOfClass(mainFrame.class, ((Component)(e.getSource())))).createGUI(); 

重新編輯: 你也可以嘗試:

((mainFrame) ((JComponent) (e.getSource())).getTopLevelAncestor()).createGUI(); 
+0

它doens't工作導致im內actionListener :( – HaZe 2014-10-02 15:04:44

+0

好的 - 那是因爲你使用了匿名類 - 這很糟糕,但不是世界末日!使用'(Component)e.getSource()'而不是'this'並檢查它是否工作。 – 2014-10-02 15:20:40

+0

編輯答案以及:) – 2014-10-02 15:24:33