代码示例
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class a extends JFrame implements ActionListener{
public static void main(String[] args) {
new a();
}
JLabel JL1 = new JLabel ("用户名:");
JLabel JL2 = new JLabel ("密码 :");
JComboBox JC = new JComboBox(new String[] {"123","456"});
JPasswordField JP1 = new JPasswordField(10);
JButton JB1 = new JButton("确定");
JButton JB2 = new JButton("取消");
JRadioButton JR1 =new JRadioButton("用户登陆");
JRadioButton JR2 =new JRadioButton("管理员登陆");
ButtonGroup BG=new ButtonGroup();
public a() {
JPanel JP = (JPanel) this.getContentPane();
JP.setLayout(new GridLayout(4,2,10,10));
JB1.addActionListener(this);
JB2.addActionListener(this);
BG.add(JR1);
BG.add(JR2);
JP.add(JL1);
JP.add(JC);
JP.add(JL2);
JP.add(JP1);
JP.add(JR1);
JP.add(JR2);
JP.add(JB1);
JP.add(JB2);
this.setSize(300,200);
this.setLocationRelativeTo(null);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==JB1) {
if(JR1.isSelected()) {
if(JC.getSelectedItem().equals("123") && JP1.getText().equals("123"))
JOptionPane.showMessageDialog(this,"欢迎您,用户"+JC.getSelectedItem()+"登陆成功");
else
JOptionPane.showMessageDialog(this, "用户名或密码错误");
}
else if(JR2.isSelected()) {
if(JC.getSelectedItem().equals("456") && JP1.getText().equals("456"))
JOptionPane.showMessageDialog(this,"欢迎您,管理员"+JC.getSelectedItem()+"登陆成功");
else
JOptionPane.showMessageDialog(this, "用户名或密码错误");
}
}
if(e.getSource()==JB2) {
System.exit(0);
}
}
}
图例
![在这里插入图片描述](https://img-blog.csdnimg.cn/3afc0401d04149b8a4bbc0f552b28013.png)