使用JOptionPane---摘自《JAVA疯狂讲义》

知识点:

1.JOptionPane圣诞框

2.带标题的边框面板

3.单选框的使用


package codes.c12.part2;


import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;


import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;


public class JOptionPaneTest {
   JFrame jf = new JFrame("测试JOptionPane");
   /*
    * 定义六个面板,分别用于定义 对话框的几种选项
    */
   private ButtonPanel messagePanel;
   private ButtonPanel messageTypePanel;
   private ButtonPanel msgPanel;
   private ButtonPanel confirmPanel;
   private ButtonPanel optionsPanel;
   private ButtonPanel inputPanel;
   private String messageString = "消息区内容";
   private Icon messageIcon = new ImageIcon("image/ico/heart.png");
   private Object messageObject = new Date();
   private Component messageComponent = new JButton("组件消息");
   private JButton msgBn = new JButton("消息对话框");
   private JButton confirmBn = new JButton("确认对话框");
   private JButton inputBn = new JButton("输入对话框");
   private JButton optionBn = new JButton("选项对话框");


   public void init() {
      JPanel top = new JPanel();
      top.setBorder(new TitledBorder(new EtchedBorder(), "对话框的通用选项",
            TitledBorder.CENTER, TitledBorder.TOP));
      top.setLayout(new GridLayout(1, 2));
      /*
       * 消息类型的Panel,该Panel中的选项决定对话框的图标
       */
      messageTypePanel = new ButtonPanel("选择消息的类型", new String[] {
            "ERROR_MESSAGE", "INFORMATION_MESSAGE", "WARNING_MESSAGE",
            "QUESTION_MESSAGE", "PLAIN_MESSAGE" });
      /*
       * 消息内容类型的Panel,该Panel中的选项决定对话框的图标
       */
      messagePanel = new ButtonPanel("选择消息内容的类型", new String[] { "字符串消息",
            "图标消息", "组件消息", "普通对象消息", "Object[]消息" });
      top.add(messageTypePanel);
      top.add(messagePanel);
      JPanel bottom = new JPanel();
      bottom.setBorder(new TitledBorder(new EtchedBorder(), "弹出不同的对话框",
            TitledBorder.CENTER, TitledBorder.TOP));
      bottom.setLayout(new GridLayout(1, 4));
      /*
       * 创建用于弹出消息对话框的Panel
       */
      msgPanel = new ButtonPanel("消息对话框", null);
      msgBn.addActionListener(new ShowAction());
      msgPanel.add(msgBn);
      /*
       * 创建用于弹出确认对话框的Panel
       */
      confirmPanel = new ButtonPanel("确认对话框", new String[] { "DEFAULT_OPTION",
            "YES_NO_OPTION", "YES_NO_CANCEL_OPTION", "OK_CANCEL_OPTION" });
      confirmBn.addActionListener(new ShowAction());
      confirmPanel.add(confirmBn);
      /*
       * 创建用于弹出输入对话框的Panel
       */
      inputPanel = new ButtonPanel("输入对话框", new String[] { "单行文本框", "下拉列表选择框" });
      inputBn.addActionListener(new ShowAction());
      inputPanel.add(inputBn);
      /*
       * 创建用于弹出选项对话框的Panel
       */
      optionsPanel = new ButtonPanel("选项对话框", new String[] { "字符串选项", "图标选项",
            "对象选项" });
      optionBn.addActionListener(new ShowAction());
      optionsPanel.add(optionBn);
      bottom.add(msgPanel);
      bottom.add(confirmPanel);
      bottom.add(inputPanel);
      bottom.add(optionsPanel);
      Box box = new Box(BoxLayout.Y_AXIS);
      box.add(top);
      box.add(bottom);
      jf.add(box);
      jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      jf.pack();
      jf.setVisible(true);
   }


