JButton 带有 HTML 命名的动作和助记符

发布于 2024-08-18 10:32:34 字数 289 浏览 4 评论 0原文

我有一个使用 Action 构造的 JButton,该操作的名称包含 html。

然后,我首先解析 html 以获取名称中的第一个字符,从而在 JButton 上设置助记符。

例如,JButton 名称可能是 "Test
Button"
,因此解析 html 后助记键应该是“T” ”。

因此,现在当呈现 JButton 时,我可以按 alt-T 来激活该按钮,但是 T 上的下划线助记符指示器不存在。

有人知道有什么方法可以让这种情况发生吗?

I have a JButton that is constructed using an Action and this action has a name that is contains html.

I then go about setting the mnemonic on the JButton by first parsing out html to get the first character in the name.

For example, the JButton name might be "<html>Test<br>Button</html>", so after parsing the html the mnemonic key should be "T".

So now when the JButton is rendered I can push alt-T to activate the button, however the underline mnemonic indicator on the T is not present.

Would anyone know a way to get this to occur?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清风无影 2024-08-25 10:32:34

我不清楚“通过解析 html 在 JButton 上设置助记符”是什么意思。可以通过调用JButton类中的setMnemonic方法在JButton上设置助记符。我尝试了下面的代码,当我按 Alt+P 时,我收到控制台中打印的消息 I am press
<代码> <代码>

public class HTMLButton extends JPanel implements ActionListener {
    JButton     b1;
    public HTMLButton() {
        super(new BorderLayout());
        b1 = new JButton("<html><b><u>P</u>ress</b></html>");
        b1.setMnemonic(KeyEvent.VK_P);
        b1.addActionListener(this);
        add(b1);
    }

    public void actionPerformed(final ActionEvent e) {
        System.out.println("I am pressed");
    }
}

>
另请参阅如何在 Swing 组件中使用 HTML 在 Java 教程中。

It is unclear to me what you mean by "setting the mnemomic on the JButton by parsing html". The mnemonic can be set on a JButton by calling the method setMnemonic in the JButton class. I tried below piece of code and when I press Alt+P I get the message I am pressed printed in the console.

public class HTMLButton extends JPanel implements ActionListener {
    JButton     b1;
    public HTMLButton() {
        super(new BorderLayout());
        b1 = new JButton("<html><b><u>P</u>ress</b></html>");
        b1.setMnemonic(KeyEvent.VK_P);
        b1.addActionListener(this);
        add(b1);
    }

    public void actionPerformed(final ActionEvent e) {
        System.out.println("I am pressed");
    }
}


Also see the section How to Use HTML in Swing Components in the Java tutorial.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文