我在 Java 应用程序中使用从右到左的菜单。
我在所有JMenuBar
, JMenu
&JMenuItem
元素上使用了它来使它们从右到左:
jmenubar.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
jmenu1.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
jmenuitem1.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
现在的问题是JMenuItem
' 的文本正确对齐,但它的键盘加速器未对齐,如图所示(F2 和 F3 不在视野范围内,其他未对齐):
问题:如何修复从右到左JMenuItem
的加速器对齐?
对于 SSCCE:
此代码产生问题:
public class test{
public static void main(String[] args){
try{
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
javax.swing.JFrame frame = new javax.swing.JFrame("test");
frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
javax.swing.JMenuBar menubar = new javax.swing.JMenuBar();
javax.swing.JMenu jmenu1 = new javax.swing.JMenu("\u0645\u0646\u0648");
javax.swing.JMenuItem jmenuitem1 = new javax.swing.JMenuItem("\u0645\u0646\u0648 \u06F1");
javax.swing.JMenuItem jmenuitem2 = new javax.swing.JMenuItem("\u0645\u0646\u0648 \u06F2");
javax.swing.JMenuItem jmenuitem3 = new javax.swing.JMenuItem("\u0645\u0646\u0648 \u06F3");
javax.swing.JMenuItem jmenuitem4 = new javax.swing.JMenuItem("\u0645\u0646\u0648 \u06F4");
jmenuitem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F2, 0));
jmenuitem2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F3, 0));
jmenuitem3.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.CTRL_MASK|java.awt.event.InputEvent.ALT_MASK));
jmenuitem4.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_D, java.awt.event.InputEvent.CTRL_MASK));
menubar.applyComponentOrientation(java.awt.ComponentOrientation.RIGHT_TO_LEFT);
jmenu1.applyComponentOrientation(java.awt.ComponentOrientation.RIGHT_TO_LEFT);
jmenuitem1.applyComponentOrientation(java.awt.ComponentOrientation.RIGHT_TO_LEFT);
jmenuitem2.applyComponentOrientation(java.awt.ComponentOrientation.RIGHT_TO_LEFT);
jmenuitem3.applyComponentOrientation(java.awt.ComponentOrientation.RIGHT_TO_LEFT);
jmenuitem4.applyComponentOrientation(java.awt.ComponentOrientation.RIGHT_TO_LEFT);
jmenu1.setFont(new java.awt.Font("Times New Roman", 0, 15));
jmenuitem1.setFont(new java.awt.Font("Times New Roman", 0, 15));
jmenuitem2.setFont(new java.awt.Font("Times New Roman", 0, 15));
jmenuitem3.setFont(new java.awt.Font("Times New Roman", 0, 15));
jmenuitem4.setFont(new java.awt.Font("Times New Roman", 0, 15));
frame.setJMenuBar(menubar);
menubar.add(jmenu1);
jmenu1.add(jmenuitem1);
jmenu1.add(jmenuitem2);
jmenu1.add(jmenuitem3);
jmenu1.add(jmenuitem4);
frame.setVisible(true);
frame.pack();
frame.setSize(200,200);
}
});
}catch(java.lang.Exception e){
System.out.println(e.getMessage());
}
}
}