JTextfield在Mac上无法对焦/可编辑

时间:2017-12-04 17:47:37

标签: java macos swing lwjgl jtextfield

我们正在使用lwjgl3在OpenGL中创建一个项目。我们的应用程序有一个“添加多维数据集”按钮,它打开一个包含4个JTextFields和2个JButton的新JFrame,这个JFrame的布局是使用GroupLayout创建的。我在Windows / Linux上运行我们项目的朋友可以毫无问题地选择和编辑这些JTextFields,但是我使用MacOS High Sierra却不能。

当我打开JFrame时,没有选择任何文本字段,它们会记录点击次数(当点击鼠标时,textField.setText会起作用),但它们永远不会抓住焦点,尽管我付出了最大努力,闪烁的光束永远不会出现(它是已经好几周了)。当我尝试在字段中键入内容(忽略没有| -beam)时,文本将出现在我当前的项目文件中(我的光标最后一次是在运行项目之前)。我添加了其他使用JTextField的类,当我运行他们的main方法时,它们工作正常但是当我在项目的某个地方实例化这些类时,框架确实打开了但文本字段再次无法选择。

这里我提供了CubeAdderPanel类,我已经将它修剪了一下。

根据回复更新

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;

import sourcecode.visualization.objects.Cube;

import java.util.HashMap;
import java.util.Map;

