如何在Swing的文本区域中设置语言?

huangapple go评论78阅读模式
英文:

How to set language in text area in Swing?

问题

我需要将应用中所有的文本区域的默认语言更改为希伯来语。我尝试使用“Set Local”但是没有生效。

有任何想法吗?

英文:

I need to change the default language into Hebrew in all the text areas in the app. I tried to use Set Local, but it didn't work.

Any idea?

答案1

得分: 2

以下代码演示了我所知道的配置JTextComponentJTextArea是其子类之一)的唯一方法,以便在获得输入焦点时显示希伯来字符。换句话说,在JTextComponent获得焦点之后,用户开始输入文本之前,无需显式更改语言。

import java.awt.BorderLayout;
import java.awt.ComponentOrientation;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.im.InputContext;
import java.util.Locale;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;

public class DfltHbrw implements ActionListener, FocusListener, Runnable {
    private static final Locale  HEBREW = new Locale("iw", "IL");
    private static final String  EXIT = "\u05E1\u05D2\u05D5\u05E8";

    private JFrame  frame;
    private JTextArea  textArea;

    @Override // java.awt.event.ActionEvent
    public void actionPerformed(ActionEvent event) {
        String actionCommand = event.getActionCommand();
        if (EXIT.equals(actionCommand)) {
            System.exit(0);
        }
    }

    @Override // java.awt.event.FocusListener
    public void focusGained(FocusEvent event) {
        InputContext ic = textArea.getInputContext();
        ic.selectInputMethod(HEBREW);
    }

    @Override // java.awt.event.FocusListener
    public void focusLost(FocusEvent event) {
        // Do nothing.
    }

    @Override // java.lang.Runnable
    public void run() {
        showGui();
    }

    private JPanel createButtons() {
        JPanel buttonsPanel = new JPanel();
        JButton button = new JButton(EXIT);
        button.addActionListener(this);
        buttonsPanel.add(button);
        return buttonsPanel;
    }

    private JScrollPane createTextArea() {
        textArea = new JTextArea(20, 60);
        textArea.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        textArea.addFocusListener(this);
        JScrollPane scrollPane = new JScrollPane(textArea);
        scrollPane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        return scrollPane;
    }

    private void showGui() {
        frame = new JFrame("Hebrew");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(createTextArea(), BorderLayout.CENTER);
        frame.add(createButtons(), BorderLayout.PAGE_END);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    /**
     * Start here.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new DfltHbrw());
    }
}

我只添加了JButton,以便有多个GUI组件,从而允许从一个组件转移焦点到另一个组件。

上述代码的相关部分是focusGained()方法。我为JTextArea添加了一个"焦点监听器",当JTextArea获得焦点时调用该方法。实际上,focusGained()方法在用户开始在其中输入文本时配置JTextArea以显示希伯来文本。

请注意,同样的focusGained()方法可用于配置JTextArea以显示任何[支持的]语言。您只需要更改传递给selectInputMethod()方法的Locale参数。

还要注意,我仅设置了ComponentOrientation,以使JTextArea中的希伯来文本看起来"自然"。为了配置JTextArea以显示希伯来文本,不需要这样做。

英文:

The below code demonstrates the only way I know of to configure a JTextComponent (which JTextArea is a subclass of) so that when it gains input focus it displays Hebrew characters. In other words no need for the user to explicitly change the language after the JTextComponent has gained focus and before the user starts entering text.

import java.awt.BorderLayout;
import java.awt.ComponentOrientation;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.im.InputContext;
import java.util.Locale;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;

public class DfltHbrw implements ActionListener, FocusListener, Runnable {
    private static final Locale  HEBREW = new Locale("iw", "IL");
    private static final String  EXIT = "\u05E1\u05D2\u05D5\u05E8";

    private JFrame  frame;
    private JTextArea  textArea;

    @Override // java.awt.event.ActionEvent
    public void actionPerformed(ActionEvent event) {
        String actionCommand = event.getActionCommand();
        if (EXIT.equals(actionCommand)) {
            System.exit(0);
        }
    }

    @Override // java.awt.event.FocusListener
    public void focusGained(FocusEvent event) {
        InputContext ic = textArea.getInputContext();
        ic.selectInputMethod(HEBREW);
    }

    @Override // java.awt.event.FocusListener
    public void focusLost(FocusEvent event) {
        // Do nothing.
    }

    @Override // java.lang.Runnable
    public void run() {
        showGui();
    }

    private JPanel createButtons() {
        JPanel buttonsPanel = new JPanel();
        JButton button = new JButton(EXIT);
        button.addActionListener(this);
        buttonsPanel.add(button);
        return buttonsPanel;
    }

    private JScrollPane createTextArea() {
        textArea = new JTextArea(20, 60);
        textArea.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        textArea.addFocusListener(this);
        JScrollPane scrollPane = new JScrollPane(textArea);
        scrollPane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        return scrollPane;
    }

    private void showGui() {
        frame = new JFrame("Hebrew");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(createTextArea(), BorderLayout.CENTER);
        frame.add(createButtons(), BorderLayout.PAGE_END);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    /**
     * Start here.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new DfltHbrw());
    }
}

I only added the JButton so that there would be more than one [GUI] component so as to allow transferring focus from one component to another.

The relevant part of the above code is the focusGained() method. I added a focus listener to the JTextArea which calls that method when the JTextArea gains focus. In fact the focusGained() method does the actual work of configuring the JTextArea for displaying Hebrew when the user starts to enter text into it.

Note that the same focusGained() method can be used to configure the JTextArea to display any [supported] language. All you need to change is the Locale argument that is passed to the selectInputMethod() method.

Also note that I only set the ComponentOrientation so that the Hebrew text in the JTextArea would look "natural". It is not required in order to configure the JTextArea for displaying Hebrew.

huangapple
  • 本文由 发表于 2020年8月20日 17:56:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63502613.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定