java swing標準對話方塊具體實現

來源:互聯網
上載者:User

複製代碼 代碼如下:package test001;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JToolBar;
public class TestJOptionPane implements ActionListener{
    private JFrame jf = new JFrame("標準對話方塊測試");
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new TestJOptionPane().createUI();
    }
    public void createUI(){
        JToolBar jtb = new JToolBar();
        String[] s = {"錯誤", "退出確認1", "退出確認2", "警告", "輸入", "選擇"};
        int size = s.length;
        JButton[] button = new JButton[size];
        for(int i = 0; i < size; i++){
            button[i] = new JButton(s[i]);
            button[i].addActionListener(this);
            jtb.add(button[i]);
        }
        jf.add(jtb, "North");
        jf.setSize(350, 150);
        jf.setLocation(400, 200);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setVisible(true);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        String s = e.getActionCommand();
        if(s.equals("錯誤")){
            JOptionPane.showMessageDialog(null, "要顯示的錯誤資訊---",
                    "錯誤提示",JOptionPane.ERROR_MESSAGE);
        }
        else if(s.equals("退出確認1")){
            int result = JOptionPane.showConfirmDialog(null,
                    "推出前是否儲存程式?");
            if(result == JOptionPane.YES_OPTION){
                System.out.println("儲存程式---");
                System.exit(0);
            }
            else if(result == JOptionPane.NO_OPTION){
                System.exit(0);
            }
        }
        else if(s.equals("退出確認2")){
            int result = JOptionPane.showConfirmDialog(null, "退出前是否儲存程式?");
            if(result == JOptionPane.YES_OPTION){
                System.out.println("儲存程式---");
                System.exit(0);
            }
            else if(result == JOptionPane.NO_OPTION){
                System.exit(0);
            }
        }
        else if(s.equals("警告")){
            Object[] options = {"繼續", "撤銷"};
            int result = JOptionPane.showOptionDialog(null,
                    "本操作可能導致資料丟失","Warning", JOptionPane.DEFAULT_OPTION,
                    JOptionPane.WARNING_MESSAGE, null, options, options[0]);
            if(result == 0){
                System.out.println("繼續操作---");
            }
        }
        else if(s.equals("輸入")){
            String name = JOptionPane.showInputDialog("請輸入您的姓名:");
            if(name != null){
                System.out.println("姓名:" + name);
            }
        }
        else if(s.equals("選擇")){
            Object[] possibleValues = {"體育", "政治", "經濟", "文化"};
            Object selectedValue = JOptionPane.showInputDialog(null,
                    "Choose one","Input", JOptionPane.INFORMATION_MESSAGE, null,
                    possibleValues, possibleValues[0]);
            String choose = (String)selectedValue;
            if(choose != null){
                System.out.println("你選擇的是:"+ choose);
            }
        }
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.