import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class NULLLayoutDemo extends JFrame implements ActionListener {
private JLabel lbUser = new JLabel();
private JLabel info = new JLabel();
private JLabel lbPassword = new JLabel();
private JButton btnLog = new JButton();/* 该行有错误什么问题呀 */
private Container container = getContentPane();
private JTextField jtextfield_1 = new JTextField(10);
private JPasswordField jtextfield_2 = new JPasswordField(10);
public NULLLayoutDemo() {
super("登陆器");
this.setSize(350, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
NULLLayoutDemo frame = new NULLLayoutDemo();
frame.setLayout();
frame.setVisible(true);
}
public void setLayout() {
container.setLayout(null);
lbUser.setText("用户名");
container.add(lbUser);
container.add(jtextfield_1); // 增加输入用户名的文本框
jtextfield_1.setBounds(80, 26, 120, 26);// 设置用户名的文本框位置
lbUser.setBounds(16, 26, 42, 16);
lbPassword.setText("密码");
container.add(lbPassword);
lbPassword.setBounds(16, 56, 40, 16);
btnLog.setText("登陆");
jtextfield_2.setEchoChar('*');
container.add(jtextfield_2);// 设置密码的文本框位置
jtextfield_2.setBounds(80, 56, 120, 26);// 增加输入密码的文本框
container.add(btnLog);
btnLog.addActionListener(this);
btnLog.setBounds(125, 101, 73, 25);
// info.setBounds(0, 130, 300, 35); // 设置信息的标签
info.setText("用户名是:我爱你 密码是:520");
container.add(info);
/*
* public void ceshi(){ if() }
*/
}
public void actionPerformed(ActionEvent e) {
String name = jtextfield_1.getText(); // 取得用户名文本框的信息
String password = jtextfield_2.getText();// 取得密码文本框的信息
// 判断是否符合条件
if ("我爱你".equals(name) && "520".equals(password)) {
JOptionPane.showMessageDialog(this, "恭喜你!爱情登陆成功", "通过",
JOptionPane.INFORMATION_MESSAGE, null);
} else {
JOptionPane.showMessageDialog(this, "你的输入错误", "错误",
JOptionPane.INFORMATION_MESSAGE, null);
}
}
}