JOptionPane.showOptionDialog的使用方法
public class Login extends JFrame {
//定义组件JPanel jp1,jp2,jp3;//面板
JLabel jlb1,jlb2;//标签
JButton jb1,jb2;//按钮
JTextField jtf;//文本
JPasswordField jpf;//密码
public Login(){
jp1=new JPanel();
jp2=new JPanel();
jp3=new JPanel();
jlb1=new JLabel("用户名");
jlb2=new JLabel("密 码");
jb1=new JButton("登录");
jb2=new JButton("重置");
jtf=new JTextField(10);
jpf=new JPasswordField(10);
this.setLayout(new GridLayout(3, 1));
jb1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//输入参数与数据库内容对比
JOptionPane.showMessageDialog(null, "登陆成功!");
Object[] options ={ "双人聊天", "多人聊天" };
int m = JOptionPane.showOptionDialog(null, "请选择聊天模式", "选项",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
if(m==0){
Face frm1=new Face();
Face frm2=new Face();
}
if(m==1){
String str=(String) JOptionPane.showInputDialog(null,"请输入聊天人数(大于2):\n","聊天人数确定",JOptionPane.PLAIN_MESSAGE,null,null,"在这输入");
int a=Integer.parseInt(str);
for(int i=0;i<a;i++)
new Face();
}
}
});
jb2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "重置成功!");
jtf.setText("");
jpf.setText("");
}
});
//加入各个组件
jp1.add(jlb1);
jp1.add(jtf);
jp2.add(jlb2);
jp2.add(jpf);
jp3.add(jb1);
jp3.add(jb2);
//加入到JFrame
this.add(jp1);
this.add(jp2);
this.add(jp3);
//设置窗体
this.setTitle("用户登录");//窗体标签
this.setSize(300, 150);//窗体大小
this.setLocationRelativeTo(null);//在屏幕中间显示(居中显示)
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//退出关闭JFrame
this.setVisible(true);//显示窗体
//锁定窗体
this.setResizable(false);
}
}