Java 如何在JMenuBar中更改JMenus的方向
当我使用Java 如何在JMenuBar中更改JMenus的方向,java,swing,jmenu,jmenuitem,jmenubar,Java,Swing,Jmenu,Jmenuitem,Jmenubar,当我使用JMenuBar在java GUI中创建菜单时,它将所有JMenus从左到右放置如下: 我想将其改为从右向左,如下所示: 我希望在英语操作系统中执行此操作,因此我不希望看到阿拉伯语或从右到左解决方案的建议。您可以使用来更改JMenuBar的方向: import javax.swing.*; import java.awt.*; public class R_L_MenuBar_Demo { public static void main(String[] args){
JMenuBar
在java GUI中创建菜单时,它将所有JMenu
s从左到右放置如下:
我想将其改为从右向左,如下所示:
我希望在英语操作系统中执行此操作,因此我不希望看到阿拉伯语或从右到左解决方案的建议。您可以使用来更改JMenuBar
的方向:
import javax.swing.*;
import java.awt.*;
public class R_L_MenuBar_Demo
{
public static void main(String[] args){
SwingUtilities.invokeLater(() -> createAndShowGUI());
}
private static void createAndShowGUI()
{
JMenuBar mb = new JMenuBar();
JMenuItem item_1 = new JMenuItem("First Item");
JMenu menu_2 = new JMenu("Second Menu");
JMenuItem item_3 = new JMenuItem("First Item in Second");
menu_2.add(item_3);
mb.add(item_1);
mb.add(menu_2);
//switch the orientation of the menubar to right to left
JButton btn_r_to_l = new JButton("Switch menubar to r_to_l");
btn_r_to_l.addActionListener(e -> {
mb.invalidate();
mb.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
mb.validate();
});
//switch the orientation of the menubar to left to right
JButton btn_l_to_r = new JButton("Switch menubar to l_to_r");
btn_l_to_r.addActionListener(e -> {
mb.invalidate();
mb.applyComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
mb.validate();
});
JFrame frame = new JFrame("R_L_MenuBar");
frame.setLayout(new FlowLayout());
frame.add(btn_r_to_l);
frame.add(btn_l_to_r);
frame.setJMenuBar(mb);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200 , 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
这将如下所示:默认外观(从左到右)
从右向左切换后:
您可以使用更改
JMenuBar的方向
:
import javax.swing.*;
import java.awt.*;
public class R_L_MenuBar_Demo
{
public static void main(String[] args){
SwingUtilities.invokeLater(() -> createAndShowGUI());
}
private static void createAndShowGUI()
{
JMenuBar mb = new JMenuBar();
JMenuItem item_1 = new JMenuItem("First Item");
JMenu menu_2 = new JMenu("Second Menu");
JMenuItem item_3 = new JMenuItem("First Item in Second");
menu_2.add(item_3);
mb.add(item_1);
mb.add(menu_2);
//switch the orientation of the menubar to right to left
JButton btn_r_to_l = new JButton("Switch menubar to r_to_l");
btn_r_to_l.addActionListener(e -> {
mb.invalidate();
mb.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
mb.validate();
});
//switch the orientation of the menubar to left to right
JButton btn_l_to_r = new JButton("Switch menubar to l_to_r");
btn_l_to_r.addActionListener(e -> {
mb.invalidate();
mb.applyComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
mb.validate();
});
JFrame frame = new JFrame("R_L_MenuBar");
frame.setLayout(new FlowLayout());
frame.add(btn_r_to_l);
frame.add(btn_l_to_r);
frame.setJMenuBar(mb);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200 , 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
这将如下所示:默认外观(从左到右)
从右向左切换后: