源代码
import javax.swing.*;
import java.awt.event.*;
public class Login {
public static void main(String[] args) {
JFrame frame = new JFrame("登录页面");
frame.setSize(350, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.add(panel);
placeComponents(panel);
frame.setVisible(true);
}
private static void placeComponents(JPanel panel) {
panel.setLayout(null);
JLabel userLabel = new JLabel("用户名:");
userLabel.setBounds(10,20,80,25);
panel.add(userLabel);
JTextField userText = new JTextField(20);
userText.setBounds(100,20,165,25);
panel.add(userText);
JLabel passwordLabel = new JLabel("密码:");
passwordLabel.setBounds(10,50,80,25);
panel.add(passwordLabel);
JPasswordField passwordText = new JPasswordField(20);
passwordText.setBounds(100,50,165,25);
panel.add(passwordText);
JButton loginButton = new JButton("登录");
loginButton.setBounds(10, 80, 80, 25);
panel.add(loginButton);
loginButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
if(userText.getText().equals("admin")&&new String(passwordText.getPassword()).equals("123456")){
new Index();
}
}
});
JButton cancelButton = new JButton("取消");
cancelButton.setBounds(100, 80, 80, 25);
panel.add(cancelButton);
cancelButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
}
}
import javax.swing.*;
import java.awt.event.*;
public class Index {
private static final int WIDTH = 300;
private static final int HEIGHT = 200;
public Index() {
final JFrame jf = new JFrame("首页");
jf.setSize(WIDTH, HEIGHT);
jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
jf.setVisible(true);
JPanel contentPane = new JPanel();
jf.setContentPane(contentPane);
JButton close = new JButton("取消");
contentPane.add(close);
close.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jf.dispose();
}
});
}
public static void main(String[] args)
{
new Index();
}
}