我想点击一个按钮时弹出一个窗口。弹出窗口应该包含一个标签名称。按钮CLOSE按钮

时间:2015-12-09 07:02:10

标签: java jbutton joptionpane

当我按下按钮时,我想要一个弹出窗口。可以用正确的代码告诉我。这是我的代码。我是java swings的新手。请提前感谢。我的代码是。

            JButton testButton=new JButton("test");
                    toolBar.addComponentRight(testButton);
                    testButton.addActionListener(new ActionListener()
                    {
                        public void actionPerformed(ActionEvent e)
                        {
    here I want to replace code-------- 
                            JOptionPane.showMessageDialog(null,"you clicked me");

                        }
                    });

        Here in output pop up window  I am getting only OK button.I need CANCEL button also along with OK button.Can anyone suggest me with the right code.Thank you in advance.

2 个答案:

答案 0 :(得分:2)

你可以做同样的事情,

JOptionPane.showConfirmDialog((Component) null, "Do you want to close ?", "Window Title", JOptionPane.OK_CANCEL_OPTION);

它同样会显示,

enter image description here

答案 1 :(得分:0)

I got the code.I hope it would be useful.here is my modified code.Thank you vishal.


     JButton testButton=new JButton("test");
                        toolBar.addComponentRight(testButton);
                        testButton.addActionListener(new ActionListener()
                        {
                            public void actionPerformed(ActionEvent e)
                            {


           JOptionPane.showConfirmDialog((Component) null, "Do you want to close ?", "Window Title", JOptionPane.OK_CANCEL_OPTION);
    }
    });