gpt4 book ai didi

java - 如何将 Netbeans Java 应用程序对象更改为 "right to left"

转载 作者:行者123 更新时间:2023-11-30 11:10:42 25 4
gpt4 key购买 nike


我需要在框架上从“右到左”显示对象吗?

enter image description here

C# 中的相同程序
这就是我想要的对象的样子

enter image description here

请帮助我:)
谢谢

最佳答案

尝试通过为每个要在其中使用阿拉伯语的组件调用 setComponentOrientation() 方法来更改组件方向,对于组合框,您应该覆盖其单元格渲染器以支持从右到左显示,请尝试以下操作代码:

public class Test extends JFrame
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable(){
public void run()
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e)
{
e.printStackTrace();
}
new Test();
}
});
}

public Test()
{
setBounds(100,100,380,400);
setTitle("بوابة العالم");
getContentPane().setLayout(new FlowLayout(FlowLayout.RIGHT));
JComboBox<String> comboBox = new JComboBox<String>(new String[]{"التاريخ","الفلسفة","الفلك"});
comboBox.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
comboBox.setPreferredSize(new Dimension(300,comboBox.getPreferredSize().height));
comboBox.setRenderer(new CellRenderer());
add(comboBox);

JTextPane textPane = new JTextPane();
textPane.setPreferredSize(new Dimension(300, 300));
textPane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
JScrollPane scroll = new JScrollPane(textPane);
scroll.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
add(scroll);

setVisible(true);
}

}

class CellRenderer implements ListCellRenderer<String>
{
@Override
public Component getListCellRendererComponent(JList<? extends String> list, String value, int index, boolean isSelected, boolean cellHasFocus)
{
JLabel label = new JLabel(value);
label.setOpaque(true);
label.setFocusable(true);
label.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
if (isSelected) {
label.setBackground(list.getSelectionBackground());
label.setForeground(list.getSelectionForeground());
} else {
label.setBackground(list.getBackground());
label.setForeground(list.getForeground());
}
return label;
}
}

结果:

enter image description here

对于标题栏方向,如果您使用标题栏的主机外观,它对您不起作用,但如果您调用 JFrame.setDefaultLookAndFeelDecorated(true) 则它会起作用,然后您的应用程序将使用提供的外观而不是主机外观,接下来您应该在框架上调用 setComponentOrientation() 方法来指定所需的方向,尝试使用此代码激活标题栏的从右到左:

public class Test extends JFrame
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable(){
public void run()
{
JFrame.setDefaultLookAndFeelDecorated(true);
new Test();
}
});
}

public Test()
{
setBounds(100,100,380,400);
setTitle("بوابة العالم");
setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
getContentPane().setLayout(new FlowLayout(FlowLayout.RIGHT));
JComboBox<String> comboBox = new JComboBox<String>(new String[]{"التاريخ","الفلسفة","الفلك"});
comboBox.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
comboBox.setPreferredSize(new Dimension(300,comboBox.getPreferredSize().height));
comboBox.setRenderer(new CellRenderer());
add(comboBox);

JTextPane textPane = new JTextPane();
textPane.setPreferredSize(new Dimension(300, 300));
textPane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
JScrollPane scroll = new JScrollPane(textPane);
scroll.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
add(scroll);

setVisible(true);
}

}

class CellRenderer implements ListCellRenderer<String>
{
@Override
public Component getListCellRendererComponent(JList<? extends String> list, String value, int index, boolean isSelected, boolean cellHasFocus)
{
JLabel label = new JLabel(value);
label.setOpaque(true);
label.setFocusable(true);
label.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
if (isSelected) {
label.setBackground(list.getSelectionBackground());
label.setForeground(list.getSelectionForeground());
} else {
label.setBackground(list.getBackground());
label.setForeground(list.getForeground());
}
return label;
}
}

结果:

enter image description here

注意:

  • 不要忘记使用 UTF-8 编码保存源文件以支持阿拉伯字符。
  • JFrame.setDefaultLookAndFeelDecorated(true) 必须在创建任何框架实例之前调用。

关于java - 如何将 Netbeans Java 应用程序对象更改为 "right to left",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27627873/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com