以下是登陆代码import java.awt.BorderLayout;
public class Login extends JFrame { private JPanel contentPane;
private JTextField textField;
private TextField passwordField;
Connection con;
java.sql.Statement stmt;
ResultSet rs; /**
 * Launch the application.
 */
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Login frame = new Login();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
} /**
 * Create the frame.
 */
public Login() {
setTitle("\u8D44\u4EA7\u6E05\u67E5\u7CFB\u7EDF");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 455, 274);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);

JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.NORTH);

JLabel label = new JLabel("\u6B22\u8FCE\u4F7F\u7528\u5357\u4EAC\u6653\u5E84\u5B66\u9662\u8D44\u4EA7\u6E05\u67E5\u7CFB\u7EDF");
label.setFont(new Font("微软雅黑", Font.BOLD, 13));
panel.add(label);

JPanel panel_1 = new JPanel();
contentPane.add(panel_1, BorderLayout.CENTER);

JLabel label_1 = new JLabel("\u7528\u6237\u540D \uFF1A");
label_1.setFont(new Font("宋体", Font.BOLD, 12));

JLabel label_2 = new JLabel("\u5BC6  \u7801 \uFF1A");
label_2.setFont(new Font("宋体", Font.BOLD, 12));

textField = new JTextField();
textField.setColumns(10);

passwordField = new TextField();
passwordField.setEchoChar('*');
GroupLayout gl_panel_1 = new GroupLayout(panel_1);
gl_panel_1.setHorizontalGroup(
gl_panel_1.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel_1.createSequentialGroup()
.addGap(94)
.addGroup(gl_panel_1.createParallelGroup(Alignment.TRAILING)
.addGroup(gl_panel_1.createSequentialGroup()
.addComponent(label_2)
.addGap(18)
.addComponent(passwordField, 118, 118, 118))
.addGroup(gl_panel_1.createSequentialGroup()
.addComponent(label_1)
.addGap(18)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, 118, GroupLayout.PREFERRED_SIZE)))
.addContainerGap(145, Short.MAX_VALUE))
);
gl_panel_1.setVerticalGroup(
gl_panel_1.createParallelGroup(Alignment.TRAILING)
.addGroup(Alignment.LEADING, gl_panel_1.createSequentialGroup()
.addGap(41)
.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(label_1))
.addGap(38)
.addGroup(gl_panel_1.createParallelGroup(Alignment.TRAILING)
.addComponent(label_2)
.addComponent(passwordField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addContainerGap(44, Short.MAX_VALUE))
);
panel_1.setLayout(gl_panel_1);

JPanel panel_2 = new JPanel();
contentPane.add(panel_2, BorderLayout.SOUTH);

JButton button = new JButton("\u767B  \u9646");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if(textField.getText().equals("")){
new JOptionPane().showMessageDialog(null,"用户名不能为空!");
}
else if(passwordField.getText().equals("")){
new JOptionPane().showMessageDialog(null,"密码不能为空!");
}
else{
try{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("数据库驱动加载成功");
}catch(Exception e){
System.out.println("数据库驱动加载失败");
e.printStackTrace();
}
    
try{
con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/AssetsManageSystem","root","lhakuma");
System.out.println("数据库服务器连接成功");
stmt = con.createStatement(); 
    rs = stmt.executeQuery("select name from user"); 
    //&&passwordField.getText().equals(rs.getString("pass"))
    while(rs.next()){
if(textField.getText().equals(rs.getString("name"))){
new MainFrame();
break;
}

else{
new JOptionPane().showMessageDialog(null,"用户名或者密码错误!请重新输入");
}
}
}catch(Exception e2){
System.out.println("获取数据失败");
e2.printStackTrace();
}
finally{
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

});
JButton button_1 = new JButton("\u6E05  \u7A7A");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText("");
passwordField.setText("");
}
});

JButton button_2 = new JButton("\u9000  \u51FA");
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
panel_2.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
panel_2.add(button);
panel_2.add(button_1);
panel_2.add(button_2); 
}
}以下是登陆成功后显示的界面代码import java.awt.BorderLayout;
public class MainFrame extends JFrame { private JPanel contentPane; /**
 * Launch the application.
 */
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainFrame frame = new MainFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
} /**
 * Create the frame.
 */
public MainFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
main(null);
}}
我的问题是:登陆成功后无限出现新的对话框,就是无限执行第二个代码段.求大神解救,这个问题困扰我很久了。100分求解答。数据库数据javaclass对话框

解决方案 »

  1.   

    public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        MainFrame frame = new MainFrame();
                        frame.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }估计是这段代码影响的,无限new,表示对线程神马的不了解啊
    不用这段,你贴的第一段代码改成以下这样应该就不无限弹了:
    while(rs.next()){
                                if(textField.getText().equals(rs.getString("name"))){
                                    new MainFrame().setVisible(true);
                                    break;
                                }
                                 
                                else{
                                    new JOptionPane().showMessageDialog(null,"用户名或者密码错误!请重新输入");
                                }
                            }
      

  2.   

     public MainFrame() {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBounds(100, 100, 450, 300);
            contentPane = new JPanel();
            contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
            contentPane.setLayout(new BorderLayout(0, 0));
            setContentPane(contentPane);
           // main(null); 这里去掉 就可以了。
        }
     
      

  3.   

    main(null);第一次看到main函数还能这么用呢
      

  4.   

    自己学习的,不是公司的那就别弄了哪有公司用这玩意啊。有那时间研究JSP也比这个强啊。
      

  5.   

    自己学习的,不是公司的那就别弄了哪有公司用这玩意啊。有那时间研究JSP也比这个强啊。
    现在是大学生,在学习当中,遇到的问题都有学习的价值啊
      

  6.   

    自己学习的,不是公司的那就别弄了哪有公司用这玩意啊。有那时间研究JSP也比这个强啊。
    现在是大学生,在学习当中,遇到的问题都有学习的价值啊时间是有限的啊,你要是学点有用的,以后或许工作有帮助,并且工资高。
    你学这个,我只能说你能积累点调试经验