   private int getOptionType() {
      switch (confirmPanel.getSelection()) {
      case "DEFAULT_OPTION":
         return JOptionPane.DEFAULT_OPTION;
      case "YES_NO_OPTION":
         return JOptionPane.YES_NO_OPTION;
      case "YES_NO_CANCEL_OPTION":
         return JOptionPane.YES_NO_CANCEL_OPTION;
      default:
         return JOptionPane.OK_CANCEL_OPTION;
      }
   }


   /**
    * 根据用户选择返回消息
    */
   private Object getMessage() {
      switch (messagePanel.getSelection()) {
      case "字符串消息":
         return messageString;
      case "图标消息":
         return messageIcon;
      case "组件消息":
         return messageComponent;
      case "普通对象消息":
         return messageObject;
      default:
         return new Object[] { messageString, messageIcon, messageComponent,
               messageObject };
      }
   }


   /**
    * 根据用户类型返回消息类型(决定图标区的图标)
    */
   private int getDialogType() {
      switch (messageTypePanel.getSelection()) {
      case "ERROR_MESSAGE":
         return JOptionPane.ERROR_MESSAGE;
      case "INFORMATION_MESSAGE":
         return JOptionPane.INFORMATION_MESSAGE;
      case "WARNING_MESSAGE":
         return JOptionPane.WARNING_MESSAGE;
      case "QUESTION_MESSAGE":
         return JOptionPane.QUESTION_MESSAGE;
      default:
         return JOptionPane.PLAIN_MESSAGE;
      }
   }


   private Object[] getOptions() {
      switch (optionsPanel.getSelection()) {
      case "字符串选项":
         return new String[] { "a", "b", "c", "d" };
      case "图标选项":
         return new Icon[] { new ImageIcon("image/ico/1.gif"),
               new ImageIcon("image/ico/2.gif"),
               new ImageIcon("image/ico/3.gif"),
               new ImageIcon("image/ico/4.gif"), };
      default:
         return new Object[] { new Date(), new Date(), new Date() };
      }
   }


   /**
    * 
    * @author pc
    * 
    */
   private class ShowAction implements ActionListener {
      public void actionPerformed(ActionEvent event) {
         switch (event.getActionCommand()) {
         case "确认对话框":
            JOptionPane.showConfirmDialog(jf, getMessage(), "确认对话框",
                  getOptionType(), getDialogType());
            break;
         case "输入对话框":
            if (inputPanel.getSelection().equals("单行文本框")) {
               JOptionPane.showInputDialog(jf, getMessage(), "输入对话框",
                     getDialogType());
            } else {
               JOptionPane.showInputDialog(jf, getMessage(), "输入对话框",
                     getDialogType(), null, new String[] { "轻量级Java EE企业应用实战",
                           "疯狂Java讲义" }, "疯狂Java讲义");
            }
            break;
         case "消息对话框":
            JOptionPane.showMessageDialog(jf, getMessage(), "消息对话框",
                  getDialogType());
            break;
         case "选项对话框":
            JOptionPane.showOptionDialog(jf, getMessage(), "选项对话框",
                  getOptionType(), getDialogType(), null, getOptions(), "a");
            break;
         }
      }
   }


   /*
    * 定义一个JPanel扩展类,该类的对象包含多个纵向排列的JRadioButton控件, <br>
    * 且扩展类可以指定一个字符串作为TitleBorder
    */
   class ButtonPanel extends JPanel {
      /**
       * 
       */
      private static final long serialVersionUID = 4585053132615306798L;
      private ButtonGroup group;


      public ButtonPanel(String title, String[] options) {
         setBorder(BorderFactory.createTitledBorder(
               BorderFactory.createEtchedBorder(), title));
         setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
         group = new ButtonGroup();
         for (int i = 0; options != null && i < options.length; i++) {
            JRadioButton b = new JRadioButton(options[i]);
            b.setActionCommand(options[i]);
            add(b);
            group.add(b);
            b.setSelected(i == 0);
         }
      }


      /*
       * 定义一个方法,用于返回用户选择的选项
       */
      public String getSelection() {
         return group.getSelection().getActionCommand();
      }
   }


   /**
    * @param args
    */
   public static void main(String[] args) {
      new JOptionPaneTest().init();
   }


}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值