gpt4 book ai didi

java - 如果 JTextField 中有任何输入,KeyListener 不响应

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

我开发了一个简单的 MVC 计算器应用程序。我决定通过在 CalculatorView 中实现 KeyListener 来添加一些功能。但是这个 KeyListener 仅在 JTextField 中没有输入时(在通过按下 GUI 按钮进行任何输入之前)或在我按下“ESC”时响应。我知道这里有些人建议使用 KeyBindings 而不是 KeyListener,但是我的代码中需要有 12 个 KeyBindings(10 个用于数字,1 个用于 ESC,1 个用于“.”字符)。有什么方法可以让 KeyListener 在我的应用程序中正常工作吗?

代码如下:

/**
*
* @author Kate Nezdoly
*/
public class CalculatorView implements ActionListener, KeyListener {

private JButton[] operButtons = new JButton[13];
private JButton[] numberButtons = new JButton[12];
private String[] operators = {"C", "(", ")", "+", "-", "*", "/", "^", "cos", "sin",
"tan", "sqrt"};
private String[] numbers = {"1", "2", "3", "4", "5", "6", "7", "8",
"9", "0", ".", "="};

public CalculatorView() {
try {
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
} catch (UnsupportedLookAndFeelException | ClassNotFoundException | InstantiationException | IllegalAccessException e) {
System.err.println(e.getMessage());
}

createAndShowGUI();
}
private JTextField input;
private boolean decimal = true;

private JPanel createContentPane() {
JPanel totalGUI = new JPanel(new BorderLayout(12, 8));
input = new JTextField("0.0", 18);
input.addKeyListener(this);
input.setEditable(false);
input.setBackground(Color.white);
input.setHorizontalAlignment(JTextField.RIGHT);

JPanel action_buttons = new JPanel(new GridLayout(5, 2));

operButtons[0] = new JButton(operators[0]);
action_buttons.add(operButtons[0]);

for (int i = 1; i < operators.length; i++) {
operButtons[i] = new JButton(operators[i]);
operButtons[i].addActionListener(this);
operButtons[i].setActionCommand(operators[i]);
action_buttons.add(operButtons[i]);
}

JPanel number_buttons = new JPanel(new GridLayout(5, 2));

for (int i = 0; i < numbers.length - 1; i++) {
numberButtons[i] = new JButton(numbers[i]);
numberButtons[i].addActionListener(this);
numberButtons[i].setBackground(Color.lightGray);
numberButtons[i].setActionCommand(numbers[i]);
number_buttons.add(numberButtons[i]);
}

numberButtons[11] = new JButton(numbers[11]);
number_buttons.add(numberButtons[11]);

totalGUI.add(input, BorderLayout.PAGE_START);
totalGUI.add(number_buttons, BorderLayout.CENTER);
totalGUI.add(action_buttons, BorderLayout.LINE_END);

totalGUI.setOpaque(true);
return totalGUI;
}

private void createAndShowGUI() {
JFrame frame = new JFrame("Calculator");

frame.setContentPane(createContentPane());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
frame.setSize(300, 190);
frame.setResizable(false);
}

public void actionPerformed(ActionEvent ae) {
String temp = ae.getActionCommand();

if (input.getText().equals("0.0")) {
input.setText("");
}

if (temp.equals(".")) {
if (decimal) {
decimal = false;
input.setText(input.getText() + "" + temp);
}
}
else {
input.setText(input.getText() + "" + temp);
}
}

public void buttonActionListeners(ActionListener al) {
//add "=" action listener
numberButtons[11].setActionCommand(numbers[11]);
numberButtons[11].addActionListener(al);

//add "C" action listener
operButtons[0].setActionCommand(operators[0]);
operButtons[0].addActionListener(al);

}

// Gets the text from the Text Box and converts it into a Double.
public String getFieldText() {
return input.getText();
}

// Sets the text displayed on the Text Box.
public void setFieldText(String message) {
input.setText("" + message);
decimal = true;
}

@Override
public void keyTyped(KeyEvent e) {

}

@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 27) {
System.exit(0);
} else if (e.getKeyCode() >= 48 && e.getKeyCode() <= 57) {
if (input.getText().equals("0.0")) {
setFieldText(String.valueOf(e.getKeyChar()));
} else {
setFieldText(input.getText() + e.getKeyChar());

}
}
}

@Override
public void keyReleased(KeyEvent e) {

}


}

Controller 类:

public class CalculatorController implements  ActionListener {

private CalculatorView view;

public CalculatorController( CalculatorView view) {
this.view = view;
view.buttonActionListeners(this);
}

@Override
public void actionPerformed(ActionEvent e) {
String action = e.getActionCommand();
switch (action) {
case "=":
view.setFieldText(Parser.parse(view.getFieldText()));
break;
case "C":
view.setFieldText("0.0");
break;
}
}

public static void main(String args[]){
CalculatorView calc = new CalculatorView();
CalculatorController contr = new CalculatorController(calc);
}

}

最佳答案

我认为没有任何理由实现KeyListener/KeyBinding

1) 添加作为 ActionCommandJButtons 示例 here

private String[] numbers = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0", ".", "="};

2) 把JTextField改成JFormattedTextField,加上NumberInstance/Formatter,那么就只有数字和小数分隔符了,不再有通过实现 KeyListener/KeyBinding 或解析/测试 NumberInstance

从 KeyBoard 监听的原因

3) 如果有多个JFormattedTextField(值可以从剪贴板推算或粘贴)或者例如JFormattedTextFieldJTextArea(用于显示之前的计算)然后使用 DocumentListener 链接这些字段

关于java - 如果 JTextField 中有任何输入,KeyListener 不响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9062261/

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