Java JButton.setAction(...)null的按钮文本 [英] Java JButton.setAction(...) null's button text

查看:234
本文介绍了Java JButton.setAction(...)null的按钮文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码在没有文字的情况下呈现 JButton

The following code renders a JButton without text:

public abstract class Test {

    public static void main(String... args) {

        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        }

        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame();
            JPanel panel = new JPanel();

            String text = "Invisible";
            JButton button = new JButton(text); // blank button rendered ???

            System.out.println(button.getText()); // prints Invisible

            button.setAction(new AbstractAction() {     
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    // do nothing
                }
            });

            System.out.println(button.getText()); // prints null ???

            button.setFocusable(false);

            button.setPreferredSize(new Dimension(100, 40));

            panel.add(button);

            frame.setResizable(false);
            frame.getContentPane().add(panel);

            frame.pack();
            frame.setVisible(true);

        });

    }

}

如果我删除调用 button.setAction(...)它会显示包含文字的按钮。

If i remove the call to button.setAction(...) it renders the button including the text.

或者:

public abstract class Test {

    public static void main(String... args) {

        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        }

        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame();
            JPanel panel = new JPanel();

            String text = "Invisible";
            JButton button = null;
            button = new JButton(new AbstractAction() {     
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    // do nothing
                }
            });

            button.setText(text); // renders the button with text
            System.out.println(button.getText()); // prints Invisible obviously

            button.setFocusable(false);

            button.setPreferredSize(new Dimension(100, 40));

            panel.add(button);

            frame.setResizable(false);
            frame.getContentPane().add(panel);

            frame.pack();
            frame.setVisible(true);

        });

    }

}

工作正常但是有一些讨厌的含义,如无法更改按钮操作而不重置其文本。

Works fine but has some nasty implications like not being able to change the buttons action without resetting its text after.

为什么?!

推荐答案

@Dima Maligin

@Dima Maligin


  • 不是答案,将被删除

  • not an answer, will be deleted

应声明Swing Action(也可以覆盖isEnabled,它可以是importnant)

Swing Action should be declared (override isEnabled too, it can be importnant)

 private class SwingAction extends AbstractAction {

   //or public Action SwingAction() {
   //       return new AbstractAction("Invisible") {
   //           here to override AbstractAction   
   //       } 
   //    } 

    public SwingAction() {
        putValue(NAME, "Invisible"); // bounds properties
        putValue(SHORT_DESCRIPTION, "Invisible");
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // do nothing
    }
}

这篇关于Java JButton.setAction(...)null的按钮文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