java - 在 Swing 应用程序中保留键盘布局?

标签 java swing keyboard-layout

我有一个 Java Swing 应用程序,它生成带有文本控件的子对话框。问题是,当您在子对话框中更改键盘布局时,它会在对话框关闭后立即变回。

我需要的是切换后保留的keboard布局,无论是在主框架还是在子框架中切换。

这是一个说明问题的 SSCCE:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class InheritInputContext {

    public static void main(String[] arg) {
        final MainFrame mainFrame = new MainFrame();
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                mainFrame.setPreferredSize(new Dimension(300, 400));
                mainFrame.pack();
                mainFrame.setLocationRelativeTo(null);
                mainFrame.setVisible(true);
            }
        });

    }
}


class MainFrame extends JFrame {

    MainFrame() {
        setLayout(new BorderLayout());
        JTextArea textArea = new JTextArea();
        add(textArea, BorderLayout.CENTER);

        JButton dialogBtn = new JButton("Dialog");
        add(dialogBtn, BorderLayout.SOUTH);
        dialogBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ChildDialog cd = new ChildDialog(MainFrame.this);
                cd.setPreferredSize(new Dimension(200, 200));
                cd.setLocationRelativeTo(MainFrame.this);
                cd.pack();
                cd.setVisible(true);
            }
        });
    }
}


class ChildDialog extends JDialog {

    ChildDialog(Window w) {
        super(w);
        JTextArea textArea = new JTextArea();
        getContentPane().add(textArea);
    }
}

最佳答案

好的,我刚刚解决了这个问题:

在 main() 方法中添加了一个监听器到 java 工具包,如下所示:

AWTEventListener awtWindowListener = new AWTEventListener() {
    @Override
    public void eventDispatched(AWTEvent event) {
        if (event instanceof WindowEvent) {
            if (WindowEvent.WINDOW_CLOSED == event.getID()
                    || WindowEvent.WINDOW_CLOSING == event.getID()) {
                Window child = ((WindowEvent) event).getWindow();
                Window parent = SwingUtilities.getWindowAncestor(child);
                if (parent == null) return;
                InputContext childIC = child.getInputContext();
                parent.getInputContext().selectInputMethod(childIC.getLocale());
            }
        }

    }
};

Toolkit.getDefaultToolkit().addAWTEventListener(awtWindowListener, AWTEvent.WINDOW_EVENT_MASK);

它适用于以父窗口作为构造函数参数生成的所有子对话框。在关闭事件中,来自子对话框 InputContext 的 Locale 被放入其父窗口的 InputContext 中。

也许有更好的方法。

关于java - 在 Swing 应用程序中保留键盘布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9667947/

相关文章:

delphi - 在 VCL 中的 DbGrids.pas 中禁用自动更改键盘布局

html - 是否有 HTML 属性告诉智能手机键盘显示特殊的电子邮件键?

Java - 映射键查找忽略大小写

Java:数据结构内存估计

Java 事件传播停止

java - 显示 JTabbedPane 的第一个选项卡

java - Dozer Java API 如何执行?

java - Intellij IDEA 项目 View 不像以前那样显示结构。

java - 无法在 public() 中访问 JPanel

Javascript 统一码 : same letters but different unicode