带有 .equal 的 getActionCommand 不等于
getActionCommand with .equal not equaling properly
我正在尝试学习 Jtextfields 和动作侦听器。我正在学习本教程 https://www.geeksforgeeks.org/java-swing-jtextfield/,但我在使用此函数时遇到错误。
public void actionPerformed(ActionEvent e) {
String input = e.getActionCommand();
if (input.equals("submit")) {
displaysText.setText(textField.getText());
textField.setText("");
System.out.println("Entered if statement");
}
}
无论我在文本字段中输入什么,它都会输入 if 语句。当我输入“提交”时,它应该只输入那个 if 语句。这是一个错误还是我做错了什么?
如果有任何帮助,这里是所有代码
package IncludesMain;
import java.awt.event.*;
import javax.swing.*;
public class Main extends JFrame implements ActionListener {
static JTextField textField;
static JFrame frame;
static JButton button;
static JLabel displaysText;
Main() {}
public static void main(String[] args) {
frame = new JFrame("textfield");
displaysText = new JLabel("N/A");
button = new JButton("submit");
Main textObject = new Main();
button.addActionListener(textObject);
textField = new JTextField(16);
JPanel panel = new JPanel();
panel.add(textField);
panel.add(button);
panel.add(displaysText);
frame.add(panel);
frame.setSize(500, 400);
frame.show();
}
public void actionPerformed(ActionEvent e) {
String input = e.getActionCommand();
if (input.equals("submit")) {
displaysText.setText(textField.getText());
textField.setText("");
System.out.println("Entered if statement");
}
}
}
e.getActionCommand()
正在执行按钮的 actionCommand,因为您将侦听器放在那里。您需要在文本字段上设置侦听器
而不是
Main textObject = new Main();
button.addActionListener(textObject);
textField = new JTextField(16);
JPanel panel = new JPanel();
应该是
Example textObject = new Example();
textField = new JTextField(16);
textField.addActionListener(textObject);
JPanel panel = new JPanel();
此外,如果您这样做,则必须按 Enter 键提交值,除非您重构 actionPerformed 方法中的逻辑,否则该按钮将不起作用
我正在尝试学习 Jtextfields 和动作侦听器。我正在学习本教程 https://www.geeksforgeeks.org/java-swing-jtextfield/,但我在使用此函数时遇到错误。
public void actionPerformed(ActionEvent e) {
String input = e.getActionCommand();
if (input.equals("submit")) {
displaysText.setText(textField.getText());
textField.setText("");
System.out.println("Entered if statement");
}
}
无论我在文本字段中输入什么,它都会输入 if 语句。当我输入“提交”时,它应该只输入那个 if 语句。这是一个错误还是我做错了什么?
如果有任何帮助,这里是所有代码
package IncludesMain;
import java.awt.event.*;
import javax.swing.*;
public class Main extends JFrame implements ActionListener {
static JTextField textField;
static JFrame frame;
static JButton button;
static JLabel displaysText;
Main() {}
public static void main(String[] args) {
frame = new JFrame("textfield");
displaysText = new JLabel("N/A");
button = new JButton("submit");
Main textObject = new Main();
button.addActionListener(textObject);
textField = new JTextField(16);
JPanel panel = new JPanel();
panel.add(textField);
panel.add(button);
panel.add(displaysText);
frame.add(panel);
frame.setSize(500, 400);
frame.show();
}
public void actionPerformed(ActionEvent e) {
String input = e.getActionCommand();
if (input.equals("submit")) {
displaysText.setText(textField.getText());
textField.setText("");
System.out.println("Entered if statement");
}
}
}
e.getActionCommand()
正在执行按钮的 actionCommand,因为您将侦听器放在那里。您需要在文本字段上设置侦听器
而不是
Main textObject = new Main();
button.addActionListener(textObject);
textField = new JTextField(16);
JPanel panel = new JPanel();
应该是
Example textObject = new Example();
textField = new JTextField(16);
textField.addActionListener(textObject);
JPanel panel = new JPanel();
此外,如果您这样做,则必须按 Enter 键提交值,除非您重构 actionPerformed 方法中的逻辑,否则该按钮将不起作用