复杂的对话框

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

public class FirstSample{
    public static void main(String[] args){
        OptionDialogFrame frame=new OptionDialogFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

class ButtonPanel extends JPanel{
    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;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();
    }
   
    private ButtonGroup group;
}

class OptionDialogFrame extends JFrame{
    public OptionDialogFrame(){
        setTitle("OptionDialogTest");
        setSize(WIDTH,HEIGHT);
       
        JPanel gridPanel=new JPanel();
        gridPanel.setLayout(new GridLayout(2,3));
       
        typePanel=new ButtonPanel("Type",new String[]{"Message","Confirm","Option","Input"});
        messageTypePanel=new ButtonPanel("Message Type",new String[]{"ERROR_MESSAGE","INFOORMATION_MESSAGE","WARNING_MESSAGE","QUESTION_MESSAGE","PLAIN_MESSAGE"});
        messagePanel=new ButtonPanel("Message",new String[]{"String","Icon","Component","Other","Object[]"});
        optionTypePanel=new ButtonPanel("Confirm",new String[]{"DEFAULT_OPTION","YES_NO_OPTION","YES_NO_CANCEL_OPTION","OK_CANCEL_OPTION"});
        optionsPanel=new ButtonPanel("Option",new String[]{"String[]","Icon[]","Object[]"});
        inputPanel=new ButtonPanel("Input",new String[]{"Text field","Combo box"});
        gridPanel.add(typePanel);
        gridPanel.add(messageTypePanel);
        gridPanel.add(messagePanel);
        gridPanel.add(optionTypePanel);
        gridPanel.add(optionsPanel);
        gridPanel.add(inputPanel);
       
        JPanel showPanel=new JPanel();
        JButton showButton=new JButton("Show");
        showButton.addActionListener(new ShowAction());
        showPanel.add(showButton);
       
        add(gridPanel,BorderLayout.CENTER);
        add(showPanel,BorderLayout.SOUTH);
    }
   
    public Object getMessage(){
        String s=messagePanel.getSelection();
        if(s.equals("String")) {return messageString;}
        if(s.equals("Icon")) {return messageIcon;}
        if(s.equals("Component")) {return messageComponent;}
        if(s.equals("Object[]")) {return new Object[]{messageString,messageIcon,messageComponent,messageObject};}
        if(s.equals("Other")) {return messageObject;}
        else return null;
    }
   
    public Object[] getOptions(){
        String s=optionsPanel.getSelection();
        if(s.equals("String[]"))
            return new String[] {"Red","Green","Blue"};
        else if(s.equals("Icon[]"))
            return new Icon[] {new ImageIcon("red.gif"),new ImageIcon("green.gif"),new ImageIcon("blue.gif")};
        else if(s.equals("Object[]"))
            return new Object[] {messageString,messageIcon,messageComponent,messageObject};
        else
            return null;
    }
   
    public int getType(ButtonPanel panel){
        String s=panel.getSelection();
        try{
            return JOptionPane.class.getField(s).getInt(null);
        }
        catch(Exception e){
            return -1;
        }
    }
   
    private class ShowAction implements ActionListener{
        public void actionPerformed(ActionEvent event){
            if(typePanel.getSelection().equals("Message")){
                JOptionPane.showMessageDialog(OptionDialogFrame.this,getMessage(),"Title",getType(messageTypePanel));
            }
            if(typePanel.getSelection().equals("Confirm")){
                JOptionPane.showConfirmDialog(OptionDialogFrame.this,getMessage(),"Title",getType(optionTypePanel),getType(messageTypePanel));
            }
            if(typePanel.getSelection().equals("Option")){
                JOptionPane.showOptionDialog(OptionDialogFrame.this, getMessage(),"Title",getType(optionTypePanel),getType(messageTypePanel),null,getOptions(),getOptions()[0]);
            }
            if(typePanel.getSelection().equals("Input")){
                if(inputPanel.getSelection().equals("Text field")){
                    JOptionPane.showInputDialog(OptionDialogFrame.this,getMessage(),"Title",getType(messageTypePanel));
                }
                else{
                    JOptionPane.showInputDialog(OptionDialogFrame.this,getMessage(),"Title",getType(messageTypePanel),null,new String[]{"Red","Green","Blue"},"Red");
                }
            }
        }
    }
   
    public static final int WIDTH=600;
    public static final int HEIGHT=400;
   
    private ButtonPanel typePanel;
    private ButtonPanel messagePanel;
    private ButtonPanel messageTypePanel;
    private ButtonPanel optionTypePanel;
    private ButtonPanel optionsPanel;
    private ButtonPanel inputPanel;
   
    private String messageString="Message";
    private Icon messageIcon=new ImageIcon("blue.gif");
    private Object messageObject=new Date();
    private Component messageComponent=new SamplePanel();
}

class SamplePanel extends JPanel{
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        Graphics2D g2=(Graphics2D) g;
        Rectangle2D rect=new Rectangle2D.Double(0,0,getWidth()-1,getHeight()-1);
        g2.setPaint(Color.YELLOW);
        g2.fill(rect);
        g2.setPaint(Color.BLUE);
        g2.draw(rect);
    }
   
    public Dimension getMinimumSize(){
        return new Dimension(10,10);
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值