用Java Swing 创建一个登录对话框
下面的例子展示了如何用Java Swing 创建一个登录对话框。
final JFrame frame = new JFrame("Test JFrame");
JLabel label = new JLabel("User Name");
JTextField userName = new JTextField(20);
JLabel labelPass = new JLabel("Password");
JPasswordField pass = new JPasswordField(20);
JButton submit = new JButton("Submit");
submit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Submit");
}
});
JButton cancel = new JButton("Cancel");
cancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Cancel");
}
});
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
panel.add(label);
panel.add(userName);
panel.add(labelPass);
panel.add(pass);
panel.add(submit);
panel.add(cancel);
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(350, 150);
frame.setVisible(true);
运行效果:
- 点赞
- 收藏
- 关注作者
评论(0)