public class CubeAdderPanel extends JFrame {

private JTextField textFieldX;
private JTextField textFieldY;
private JTextField textFieldZ;
private JTextField textFieldSize;

private SolidButton addButton;
private SolidButton backButton;

private JLabel lblX;
private JLabel lblY;
private JLabel lblZ;
private JLabel lblSize;

private Font font = new Font("Open Sans Light", Font.PLAIN, 13);

private Map<JTextField, Double> textInput = new HashMap<>();

private final MenuHandler handler;

public CubeAdderPanel(MenuHandler handler){

    this.handler = handler;

    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    setAlwaysOnTop(true);
    setType(Type.UTILITY);
    setResizable(false);
    setUndecorated(true);
    setBounds(1370, 130, 280, 210);

    String osName = System.getProperty("os.name");
    if (osName.contains("Mac")) {
        setBounds(1130, 40, 280, 240);
    }

    initTextFields();
    initButtons();
    initLabels();

    //initFocusTraversal();
    initTextInputMap();
    initTextActions();

    //Creates JFrame layout
    JSeparator separator = new JSeparator();
    GroupLayout gl_panel = new GroupLayout(getContentPane());
    gl_panel.setHorizontalGroup(
            gl_panel.createParallelGroup(Alignment.LEADING)
                    .addGroup(Alignment.TRAILING, gl_panel.createSequentialGroup()
                            .addGap(47)
                            .addGroup(gl_panel.createParallelGroup(Alignment.TRAILING)
                                    .addGroup(gl_panel.createSequentialGroup()
                                            .addComponent(addButton, GroupLayout.PREFERRED_SIZE, 76, GroupLayout.PREFERRED_SIZE)
                                            .addPreferredGap(ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
                                            .addComponent(backButton, GroupLayout.PREFERRED_SIZE, 76, GroupLayout.PREFERRED_SIZE))
                                    .addGroup(gl_panel.createSequentialGroup()
                                            .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
                                                    .addComponent(lblX)
                                                    .addComponent(lblY, GroupLayout.PREFERRED_SIZE, 8, GroupLayout.PREFERRED_SIZE)
                                                    .addComponent(lblZ)
                                                    .addComponent(lblSize))
                                            .addPreferredGap(ComponentPlacement.RELATED, 26, Short.MAX_VALUE)
                                            .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
                                                    .addComponent(textFieldSize, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                                    .addComponent(textFieldX, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                                    .addComponent(textFieldY, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                                    .addComponent(textFieldZ, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))))
                            .addGap(42))
                    .addGroup(gl_panel.createSequentialGroup()
                            .addComponent(separator, GroupLayout.PREFERRED_SIZE, 279, GroupLayout.PREFERRED_SIZE)
                            .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
    gl_panel.setVerticalGroup(
            gl_panel.createParallelGroup(Alignment.LEADING)
                    .addGroup(gl_panel.createSequentialGroup()
                            .addGap(35)
                            .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
                                    .addComponent(lblX)
                                    .addComponent(textFieldX, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(ComponentPlacement.RELATED)
                            .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
                                    .addComponent(textFieldY, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                    .addComponent(lblY))
                            .addPreferredGap(ComponentPlacement.RELATED)
                            .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
                                    .addComponent(textFieldZ, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                    .addComponent(lblZ))
                            .addPreferredGap(ComponentPlacement.RELATED)
                            .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
                                    .addComponent(lblSize)
                                    .addComponent(textFieldSize, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                            .addGap(18)
                            .addComponent(separator, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(ComponentPlacement.RELATED)
                            .addGap(15)
                            .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
                                    .addComponent(backButton, GroupLayout.PREFERRED_SIZE, 27, GroupLayout.PREFERRED_SIZE)
                                    .addComponent(addButton, GroupLayout.PREFERRED_SIZE, 27, GroupLayout.PREFERRED_SIZE))
                            .addContainerGap(46, Short.MAX_VALUE))
    );
    getContentPane().setLayout(gl_panel);
}

private void initTextInputMap() {
    double empty = Double.POSITIVE_INFINITY;
    textInput.put(textFieldX, empty);
    textInput.put(textFieldY, empty);
    textInput.put(textFieldZ, empty);
    textInput.put(textFieldSize, empty);
}

private void saveTextInput(JTextField textField) {
    try {
        textInput.put(textField, Double.parseDouble(textField.getText()));
        getFocusOwner().transferFocus();
    } catch (NumberFormatException e) {
        Toolkit.getDefaultToolkit().beep();
        textField.setText("");
        textInput.put(textField, Double.POSITIVE_INFINITY);
    } finally {
        boolean validInputs = hasValidInputs();
        if (! hasValidInput(textField)) {
            Toolkit.getDefaultToolkit().beep();
        }
        addButton.setEnabled(validInputs);
    }
}

private void saveTextInputs() {
    for (JTextField textField : textInput.keySet()) {
        saveTextInput(textField);
    }
}

private void initTextActions() {
    for (JTextField textField : textInput.keySet()) {
        textField.addActionListener((ActionEvent a) -> {
                    saveTextInput(textField);
        });
    }
}

private void initFocusTraversal() {
    getContentPane().setFocusTraversalPolicy(
            new SimpleFocusTraversalPolicy(
                    textFieldX, textFieldY, textFieldZ,
                            textFieldSize));
}

private void initTextFields() {
    textFieldX = new JTextField();
    textFieldX.setColumns(10);
    textFieldX.setName("X");

    textFieldY = new JTextField();
    textFieldY.setColumns(10);
    textFieldY.setName("Y");

    textFieldZ = new JTextField();
    textFieldZ.setColumns(10);
    textFieldZ.setName("Z");

    textFieldSize = new JTextField();
    textFieldSize.setColumns(10);
    textFieldSize.setName("Size");
}

private void initButtons() {
    addButton = new SolidButton("add");
    addButton.addActionListener((ActionEvent a) -> {
        saveTextInputs();
        if (!hasValidInputs()) {
            Toolkit.getDefaultToolkit().beep();
            cleanInvalidInputs();
        } else {
            float x = textInput.get(textFieldX).floatValue();
            float y = textInput.get(textFieldY).floatValue();
            float z = textInput.get(textFieldZ).floatValue();
            float size = textInput.get(textFieldSize).floatValue();
            Cube cube = new Cube(x, y, z, size, 360, 1);
            handler.addCube(cube);
            resetInputs();
        }
    });

    addButton.setFont(font);

    backButton = new SolidButton("back");
    backButton.addActionListener((ActionEvent a) -> {
        setVisible(false);
        handler.restoreMenu();
    });
    backButton.setFont(font);
}

private void initLabels() {...}

private int counter = 1;

@Override
public void setVisible(boolean state) {
    super.setVisible(state);
    addButton.setEnabled(!state);
    if (state) {
        if (counter == 1) {
            counter = 0;
            super.setVisible(true);
        }
        textFieldX.requestFocus();
    }
}

public void terminate() {
    dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
}

private boolean hasValidInputs() {...}

private void cleanInvalidInputs() {...}

private boolean hasValidInput(JTextField textField) {...}

public void resetInputs() {...}

谢谢!!如果需要任何其他信息,我很乐意提供!

编辑:

当我运行JTextField.main()时,这个简单的TextField类提供了一个完美的可编辑/可聚焦的JTextField。

package sourcecode.visualization.GUI;

import javax.swing.*;

public class TextField {

JFrame frame = new JFrame("Jtextfield demo");
JTextField textField = new JTextField("Hello from JTextField", 10);

public TextField() {
    frame.add(textField);
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            new TextField();
        }
    });
}

然而,当我从项目内的任何地方调用TextField时,尽管我尝试了所有与焦点相关的函数等,但在运行Main.main()时它永远不可编辑/可聚焦。在这里,我向我们的Pilot类添加了一个TextField对象。

....
import sourcecode.visualization.GUI.TextField;

public class Pilot implements ILogic {

  TextField textField = new TextField();
....}

所以这让我相信问题来自于另一个类,而不是CubeAdderPanel类,干扰了JTextFields / swing,但我不知道哪一个可能导致这个问题,我们已经查看了所有这些,因为我们所有的代码似乎都正确,因为它在Windows和Linux上运行良好。它可能是Shader,Mesh,Window,Renderer ......类吗?什么可能导致这个问题?

0 个答案:

没有答案