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);
}
}