2013年9月20日星期五

A program to solve the problem reward score

The following is the landing code

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

The following is displayed after successful login interface code

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

}




My question is : After the successful landing unlimited new dialog box appears , is the infinite execution second snippet . ask god to rescue , this problem has troubled me for a long time . 100 points of answers .
------ Solution ---------------------------------------- ----
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 ();
}
}
});
}

estimated impact of this code , unlimited new, expressed ignorance of God Ma ah thread
do this , the first piece of code you posted so that should not be changed to the following infinite bomb :
while (rs.next ()) {
if (textField.getText (). equals (rs.getString ("name"))) {
new MainFrame (). setVisible (true);
break;
}

else {
new JOptionPane (). showMessageDialog (null, " user name or password is incorrect ! Please re-enter " ) ;
}
}

------ Solution ------------------------------------ --------


 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); 这里去掉 就可以了。
    }
 

------ For reference only ------------------------ ---------------
studied . See also post harvest it .
to the landlord and the first floor like this one.
------ For reference only -------------------------------------- -
main (null); first saw the main function can so use it . . .
------ For reference only -------------------------------------- -

a move from worry . Haha.
------ For reference only -------------------------------------- -
ask the landlord, you this code is the company ? Or their own learning ?

------ For reference only ---------------------------------- -----

their own learning , not the company's . . .
------ For reference only -------------------------------------- -

here to remove is not enough, a new dialog box does not come out of the bomb .
------ For reference only -------------------------------------- -

their own learning , not the company's . . .  

then do not get it. . . How can companies use this thing ah. Have that time to study the JSP also stronger than this ah.
------ For reference only -------------------------------------- -

their own learning , not the company's . . .          
  
then do not get it. . . How can companies use this thing ah. Have that time to study the JSP also stronger than this ah.  
now a college student in the learning, the problems encountered are learning value ah
------ For reference only ---------------- -----------------------

their own learning , not the company's . . .                
    
then do not get it. . . How can companies use this thing ah. Have that time to study the JSP also stronger than this ah.          
now a college student in the learning, the problems encountered are learning value ah  

time is limited , ah , if you learn something useful , perhaps after working there to help , and high wages .
you learn this , I can only say that you can accumulate points debugging experience . . .

没有评论:

发表评论