Localized accelerators (JMenuItem hotkeys) in Swing

前端 未结 1 559
挽巷
挽巷 2021-01-15 18:04

I am working in an English app on a german laptop, over a spanish OS.

Even if I explictly set Locale.setDefault(Locale.ENGLISH) at the beggining of my app, I am seei

相关标签:
1条回答
  • 2021-01-15 18:27

    You have to make sure that you set the locale before any toolkit code is executed. The following code shows the effect: if you move the Locale.setDefault(Locale.GERMAN); to any other line it will show the default accelerator names again.

    Instead of setting the locale inside your code you may also append the following argument to the VM:

    -Duser.language=DE
    

    locale menu

    public class MenuLocale {
    
        public static void main(String[] args) {
            Locale.setDefault(Locale.GERMAN);
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    JFrame f = new JFrame();
                    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    
                    JMenuBar menubar = new JMenuBar();
                    JMenu menu = new JMenu("Menu");
                    JMenuItem menuitem = new JMenuItem("Menuitem");    
                    menuitem.setAccelerator(KeyStroke.getKeyStroke('X', KeyEvent.CTRL_MASK | KeyEvent.SHIFT_MASK));
    
                    f.setJMenuBar(menubar);
                    menubar.add(menu);
                    menu.add(menuitem);
    
                    f.pack();
                    f.setVisible(true);
                }
            });
        }
    }
    
    0 讨论(0)
提交回复
热议问题