2015-09-20 3 views
1

я узнавал из этого учебника: «Java Затмение GUI Учебник 8 # Как открыть второй JFrame с помощью First JFrame» и после входа во втором кадре не всплывалоМоя программа не может открыть второй кадр

import java.awt.EventQueue; 
import java.awt.Image; 
import java.sql.*; 
import javax.swing.*; 
import javax.swing.ImageIcon; 

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

public class Login { 

private JFrame frame; 

/** 
* Launch the application. 
*/ 
public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       Login window = new Login(); 
       window.frame.setVisible(true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 


Connection connection=null; 
private JTextField textFieldUN; 
private JPasswordField passwordField; 
private JLabel lblNewLabel_1; 

/** 
* Create the application. 
*/ 
public Login() { 
    initialize(); 
    connection=sqliteConnection.dbConnector(); 
} 

/** 
* Initialize the contents of the frame. 
*/ 
private void initialize() { 
    frame = new JFrame(); 
    frame.setBounds(100, 100, 527, 302); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.getContentPane().setLayout(null); 

    JLabel lblNewLabel = new JLabel("Username:"); 
    lblNewLabel.setBounds(64, 67, 63, 14); 
    frame.getContentPane().add(lblNewLabel); 

    JLabel lblPassword = new JLabel("Password:"); 
    lblPassword.setBounds(64, 114, 82, 20); 
    frame.getContentPane().add(lblPassword); 

    textFieldUN = new JTextField(); 
    textFieldUN.setBounds(157, 64, 86, 20); 
    frame.getContentPane().add(textFieldUN); 
    textFieldUN.setColumns(10); 

    JButton btnLogin = new JButton("Login"); 
    btnLogin.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 
      try{ 
       String query="select* from userInfo where username=? and password=? "; 
       PreparedStatement pst=connection.prepareStatement(query); 
       pst.setString(1,textFieldUN.getText()); 
       pst.setString(2,passwordField.getText()); 

       ResultSet rs=pst.executeQuery(); 
       int count=0; 
       while(rs.next()){ 
        count=count+1; 

       } 
       if(count ==1) 
       { 
        JOptionPane.showMessageDialog(null,"Username and password is correct"); 
        frame.dispose(); 
        UserInfo usInfo= new UserInfo(); 
        usInfo.setVisible(true); 
       } 
       else if(count>1) 
       { 
        JOptionPane.showMessageDialog(null, "Dulicate Username ans password"); 
       } 
       else { 

        JOptionPane.showMessageDialog(null, "Username and password is NOT correct"); 
       } 
      //u can use rs.close 
      // and write pst.close 
      // so u don't have to code what's on the bottom 

      }catch(Exception e1) 
      { 
       JOptionPane.showMessageDialog(null, e1); 

      } 
      finally{ 
       try{ 

       }catch(Exception e1) 
       { 
        JOptionPane.showMessageDialog(null, e1);       
       } 
      } 
     } 
    }); 
    btnLogin.setBounds(157, 166, 89, 23); 
    frame.getContentPane().add(btnLogin); 

    passwordField = new JPasswordField(); 
    passwordField.setBounds(156, 114, 87, 20); 
    frame.getContentPane().add(passwordField); 

    lblNewLabel_1 = new JLabel(""); 
    Image img = new ImageIcon(this.getClass().getResource("/Crimson_Arks_final.jpg")).getImage(); 
    lblNewLabel_1.setIcon(new ImageIcon(img)); 
    lblNewLabel_1.setBounds(317, 25, 184, 184); 
    frame.getContentPane().add(lblNewLabel_1); 
} 
} 

Вот мой второй исходный кадр код:

import java.awt.EventQueue; 
import java.awt.Image; 
import java.sql.*; 
import javax.swing.*; 
import javax.swing.ImageIcon; 
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 
import java.awt.Font; 

public class UserInfo { 


private JFrame frame; 

/** 
* Launch the application. 
*/ 
public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       UserInfo window = new UserInfo(); 
       window.frame.setVisible(true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

/** 
* Create the application. 
*/ 
public UserInfo() { 
    initialize(); 
} 

/** 
* Initialize the contents of the frame. 
*/ 
private void initialize() { 
    frame = new JFrame(); 
    frame.setBounds(100, 100, 450, 300); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.getContentPane().setLayout(null); 

    JLabel lblHelloUser = new JLabel("Hello User"); 
    lblHelloUser.setFont(new Font("Sitka Display", Font.BOLD, 23)); 
    lblHelloUser.setBounds(99, 42, 121, 54); 
    frame.getContentPane().add(lblHelloUser); 
} 

public void setVisible(boolean b) { 
    // TODO Auto-generated method stub 

} 

} 
+0

См. [Использование нескольких JFrames, хорошая/плохая практика?] (Http://stackoverflow.com/q/9554636/418556) –

+0

* Java Eclipse GUI Tutorial 8 # Как открыть второй jframe с использованием первого jframe "* Uggh .. видеоурока. Я должен был догадаться из совета мусора. См. [Создание GUI с JFC/Swing] (http://docs.oracle.com/javase/tutorial/uiswing/) след для учебника, который *** является ** * Стоит делать. –

ответ

1

изменить ваш SetVisible метод в Info пользователя класса ниже:

public void setVisible(boolean b) { 
    frame.setVisible(b); 
} 

или

Метод mainUserInfo не используется. Вы должны переместить блок-код основного метода в свой конструктор UserInfo

Смежные вопросы