📄 joptiondemo .txt
字号:
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.*;
import javax.swing.JOptionPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JDialog;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class JOptionDemo extends JFrame {
public JOptionDemo() {
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
getContentPane().setLayout(null);
jButton1.setBounds(new Rectangle(23, 35, 119, 47));
jButton1.setText("确定取消");
//添加监听
jButton1.addActionListener(new JOptionDemoactionAdapter(this));
jButton2.setBounds(new Rectangle(151, 34, 115, 47));
jButton2.setText("提示输入文本");
//添加监听
jButton2.addActionListener(new JOptionDemoactionAdapter(this));
jButton5.setBounds(new Rectangle(29, 195, 358, 90));
jButton5.setText("模式对话框");
//添加监听
jButton5.addActionListener(new JOptionDemoactionAdapter(this));
jButton4.setBounds(new Rectangle(28, 110, 358, 76));
jButton4.setText("组合对话框");
//添加监听
jButton4.addActionListener(new JOptionDemoactionAdapter(this));
jButton3.setBounds(new Rectangle(280, 35, 102, 45));
jButton3.setText("显示消息");
//添加监听
jButton3.addActionListener(new JOptionDemoactionAdapter(this));
this.getContentPane().add(jButton4);
this.getContentPane().add(jButton5);
this.getContentPane().add(jButton3);
this.getContentPane().add(jButton1);
this.getContentPane().add(jButton2);
}
public static void main(String[] args) {
JOptionDemo joptiondemo = new JOptionDemo();
joptiondemo.setSize(450, 400);
joptiondemo.setVisible(true);
}
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JButton jButton3 = new JButton();
JButton jButton4 = new JButton();
JButton jButton5 = new JButton();
}
//监听器类++++++++++++++++++++++++++++++++
class JOptionDemoactionAdapter implements ActionListener {
private JOptionDemo adaptee;
JOptionDemoactionAdapter(JOptionDemo adaptee) {
this.adaptee = adaptee;
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
public void actionPerformed(ActionEvent e) {
if (e.getSource() == adaptee.jButton1) {
//确定取消
int response;
response = JOptionPane.showConfirmDialog(null, "这是一个确定取消对话框!!!",
"确定与取消", JOptionPane.OK_CANCEL_OPTION);
System.out.println("返回值" + response);
} else if (e.getSource() == adaptee.jButton2) {
//提示输入文本
String response;
response = JOptionPane.showInputDialog(null, "输入对话框(请输入你的名字):");
System.out.println("你的名字是:" + response);
} else if (e.getSource() == adaptee.jButton3) {
//消息对话框
JOptionPane.showMessageDialog(null, "作业一定要完成啊!!");
System.out.println("消息对话框");
} else if (e.getSource() == adaptee.jButton4) {
//选项对话框
JButton[] gender = new JButton[3];
gender[0] = new JButton("张晋旺");
gender[1] = new JButton("侯虎兵");
gender[2] = new JButton("好好学习");
int response;
response = JOptionPane.showOptionDialog(null, "你们做什么?", "我是学生", 0,
JOptionPane.INFORMATION_MESSAGE, null, gender, gender[2]);
System.out.println("jButton4");
} else if (e.getSource() == adaptee.jButton5) {
//模式窗口 false为非模式
JDialog DD = new JDialog(adaptee, "模式窗口", true);
DD.setBounds(200, 200, 300, 300);
JButton a=new JButton("刘宇航努力学习才是好孩子!!!");
DD.getContentPane().add(a);
DD.setVisible(true);
System.out.println("模式对话框");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -