public static void main(String[] args) {
JFrame jf=new JFrame("测试对话框");
jf.setSize(400,300);
jf.setLocationRelativeTo(null);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//1,消息对话框
JButton btn1=new JButton("showMessageDialog 消息框");
btn1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//对话框
JOptionPane.showMessageDialog(jf,
"李乃龙最帅",
"谁最帅",
JOptionPane.INFORMATION_MESSAGE);
}
});
//2,消息对话框(警告)
JButton btn2=new JButton("showMessageDialog 警告");
btn2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//对话框
JOptionPane.showMessageDialog(jf,
"拿出你的IQ CQ QQ",
"打劫",
JOptionPane.WARNING_MESSAGE);
}
});
//3,确认对话框
JButton btn3=new JButton("showMessageDialog 确认对话框");
btn3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//需要接收结果
// JOptionPane.YES_NO_OPTION 指是否
// JOptionPane.YES_OPTION 是否
// JOptionPane.YES_NO_CANCEL_OPTION 是否都取消
int result=JOptionPane.showConfirmDialog(jf,
"确认分手吗",
"分手快乐",
// JOptionPane.YES_NO_OPTION 指是否
// JOptionPane.YES_OPTION 是否
// JOptionPane.YES_NO_CANCEL_OPTION 是否都取消
JOptionPane.YES_NO_OPTION);
System.out.println(result);
}
});
//4,输入框
JButton btn4=new JButton("输入对话框");
btn4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String msg=JOptionPane.showInputDialog(jf,"请输入你的银行密码","88888888");
System.out.println("收到密码"+msg);
}
});
//5,输入框 下拉选择
JButton btn5=new JButton("下拉对话框");
btn5.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Object[]selectValues={"中国","美国","日本"};
//显示输入框
Object content=JOptionPane.showInputDialog(
jf,
"选择你想去的城市",
"个人去向",
JOptionPane.PLAIN_MESSAGE,
null,//图标
selectValues,
selectValues[0]);
System.out.println("你的选项为"+content);
}
});
//6,选项对话框
JButton btn6=new JButton("选项对话框");
btn6.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Object[]selectValues={"中国","美国","日本"};
int optionSelect=JOptionPane.showOptionDialog(
jf,
"请选一种",
"买它买它",
JOptionPane.YES_OPTION,
JOptionPane.ERROR_MESSAGE,
null,// 图标
selectValues,
selectValues[2]
);
System.out.println("你的选项为"+optionSelect);
}
});
//盒子工具
Box vbox=Box.createVerticalBox();
vbox.add(btn1);
vbox.add(btn2);
vbox.add(btn3);
vbox.add(btn4);
vbox.add(btn5);
vbox.add(btn6);
JPanel panel=new JPanel();
panel.add(vbox);
jf.setContentPane(panel);
jf.setVisible(true);
}
Swing测试对话框
最新推荐文章于 2021-07-27 10:06:17 发布