簡體   English   中英

如何在Swing中打開/關閉助記符的自動可視化?

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

當我使用默認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的圖像(默認情況下可以看到助記符)

在此輸入圖像描述

這是Win L&F的圖像(默認情況下,助記符是不可見的)

在此輸入圖像描述

但是當我按下“Alt”鍵時它們會顯示出來

在此輸入圖像描述

不確定它是否適用於所有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