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

时间:2015-04-23 10:15:49

标签: java swing look-and-feel

当我使用默认L& F时,会直接显示助记符。当我使用Windows L& F时,只有当我按下" Alt"时才会看到助记符。键。是否可以控制此功能(打开Windows L& F默认L& F的行为)?

以下是重现的代码(可能仅适用于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);

    }

}

这是默认L& F的图像(默认情况下可以看到助记符)

enter image description here

以下是Win L& F的图像(默认情况下,助记符不可见)

enter image description here

但是当我按下" Alt"键

enter image description here

1 个答案:

答案 0 :(得分:2)

不确定它是否适用于所有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();
    }
  }
}