test042.java

package pack02;

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

public class test042 extends JFrame
{

public static void main(String[] args)
{
new test042();

}

public test042()
{
this.setTitle("习题");
this.setLayout(new GridLayout(3,1,5,5));
Container container1 = this.getContentPane();
JTextField textField1 = new JTextField("用户名");
JTextField textField2 = new JTextField();
container1.add(textField1);
container1.add(textField2);


JButton button1 = new JButton("登录");
JButton button2 = new JButton("重置");
JPanel panel1 = new JPanel(new FlowLayout());
panel1.add(button1);
panel1.add(button2);
container1.add(panel1);

button1.addActionListener(new ActionListener()
{

@Override
public void actionPerformed(ActionEvent e)
{
String text1 = textField1.getText();
String text2= textField2.getText();

if (text1.equals("admin") && text2.equals("xxx"))
{
JOptionPane.showMessageDialog(null, "登录成功");
}
else
{
JOptionPane.showMessageDialog(null, "登录失败");

}

}
});


this.setSize(500,300);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

}
}