Java-如果之后定义了actionPerformed,则JButton文本会消失 [英] Java - JButton text disappears if actionPerformed defined afterwards

查看:83
本文介绍了Java-如果之后定义了actionPerformed,则JButton文本会消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这已经困扰了我一段时间。如果我在定义 setAction 的Jem 之前在之前在JButton上定义 setText ,则文本消失:

This has been bugging me for a while. If I define setText on a JButton before defining setAction, the text disappears:

JButton test = new JButton();
test.setText("test");  // Before - disappears!
test.setAction(new AbstractAction() {
    public void actionPerformed(ActionEvent e) {
        // do something
    }
});
this.add(test);

如果是之后,则没问题。

JButton test = new JButton();
test.setAction(new AbstractAction() {
    public void actionPerformed(ActionEvent e) {
        // do something
    }
});
test.setText("test");  // After - no problem!
this.add(test);

此外,如果我在JButton构造函数中设置了文字,那就很好了!

Furthermore, if I set the text in the JButton constructor, it's fine! Yarghh!

为什么会发生?

推荐答案

文档:


设置操作会导致立即更改Swing组件支持操作中描述的所有属性

Setting the Action results in immediately changing all the properties described in Swing Components Supporting Action.

这些属性在此处,并包含文本。

Those properties are described here, and include text.

这篇关于Java-如果之后定义了actionPerformed,则JButton文本会消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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