⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 joptionpanetest.java

📁 编程之道--java程序设计入门源代码
💻 JAVA
字号:
/**
 * 对话框的测试
 */
import java.awt.Container;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JOptionPane;

public class JOptionPaneTest
{
	public static void main(String[] args)
	{
		JOptionPaneFrame frame = new JOptionPaneFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.show();
	} 
}

/**
 * 这是定制自己的按钮框架
 */
class JOptionPaneFrame extends JFrame
{
	private static final int WIDTH = 400;
	private static final int HEIGHT = 300;
	
	private JButton messageButton;
	private JButton confirmButton;
	private JButton optionButton;
	private JButton inputButton;
	
	public JOptionPaneFrame()
	{
		setSize(WIDTH, HEIGHT);//设置框架的大小		
		setTitle("对话框测试");//设置框架的标题
		
		Container con = getContentPane();//得到了内容窗格
		
		messageButton = new JButton("Show Message Dialog");
	    confirmButton = new JButton("Show Confirm Dialog");
	    optionButton = new JButton("Show Option Dialog");
	    inputButton = new JButton("Show Input Dialog");
	    
	    messageButton.addActionListener(new OptionAction("message"));
	    confirmButton.addActionListener(new OptionAction("confirm"));
	    optionButton.addActionListener(new OptionAction("option"));
	    inputButton.addActionListener(new OptionAction("input"));
		
		JPanel buttonPanel = new JPanel();
		buttonPanel.add(messageButton);	
		buttonPanel.add(confirmButton);
		buttonPanel.add(optionButton);
		buttonPanel.add(inputButton);
		con.add(buttonPanel, BorderLayout.CENTER);
	}
	
	class OptionAction implements ActionListener
	{
	    private String str;
	    public OptionAction(String str)
	    {
	        this.str = str;
	    }
	    
	    public void actionPerformed(ActionEvent event)
	    {
	        if(str.equals("message"))
	            JOptionPane.showMessageDialog(null, "this is the message");
	        
	        if(str.equals("confirm"))
	            JOptionPane.showConfirmDialog(null, 
	            "this is the confirm message", 
	            "this is the confirm dialog",
	            JOptionPane.YES_NO_CANCEL_OPTION);
	        
	        if(str.equals("option"))
	        {
	            Object[] options = { "OK", "CANCEL" };
                JOptionPane.showOptionDialog(null, 
                "Click OK to continue", "Warning", 
                JOptionPane.DEFAULT_OPTION, 
                JOptionPane.WARNING_MESSAGE,
                null, options, options[0]);
	        }
	    
	        if(str.equals("input"))
	        {
	            Object[] possibleValues = { "First", "Second", "Third" };
                JOptionPane.showInputDialog(null,
                "Please Choose one", "Input",
                JOptionPane.INFORMATION_MESSAGE,
                new ImageIcon("blue.gif"),
                possibleValues, possibleValues[0]);
	        }
	    }
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -