为什么按钮不反映Action'NAME属性?

时间:2012-03-24 08:43:11

标签: java swing action modal-dialog jbutton

我认为按钮在下面的代码中应该有“关闭”标题,但它没有:

public class Test_Actions extends JDialog 
{   
    private AbstractAction closeAction = new AbstractAction() 
    {       
        {
            putValue("NAME", "Close");
        }

        @Override
        public void actionPerformed(ActionEvent arg0) 
        {
            Test_Actions.this.setVisible(false);
            Test_Actions.this.dispatchEvent(new WindowEvent(Test_Actions.this, WindowEvent.WINDOW_CLOSING));
        }       
    };

    public Test_Actions() 
    {       
        JLabel label = new JLabel("Hello world");

        JButton button = new JButton(closeAction);
        //button.setText("Text");

        setLayout(new BorderLayout());
        add(label, BorderLayout.CENTER);
        add(button, BorderLayout.SOUTH);

        setDefaultCloseOperation(DISPOSE_ON_CLOSE);

    }

    public static void main(String[] args) throws InterruptedException, InvocationTargetException 
    {
        final Test_Actions dialog = new Test_Actions();
        dialog.setModal(true);

        SwingUtilities.invokeAndWait(new Runnable() 
        {
            @Override
            public void run() 
            {
                dialog.pack();
                dialog.setVisible(true);
            }       
        });

        System.out.println("Here");
    }
}

1 个答案:

答案 0 :(得分:8)

使用Action.NAME代替"NAME"

value of this constant实际上是"Name"所以你也可以使用它,但正如你所看到的那样容易出错。