2014-10-02 5 views
0

메신저 프로그램을 만들고 난이 일을 작동하게하는 방법을 모르고도 없어 :자바의 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에 알립니다. 내 의도는 sombody 성공적으로 로그인하고 다음 내용을 표시 할 때까지 mainFrame을 비우는 것입니다.

답변

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 !!"); 
} 

다른 옵션 (즉, 자주 사용되는 -이어야 내게로)있다 :

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

RE-EDIT : 가 다음과 같이 시도해 볼 수도 있습니다 :

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

작업 원인이 actionListener 내부에 없습니다. – HaZe

+0

Ok - 익명의 클래스를 사용했기 때문에 그렇습니다. 그건 나쁘지 만 세상 끝은 아닙니다! 'this' 대신'(Component) e.getSource()'를 사용하고 그것이 작동하는지 확인하십시오. –

+0

답변도 편집 됨 :) –