高分悬赏解决一个程序问题

稀里糊涂的码农 2013-09-17 07:03:26
以下是登陆代码

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分求解答。
...全文
310 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 9 楼 healer_kx 的回复:
[quote=引用 7 楼 lhakuma 的回复:] [quote=引用 6 楼 healer_kx 的回复:] 问一下楼主,你这个代码是公司的?还是自己学习的?
自己学习的,不是公司的。。。[/quote] 那就别弄了。。。哪有公司用这玩意啊。有那时间研究JSP也比这个强啊。[/quote] 现在是大学生,在学习当中,遇到的问题都有学习的价值啊
healer_kx 2013-09-19
  • 打赏
  • 举报
回复
引用 10 楼 lhakuma 的回复:
[quote=引用 9 楼 healer_kx 的回复:] [quote=引用 7 楼 lhakuma 的回复:] [quote=引用 6 楼 healer_kx 的回复:] 问一下楼主,你这个代码是公司的?还是自己学习的?
自己学习的,不是公司的。。。[/quote] 那就别弄了。。。哪有公司用这玩意啊。有那时间研究JSP也比这个强啊。[/quote] 现在是大学生,在学习当中,遇到的问题都有学习的价值啊[/quote] 时间是有限的啊,你要是学点有用的,以后或许工作有帮助,并且工资高。 你学这个,我只能说你能积累点调试经验。。。
马达马达达 2013-09-18
  • 打赏
  • 举报
回复
main(null);第一次看到main函数还能这么用呢。。。
鲸鱼先生 2013-09-18
  • 打赏
  • 举报
回复
学习了。看帖也是收获呀。 给楼主和一楼赞一个。
大_爱 2013-09-18
  • 打赏
  • 举报
回复


 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); 这里去掉 就可以了。
    }
 
healer_kx 2013-09-18
  • 打赏
  • 举报
回复
引用 7 楼 lhakuma 的回复:
[quote=引用 6 楼 healer_kx 的回复:] 问一下楼主,你这个代码是公司的?还是自己学习的?
自己学习的,不是公司的。。。[/quote] 那就别弄了。。。哪有公司用这玩意啊。有那时间研究JSP也比这个强啊。
Cute_Tiger 2013-09-18
  • 打赏
  • 举报
回复
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 楼 beefcattlexiaoyang 的回复:


 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); 这里去掉 就可以了。
    }
 
这里去掉还不行,新的对话框弹不出来的。
  • 打赏
  • 举报
回复
引用 6 楼 healer_kx 的回复:
问一下楼主,你这个代码是公司的?还是自己学习的?
自己学习的,不是公司的。。。
healer_kx 2013-09-18
  • 打赏
  • 举报
回复
问一下楼主,你这个代码是公司的?还是自己学习的?
william_yao 2013-09-18
  • 打赏
  • 举报
回复
引用 2 楼 beefcattlexiaoyang 的回复:


 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); 这里去掉 就可以了。
    }
 
一招去除烦恼。哈哈。

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