FrameMessageboxDemo

public class FrameMessageboxDemo extends JFrame
{
    private static final long serialVersionUID = 1L;
    
    private JButton[] b = {new JButton("确认"), new JButton("警告"), new JButton("爱好"), new JButton("录入爱好"),
        new JButton("选择爱好")};
    
    private JTextField textField = new JTextField(20);
    
    private ActionListener al = new ActionListener()
    {
        
        @Override
        public void actionPerformed(ActionEvent e)
        {
            String id = ((JButton)e.getSource()).getText();
            if ("警告".equals(id))
            {
                JOptionPane.showMessageDialog(null, "系统运行期间出现了错误,请与程序员联系!", "系统提示!", JOptionPane.ERROR_MESSAGE);
                
            }
            else if ("确认".equals(id))
            {
                JOptionPane.showConfirmDialog(null, "你确认退出系统吗?", "系统提示!", JOptionPane.YES_NO_OPTION);
            }
            else if ("爱好".equals(id))
            {
                Object[] options = {"协作", "政治官僚"};
                int sel = JOptionPane.showOptionDialog(null,
                    "请选择您的爱好!",
                    "系统提示!",
                    JOptionPane.DEFAULT_OPTION,
                    JOptionPane.WARNING_MESSAGE,
                    null,
                    options,
                    options[0]);
                if (sel != JOptionPane.CLOSED_OPTION)
                {
                    textField.setText("您选择的个人爱好为:" + options[sel]);
                }
                
            }
            else if ("录入爱好".equals(id))
            {
                String val = JOptionPane.showInputDialog("您爱好什么?");
                textField.setText(val);
            }
            else if ("选择爱好".equals(id))
            {
                Object[] selections = {"写作", "政治", "军事"};
                // JOptionPane.INFORMATION_MESSAGE :选择框的形式显示
                Object val = JOptionPane.showInputDialog(null,
                    "请输入您的爱好",
                    "系统提示",
                    JOptionPane.INFORMATION_MESSAGE,
                    null,
                    selections,
                    selections[0]);
                if (val != null)
                {
                    textField.setText(val.toString());
                }
            }
        }
    };
    
    private FlowLayout flowLayout = new FlowLayout();
    
    private JLabel label = new JLabel();
    
    private void jbInit()
        throws Exception
    {
        Container cop = this.getContentPane();
        cop.setLayout(flowLayout);
        for (int i = 0; i < b.length; i++)
        {
            b[i].addActionListener(al);
            cop.add(b[i]);
        }
        this.setTitle("消息框(Message box)");
        label.setText("显示消息");
        cop.add(textField, null);
        cop.add(label);
    }
    
    public FrameMessageboxDemo()
    {
        try
        {
            jbInit();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args)
    {
        FrameMessageboxDemo frame = new FrameMessageboxDemo();
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
    
}
 

你可能感兴趣的:(message)