eclipse软件编写
package swing;
import javax.swing.;
import java.awt.;
import java.awt.event.*;
public class Login {
JFrame f=new JFrame(“login”);
JTextField t1=new JTextField(15);
JPasswordField p=new JPasswordField(15);
JButton b1=new JButton(“submit”);
JButton b2=new JButton(“cancel”);
public static void main(String[] args) {
new Login().launch();
}
public void launch() {
f.setSize(350,200);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationRelativeTo(null);
f.setLayout(new GridLayout(0,2));
JPanel p1=new JPanel();
p1.add(new JLabel("username"));
JPanel p2=new JPanel();
p2.add(t1);
JPanel p3=new JPanel();
p3.add(new JLabel("password"));
JPanel p4=new JPanel();
p4.add(p);
JPanel p5=new JPanel();
p5.add(b1);
JPanel p6=new JPanel();
p6.add(b2);
b1.addActionListener(submit);
b2.addActionListener(cancel);
f.add(p1);
f.add(p2);
f.add(p3);
f.add(p4);
f.add(p5);
f.add(p6);
f.setVisible(true);;
}
ActionListener submit=new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String sname=t1.getText();
String pword=new String(p.getPassword());
sname=sname.trim();
pword=pword.trim();
if(sname.equals("")||pword.equals("")) {
JOptionPane.showMessageDialog(f,"please enter your message");
}else if(sname.equals("liuna")&&pword.equals("928")) {
f.dispose();
}else {
JOptionPane.showMessageDialog(f,"your message is wrong");
}
}
};
ActionListener cancel=new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
t1.setText("");
p.setText("");
}
};
}