Netbeans 6.7.1 ,这是个简单的登录框小程序,输入用户名和密码,正确就提示正确,错误就提示错误然后重置输入框,代码如下:
package Package1;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class LogIn {
public static void main(String agrs[]){
MainWindow Login=new MainWindow();
Login.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Login.setVisible(true);
Login.setResizable(false);
}
}
class MainWindow extends JFrame {
JPanel panel;
FlowLayout layout;
JLabel label1,label2,label3;
JButton login;
JTextField username;
JPasswordField password;
public MainWindow(){
setTitle("Log in");
setSize(500,500);
layout=new FlowLayout(FlowLayout.CENTER);
panel=new JPanel(layout);
login=new JButton("Log in");
username=new JTextField(10);
password=new JPasswordField(10);
password.setEchoChar('*');
label1=new JLabel("Please input your username and password:");
label2=new JLabel("username:");
label3=new JLabel("password:");
panel.add(label1);
panel.add(label2);
panel.add(username);
panel.add(label3);
panel.add(password);
panel.add(login);
this.setContentPane(panel);
login.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
String str1;
char[] PInput;
str1=username.getText();
PInput=password.getPassword();
if((str1.equals("javaname"))&&(PInput.equals("javapassword")))
label1.setText("Correct!");
else{
username.setText("");
password.setText("");
label1.setText("Input again!");
}
}
});
}
}
我运行始终显示有错误,看不到任何运行结果,不知道哪里有错
另外,怎么控制提示标签和输入框的位置关系?比如我想像QQ那样,变量的位置关系是:
L1:label1
L2:label2,username
L3:label3,password
L4:login
------解决方案--------------------------------------------------------
package jframe.zidong;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyJFrame extends JFrame
{
private JPanel panel1,panel2,panel3;
final private JLabel label1=new JLabel("Please input your username and password:");;
final private JLabel label2=new JLabel("username:");;
final private JLabel label3=new JLabel("password:");
final private JButton login=new JButton("Log in");
final private JTextField username=new JTextField(10);
final private JPasswordField password=new JPasswordField(10);
public MyJFrame(String title)
{
super(title);
setSize(300,200);
setLocation(400, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setJPanel();
setLayout(new GridLayout(3,1));
getContentPane().add(panel1);
getContentPane().add(panel2);
getContentPane().add(panel3);
}
public void setJPanel()
{
panel1=new JPanel();