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

📄 joptionpanetest.java

📁 详细讲解了java的基础知识
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;

public class JOptionPaneTest extends JApplet{
 
  JPanel jPanel1= new JPanel();
  JFrame jFrame1=new JFrame();
  JButton jButton1 = new JButton();
  JButton jButton2 = new JButton();
  JButton jButton3 = new JButton();
  JButton jButton4= new JButton();
  JButton jButton5 = new JButton();
  JButton jButton6= new JButton();
  JButton jButton7= new JButton();
  JButton jButton8 = new JButton();
  JButton jButton9= new JButton();
  JButton jButton10= new JButton();	
	
  GridLayout gridLayout1 = new GridLayout(); 

  //初始化
  public void init() {
    try  {
      myInit();
    }
    catch(Exception e)  {   //异常处理
      e.printStackTrace();
    }
  }

  //组件初始设定
  private void myInit() throws Exception {
    this.setSize(new Dimension(300, 300));
    this.getContentPane ().setLayout(new BorderLayout(5,5));
    jPanel1.setLayout(gridLayout1);
    gridLayout1.setColumns(2);
    gridLayout1.setHgap(5);
    gridLayout1.setRows(5);
    gridLayout1.setVgap(5);
	jPanel1.add(jButton1);
	jPanel1.add(jButton2);
	jPanel1.add(jButton3);
	jPanel1.add(jButton4);
	jPanel1.add(jButton5);
	jPanel1.add(jButton6);
	jPanel1.add(jButton7);
	jPanel1.add(jButton8);
	jPanel1.add(jButton9);
	jPanel1.add(jButton10);
    this.getContentPane().add(jPanel1, BorderLayout.CENTER);
	
	
 
    jButton1.setText("JOptionPane示范一");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(ActionEvent e) {
        jButton1_actionPerformed(e);
      }
    });
    jButton2.setText("JOptionPane示范二");
   jButton2.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(ActionEvent e) {
        jButton2_actionPerformed(e);
      }
    });
   jButton3.setText("JOptionPane示范三");
   jButton3.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(ActionEvent e) {
        jButton3_actionPerformed(e);
      }
    });
	jButton4.setText("JOptionPane示范四");
   jButton4.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(ActionEvent e) {
        jButton4_actionPerformed(e);
      }
    });
	jButton5.setText("JOptionPane示范五");
   jButton5.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(ActionEvent e) {
        jButton5_actionPerformed(e);
      }
    });
	jButton6.setText("JOptionPane示范六");
   jButton6.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(ActionEvent e) {
        jButton6_actionPerformed(e);
      }
    });
	jButton7.setText("JOptionPane示范七");
   jButton7.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(ActionEvent e) {
        jButton7_actionPerformed(e);
      }
    });
	jButton8.setText("JOptionPane示范八");
   jButton8.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(ActionEvent e) {
        jButton8_actionPerformed(e);
      }
    });
	jButton9.setText("JOptionPane示范九");
   jButton9.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(ActionEvent e) {
        jButton9_actionPerformed(e);
      }
    });
	jButton10.setText("JOptionPane示范十");
   jButton10.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(ActionEvent e) {
        jButton10_actionPerformed(e);
      }
    });
       this.resize(300,200);
  }
	

 
 	void jButton1_actionPerformed(ActionEvent e) {  	
	JOptionPane optionPane = new JOptionPane(
		new JLabel("这是一个用OptionPane产生的对话窗口"), // message
		JOptionPane.INFORMATION_MESSAGE); // messageType
		JDialog dialog = optionPane.createDialog(
			 this.getContentPane(), // parentComponent
			"Option-Dialog"); // title
		dialog.setVisible(true);
	
		
  	}
		
	void jButton2_actionPerformed(ActionEvent e) {
  	  	JOptionPane optionPane = new JOptionPane(
		"这是一个用OptionPane产生的内部窗体", // message
		JOptionPane.INFORMATION_MESSAGE); // messageType
		JInternalFrame jif=optionPane.createInternalFrame(
			this.getContentPane(), "InternalFrame");
		jif.setVisible(true);
  	}
	void jButton3_actionPerformed(ActionEvent e) {
  	  	JOptionPane.showMessageDialog(
		jButton3, // parentComponent
		 "警告!吸烟容易致癌&*#@^*...", // message
		"警告标志", // title
		JOptionPane.ERROR_MESSAGE); 
		
  	}
	void jButton4_actionPerformed(ActionEvent e) {
  	  	 JOptionPane.showInternalMessageDialog(this.getContentPane(), 
		"小心!电脑看太多会近视!!", 		
		"InternalMessageDialog", JOptionPane.ERROR_MESSAGE);
		

  	}
	void jButton5_actionPerformed(ActionEvent e) {
  	  	JOptionPane.showConfirmDialog(null,
		"您老实说没关系,Java真的好学吗?", 
		"Java甘苦谈", JOptionPane.YES_NO_OPTION,
		JOptionPane.QUESTION_MESSAGE);
		


  	}
	void jButton6_actionPerformed(ActionEvent e) {
  	  	JOptionPane.showInternalConfirmDialog(this.getContentPane(),
		"Swing好看又好用吗?", "Swing-'荡'的眼花缭乱",
		JOptionPane.YES_NO_CANCEL_OPTION, 
		JOptionPane.QUESTION_MESSAGE);
			
		

  	}
	void jButton7_actionPerformed(ActionEvent e) {
	 	Object[] possibleValues = { "Epson Stylus Color 600", 
				"HP DJ1600C", "Canon LBP2460" };
		String s = (String)JOptionPane.showInputDialog(jButton10,
		"请选择欲使用的打印机", "选项对话框",
		JOptionPane.INFORMATION_MESSAGE, 
		new ImageIcon("printer.gif"),possibleValues, possibleValues[0]);
		if(s == null)
			showStatus("cancel button activated");
		else
			showStatus("选用的打印机"+s);
		

	}
	void jButton8_actionPerformed(ActionEvent e) {
		String s = JOptionPane.showInternalInputDialog(
			this.getContentPane(),"请输入您的姓名:");

				if(s == null)
					showStatus("取消输入对话框");
				else
					showStatus("所输入的姓名: " + s);
	}
	
	void jButton9_actionPerformed(ActionEvent e) {
	JButton button = new JButton("show dialog ...");
	JButton applyBtn = new JButton("应用");
	final ExchangePanel exPanel = new ExchangePanel();
	String title = "请选择兑换的钱币";
	Object[] btnObjects = new Object[] {
		"确定", 
		applyBtn,
		"Cancel",
	};
	int value = JOptionPane.showOptionDialog(
			jButton9, // parentComponent
			exPanel, // Object message-Panel也可以当message!
			title, // title
			JOptionPane.DEFAULT_OPTION, // optionType
			JOptionPane.PLAIN_MESSAGE, // messageType
			null, // icon
			btnObjects, // options
			applyBtn); // initialValue 

	switch(value) {
		case JOptionPane.CLOSED_OPTION:
			showStatus("对话窗口被关掉了");
			break;
		case JOptionPane.OK_OPTION:
			showStatus("所选择的兑换钱币:" +
			exPanel.getSelectedCurrency() );
			break;
		case JOptionPane.CANCEL_OPTION:
			showStatus("取消对话窗口");
			break;
	}
		
	
    	}
	void jButton10_actionPerformed(ActionEvent e) {
	 	
		Object[] options = { "确定", "取消" };
		JOptionPane.showOptionDialog(jButton6, "按钮也可以改成中文喔!", 
			"你讲台语嘛也通!?",
		JOptionPane.OK_CANCEL_OPTION, 
		JOptionPane.WARNING_MESSAGE,
		null, options, options[0]);

	}
	
class ExchangePanel extends JPanel { 
	private ButtonGroup btnGroup = new ButtonGroup();

	private JRadioButton[] jrButtons = {
		new JRadioButton("美元"),
		new JRadioButton("日元"),
		new JRadioButton("卢布"),
		new JRadioButton("马克"),
	};
	public ExchangePanel() {//建构函数
		setBorder(BorderFactory.createTitledBorder("兑换钱币种类"));

		for(int i=0; i < jrButtons.length; ++i) {
			if(i ==0)
				jrButtons[i].setSelected(true);
			add(jrButtons[i]);
			btnGroup.add(jrButtons[i]);
		}
	}
	public String getSelectedCurrency() {
		String sc = null;  // sc = 所选择的货币

		for(int i=0; i < jrButtons.length; ++i) {
			if(jrButtons[i].isSelected())
				sc= jrButtons[i].getText();
		}
		return sc;
	}

	}
  
} 



⌨️ 快捷键说明

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