JOptionPane对话框演示

package Assis;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class OptionPaneDemo extends JFrame implements ActionListener {
	private static final long serialVersionUID = 1L;
	// 创建四个功能按钮
	JButton btnMessage = new JButton("显示消息对话框");
	JButton btnConfirm = new JButton("显示确认对话框");
	JButton btnInput = new JButton("显示输入对话框");
	JButton btnOption = new JButton("显示选项对话框");

	public OptionPaneDemo() {
		// 设置框架窗体标题
		super("JOptionPaneDemo Demo");
		// 给四个功能按钮添加事件监听器
		btnMessage.addActionListener(this);
		btnConfirm.addActionListener(this);
		btnInput.addActionListener(this);
		btnOption.addActionListener(this);
		// 设置内容窗格的布局管理器为FlowLayout
		getContentPane().setLayout(new FlowLayout());
		// 把四个功能按钮加入到内容窗格中
		getContentPane().add(btnMessage);
		getContentPane().add(btnConfirm);
		getContentPane().add(btnInput);
		getContentPane().add(btnOption);
		// 显示框架窗体
		pack();
		setVisible(true);
	}

	// 程序的入口方法
	public static void main(String[] args) {
		OptionPaneDemo frame = new OptionPaneDemo();
		// 设置框架窗体的事件监听(关闭窗体事件)
		frame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
	}

	// 处理按钮事件
	public void actionPerformed(ActionEvent e) {
		Object objCommand = e.getSource();

		if (objCommand == btnMessage)
			JOptionPane.showMessageDialog(this, "这是个消息框对话框");
		else if (objCommand == btnConfirm)
			JOptionPane.showConfirmDialog(this, "这是个确认框对话框");
		else if (objCommand == btnInput)
			JOptionPane.showInputDialog(this, "这是个输入框对话框");
		else {
			Object[] options = { "确定", "取消" };
			JOptionPane.showOptionDialog(this, "这是个选项对话框", "选项对话框标题",
					JOptionPane.YES_OPTION, JOptionPane.QUESTION_MESSAGE, null,
					options, options[0]);
		}

	}
}

 

你可能感兴趣的:(框架,swing)