简体   繁体   English

如何在Swing中打开/关闭助记符的自动可视化?

[英]How can I turn on/off automatic visualization of mnemonics in Swing?

When I use default L&F the mnemonics are shown directly. 当我使用默认L&F时,会直接显示助记符。 When I use Windows L&F the mnemonics go visible only if I press "Alt" key. 当我使用Windows L&F时,只有按“Alt”键才能看到助记符。 Is it possible to control this feature (turn on the behaviour of Windows L&F for default L&F)? 是否可以控制此功能(打开Windows L&F默认L&F的行为)?

Here is the code to reproduce (probably works only for Windows) 这是重现的代码(可能仅适用于Windows)

import java.util.Locale;

import javax.swing.JOptionPane;
import javax.swing.UIManager;

public class OptionPaneTest {

    public static void main(String[] args) throws Exception {
        Locale.setDefault(Locale.ENGLISH);
        JOptionPane.showConfirmDialog(null, "Test Message", "Test title", JOptionPane.YES_NO_CANCEL_OPTION);
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); // don't know whether it works for Mac
        JOptionPane.showConfirmDialog(null, "Test Message", "Test title", JOptionPane.YES_NO_CANCEL_OPTION);

    }

}

Here ist the image for default L&F (mnemonics are visible by default) 这里是默认L&F的图像(默认情况下可以看到助记符)

在此输入图像描述

Here is the image for Win L&F (mnemonics are invisible by default) 这是Win L&F的图像(默认情况下,助记符是不可见的)

在此输入图像描述

But they go visible when I press "Alt" key 但是当我按下“Alt”键时它们会显示出来

在此输入图像描述

Not sure if it works on all Windows but you could try UIManager.put("Button.showMnemonics", true); 不确定它是否适用于所有Windows但您可以尝试UIManager.put("Button.showMnemonics", true); :

import java.awt.EventQueue;
import java.util.Locale;
import javax.swing.JOptionPane;
import javax.swing.UIManager;

public class OptionPaneTest2 {
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      @Override public void run() {
        createAndShowGUI();
      }
    });
  }
  public static void createAndShowGUI() {
    try {
      Locale.setDefault(Locale.ENGLISH);
      JOptionPane.showConfirmDialog(
          null, "Message", "title", JOptionPane.YES_NO_CANCEL_OPTION);

      UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
      JOptionPane.showConfirmDialog(
          null, "Message", "title", JOptionPane.YES_NO_CANCEL_OPTION);

      UIManager.put("Button.showMnemonics", true);
      JOptionPane.showConfirmDialog(
          null, "Message", "title", JOptionPane.YES_NO_CANCEL_OPTION);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM