1.设计登录页
public class LoginView extends JFrame implements ActionListener {
// 用于短文本字符串或图像的显示区域,或两者。
JLabel usernameJLabel = null;
JLabel passwordJLabel = null;
// JTextField是一个轻量级组件,允许编辑单行文本
JTextField userJTextField = null;
// JPasswordField是一个轻量级组件,允许编辑单行文本,其中视图指示输入的内容,但不显示原始字符。
JPasswordField passwordField = null;
// 实现“推”按钮。
JButton loginButton = null;
JButton escButton = null;
String username;
String password;
{
Properties ps = new Properties();
try {
ps.load(this.getClass().getResourceAsStream("/user.properties"));
} catch (Exception e) {
e.printStackTrace();
}
username = ps.getProperty("username");
password = ps.getProperty("password");
}
public LoginView() {
// 播放背景音乐
try {
new BackGroudMusic(this.getClass().getResource("/sound/main.wav").toURI().toURL());
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (URISyntaxException e1) {
e1.printStackTrace();
}
// 用户名的输入
usernameJLabel = new JLabel();
usernameJLabel.setText("用户名:");// 设置文本
usernameJLabel.setLocation(30, 150);// 设置坐标
usernameJLabel.setSize(50, 20);// 设置大小
usernameJLabel.setForeground(Color.blue);
this.add(usernameJLabel);// 将这个组件加到窗体中
// 密码的输入
passwordJLabel = new JLabel();
passwordJLabel.setText("密 码:");// 设置文本
passwordJLabel.setLocation(30, 200);// 设置坐标
passwordJLabel.setSize(50, 20);// 设置大小
passwordJLabel.setForeground(Color.blue);
this.add(passwordJLabel);// 将这个组件加到窗体中
// 把用户名的输入框添加到窗体中
userJTextField = new JTextField();
userJTextField.setLocation(90, 150);
userJTextField.setSize(100, 20);
userJTextField.setText(username);
this.add(userJTextField);
// 把密码的输入框添加到窗体中
passwordField = new JPasswordField();
passwordField.setLocation(90, 200);// 设置坐标
passwordField.setSize(100, 20);// 设置大小
passwordField.setText(password);
this.add(passwordField);
// 登录按钮
loginButton = new JButton();
try {
loginButton.setIcon(new ImageIcon(this.getClass().getResource("/image/denglu.gif").toURI().toURL()));
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (URISyntaxException e1) {
e1.printStackTrace();
}
//
loginButton.setLocation(30, 250);// 设置坐标
loginButton.setSize(68, 20);// 设置大小
loginButton.addActionListener(this);
this.add(loginButton);
// 退出按钮
escButton = new JButton();
try {
escButton.setIcon(new ImageIcon(this.getClass().getResource("/image/quxiao.gif").toURI().toURL()));
} catch (Exception e) {
e.printStackTrace();
}
escButton.setLocation(116, 250);// 设置坐标
escButton.setSize(68, 20);// 设置大小
escButton.addActionListener(this);
this.add(escButton);
// 背景渲染"image/115.png"
try {
setIconImage(new ImageIcon(this.getClass().getResource("/image/115.png").toURI().toURL()).getImage());
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (URISyntaxException e1) {
e1.printStackTrace();
}
// 将该图像设为窗体的坐标
BackGroudImage backGroudImage;
try {
backGroudImage = new BackGroudImage(this.getClass().getResource("/image/login.jpg").toURI().toURL());
backGroudImage.setSize(599, 330);// 设置大小
this.add(backGroudImage);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}// 背景渲染
// 窗体的界面和将窗体显露出来
this.setSize(599, 370);
this.setLocation(0, 0);
this.setVisible(true);
}
// 表示被触发的组件,信息监控
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(loginButton)) {
if (userJTextField.getText().equals(username)
&& String.valueOf(passwordField.getPassword()).equals(
password)) {
this.dispose();
new ChooseView();
} else {
JOptionPane.showMessageDialog(null, "登录失败");
}
}
if (e.getSource().equals(escButton)) {
System.exit(0);
}
}
}