2014-04-26 3 views
-2

이 작업을 수행하는 코드가 있지만이를 수행 할 수 없습니다. 나는 비슷한 것을 만들어 문제없이 일했다. 기본적으로 내 textBox에 입력 한 사용자 이름을 otherJFrame의 레이블에 전달하려고합니다. 두 개의 JFrame이있는 것이 좋지 않습니다. JFrame에서 다른 JFrame으로 값 전달

내 데이터를 구문 분석하려고 코드

은 여기에서 일어날 것으로 예상되는 :

if(rs.next()) 
{ 
    JOptionPane.showMessageDialog(null, "Successfully Logged In", "Success", JOptionPane.INFORMATION_MESSAGE); 

this.setVisible(false); 
new FrmMain2(username).setVisible(true); 
} 

이것은 1'st 프레임

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 

package mysqltryouts; 

import java.awt.event.WindowAdapter; 
import java.awt.event.WindowEvent; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.Statement; 
import java.util.Scanner; 
import javax.swing.JFrame; 
import javax.swing.JOptionPane; 

/** 
* 
* @author ExtremeSwat 
*/ 





public class login_panel extends javax.swing.JFrame { 


    private Connection connect = null; 
    private Statement statement = null; 
    private PreparedStatement preparedStatement = null; 
    private ResultSet resultSet = null; 




    public void readDataBase() throws Exception 
    { 

     String username = jTextField1.getText(); 

     String password = jPasswordField1.getText(); 

// System.out.println(username); 
// System.out.println(password); 
// System.out.println("test"); 

     //--------------------- 

//  String databaseUsername = ""; 
//  String databasePassword = ""; 

    try { 
     // this will load the MySQL driver, each DB has its own driver 

     Class.forName("com.mysql.jdbc.Driver"); 

     // setup the connection with the DB. 

     connect = DriverManager 
      .getConnection("jdbc:mysql://localhost/cards?" 
       + "user=root&password=password"); 





    //String sqlQuery = "select count(*) > 0 as match_found FROM username WHERE username = ? and password = MD5(?)"; 
     String sqlQuery = "SELECT * FROM username WHERE username = ? and password = MD5(?)"; 
     // String sqlQuery = "select count(*) > 0 as cnt FROM username WHERE username = ? and password = MD5(?)"; 

     PreparedStatement pst = connect.prepareStatement(sqlQuery); 
pst.setString(1, username); 
pst.setString(2, password); 

ResultSet rs = pst.executeQuery(); 

if(rs.next()) 
{ 

JOptionPane.showMessageDialog(null, "Successfully Logged In", "Success", JOptionPane.INFORMATION_MESSAGE); 

//this.setVisible(false); 
//new FrmMain().setVisible(true); 

//this.setVisible(false); 
//new FrmMain(username).setVisible(true); 

this.setVisible(false); 
new FrmMain2(username).setVisible(true); 



} 
else 
{ 
    JOptionPane.showMessageDialog(null, "Failed to log in", "Failure", JOptionPane.WARNING_MESSAGE); 
} 

    } catch (Exception e) { 
     throw e; 
    } finally { 
     close(); 
    } 

    } 


      private void close() { 
    close(resultSet); 
    close(statement); 
    close(connect); 
    } 

    private void close(AutoCloseable c) throws UnsupportedOperationException { 
    try { 
     if (c != null) { 
     c.close(); 
     } 
    } catch (Exception e) { 
    // don't throw now as it might leave following closables in undefined state 
    } 

} 



    /** 
    * Creates new form login_panel 
    */ 
    public login_panel() { 
     initComponents(); 
    } 



    /** 
    * This method is called from within the constructor to initialize the form. 
    * WARNING: Do NOT modify this code. The content of this method is always 
    * regenerated by the Form Editor. 
    */ 
    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code">       
    private void initComponents() { 

     jTextField1 = new javax.swing.JTextField(); 
     jPasswordField1 = new javax.swing.JPasswordField(); 
     jLabel1 = new javax.swing.JLabel(); 
     jLabel2 = new javax.swing.JLabel(); 
     jButton1 = new javax.swing.JButton(); 
     jButton2 = new javax.swing.JButton(); 
     jLabel3 = new javax.swing.JLabel(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     jTextField1.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       jTextField1ActionPerformed(evt); 
      } 
     }); 

     jPasswordField1.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       jPasswordField1ActionPerformed(evt); 
      } 
     }); 

     jLabel1.setText("Username"); 

     jLabel2.setText("Password"); 

     jButton1.setText("Login"); 
     jButton1.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       jButton1ActionPerformed(evt); 
      } 
     }); 

     jButton2.setText("eXIT"); 
     jButton2.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       jButton2ActionPerformed(evt); 
      } 
     }); 

     jLabel3.setFont(new java.awt.Font("Old English Text MT", 1, 36)); // NOI18N 
     jLabel3.setText("Login B0$$"); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() 
       .addContainerGap(66, Short.MAX_VALUE) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() 
         .addComponent(jButton2) 
         .addContainerGap()) 
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() 
         .addComponent(jLabel3) 
         .addGap(53, 53, 53)))) 
      .addGroup(layout.createSequentialGroup() 
       .addGap(20, 20, 20) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
        .addComponent(jLabel1) 
        .addComponent(jLabel2)) 
       .addGap(18, 18, 18) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
        .addGroup(layout.createSequentialGroup() 
         .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE) 
         .addGap(18, 18, 18) 
         .addComponent(jButton1)) 
        .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)) 
       .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addContainerGap() 
       .addComponent(jLabel3) 
       .addGap(38, 38, 38) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(jLabel1) 
        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addComponent(jButton1)) 
       .addGap(4, 4, 4) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(jLabel2) 
        .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 63, Short.MAX_VALUE) 
       .addComponent(jButton2)) 
     ); 

     pack(); 
    }// </editor-fold>       

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {            
     // TODO add your handling code here: 
    }           

    private void jPasswordField1ActionPerformed(java.awt.event.ActionEvent evt) {             
     // TODO add your handling code here: 
    }            

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {           


    try{ 


// login_panel dao = new login_panel(); 
// 
// 
// dao.readDataBase(); 

     this.readDataBase(); 

    } 
    catch(Exception e) 
    { 
     System.out.println(e.getMessage()); 
    } 


    }           

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {           


    int dialogButton = JOptionPane.YES_NO_OPTION; 

    int dialogResult = JOptionPane.showConfirmDialog(null, "Exit? ", "Information",dialogButton); 

     if(dialogResult == JOptionPane.YES_OPTION) 
     { 
      JOptionPane.showMessageDialog(null, "Thanks for the stay","Confirm",JOptionPane.INFORMATION_MESSAGE); 
      System.exit(0); 

     }else 
     { 
      JOptionPane.showMessageDialog(null, "Remaining...","Remaining....",JOptionPane.INFORMATION_MESSAGE); 

     } 

    }           

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String args[]) { 
     /* Set the Nimbus look and feel */ 
     //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> 
     /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. 
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */ 
     try { 
      for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 
       if ("Nimbus".equals(info.getName())) { 
        javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
        break; 
       } 
      } 
     } catch (ClassNotFoundException ex) { 
      java.util.logging.Logger.getLogger(login_panel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (InstantiationException ex) { 
      java.util.logging.Logger.getLogger(login_panel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (IllegalAccessException ex) { 
      java.util.logging.Logger.getLogger(login_panel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
      java.util.logging.Logger.getLogger(login_panel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } 
     //</editor-fold> 

     /* Create and display the form */ 
     java.awt.EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       //new login_panel().setVisible(true); 

       login_panel frame = new login_panel(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    // Variables declaration - do not modify      
    private javax.swing.JButton jButton1; 
    private javax.swing.JButton jButton2; 
    private javax.swing.JLabel jLabel1; 
    private javax.swing.JLabel jLabel2; 
    private javax.swing.JLabel jLabel3; 
    private javax.swing.JPasswordField jPasswordField1; 
    private javax.swing.JTextField jTextField1; 
    // End of variables declaration     
} 

두 번째 양식에서 코드에서 전체 코드입니다 입니다

package mysqltryouts; 

import javax.swing.*; 
import java.awt.*; 

//am cancer 

public class FrmMain2 { 

    private String userName; 

    public FrmMain2(String username) { 
    this.userName=username; 
    gui(); 
} 


    public void gui() 
    { 


     JFrame f = new JFrame("Ma JFrame"); 

     JPanel p = new JPanel(); 


     p.setBackground(Color.YELLOW); 


     f.setSize(600,400); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 


     JButton b1 = new JButton("Back"); 
     JLabel l1 = new JLabel("Username"); 
     l1.setText(userName); 
     p.add(b1); 
     p.add(l1); 

     f.add(p); 




     f.setVisible(true); 
    } 


    public static void main(String[] args) 
    { 
     //new FrmMain2(username); 
    } 

} 

그래서 내 이름 문자열을 MainFrame2에서 내 레이블로 전송하려고합니다. 내가 여기에 게시 비슷한 문제가 있었다 (그리고 해결)하지만 난 ..... 그것을 할 수 없습니다

답변

1

당신 상태 :

나는 그것이 2 JFrames

이 좋은 방법이 아닙니다 알고

그런 다음하지 마십시오. 로그인 "JFrame"은 모달 JDialog 여야합니다. 기간. 이야기의 끝.

왜?

그런 다음 기본 GUI는 대화 상자가 더 이상 표시되지 않으면 대화 상자가 표시된 곳으로 코드 컨트롤이 바로 돌아올 것임을 알리는 대화 상자를 호출 할 수 있습니다. 그런 다음 메인 GUI가 대화 상자의 사용자 제공 데이터 상태를 쿼리하고 앞으로 이동하는 것은 간단합니다. 예를 들어

:

https://stackoverflow.com/a/21462653/522444 그냥 총알을 물고 그것을 당신은 그것을 후회하지 않을 것입니다.

관련 문제