gpt4 book ai didi

java - 为什么操作监听器会阻止我的程序连续递增/递减数字?

转载 作者:行者123 更新时间:2023-12-02 04:32:55 25 4
gpt4 key购买 nike

我正在尝试编写一个简单的计数器程序,该程序创建一个带有递增/递减按钮的窗口,用于对数字加/减一。我的问题是该程序只能在单击一次后才能运行。我试图让它在多次点击后继续运行。为什么 Action 监听器会阻止多次点击?

示例:

用户类型 5. 单击“+”按钮。结果显示6。
如果用户再次点击,什么也不会发生。

所需功能:

用户类型 5. 单击“+”按钮 5 次。结果应显示 10。

import javax.swing.JFrame;
import java.util.Scanner;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.KeyEvent;
import java.net.URL;
import javax.swing.AbstractButton;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;

public class CounterTestDemo4 extends JFrame
{
// Create Buttons and fields
private JButton jbtAdd = new JButton("+");
private JButton jbtSub = new JButton("-");
private JTextField jtfNumber1 = new JTextField(8);
private JTextField jtfResponse = new JTextField(8);

public static void main(String[] args)
{

JFrame frame = new CounterTestDemo4();
frame.setTitle("Counter Test");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}


public CounterTestDemo4()
{
//Create Panel
JPanel panel = new JPanel();
panel.add(new JLabel("Enter Number:"));
panel.add(jtfNumber1);
panel.add(new JLabel("Result:"));
panel.add(jtfResponse);
panel.add(jbtAdd);
panel.add(jbtSub);
add(panel);
BtnListener listener = new BtnListener();
jbtAdd.addActionListener(listener);
jbtSub.addActionListener(listener);
}

class JtfListener implements ActionListener {

@Override
public void actionPerformed(ActionEvent e) {
int x = Integer.parseInt(jtfNumber1.getText());
}
}

class BtnListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
int int1 = Integer.parseInt(jtfNumber1.getText());
if(e.getSource().equals(jbtAdd))
{
int1++;
jtfResponse.setText(String.valueOf(int1));
}

else if( e.getSource().equals(jbtSub) )
{
int1--;
jtfResponse.setText(String.valueOf(int1));
//end if
}//end else if statement
}//end action performed
}//end BtnListner
}//end CounterTestDemo4

最佳答案

您从一个 JTextField 获取数字并更改另一个 JTextField,但您永远不会获得更改后的数字。建议:不要从 ActionListener 中的任何 JTextField 获取数字。相反,创建一个 int 变量并将数字存储在该变量中。使用文本字段仅用于显示。唯一的异常(exception)是我想如果您想从 JTextField 设置数字,然后在该监听器内部设置 int 字段,但之后使用 int 字段。

然后摆脱 jtfResponse:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class CounterTestDemo4 extends JFrame {
private JButton jbtAdd = new JButton("+");
private JButton jbtSub = new JButton("-");
private JTextField jtfNumber1 = new JTextField(8);

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new CounterTestDemo4();
frame.setTitle("Counter Test");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
});

}

public CounterTestDemo4() {
JPanel panel = new JPanel();
panel.add(new JLabel("Enter Number:"));
panel.add(jtfNumber1);
panel.add(new JLabel("Result:"));
panel.add(jbtAdd);
panel.add(jbtSub);
add(panel);
BtnListener listener = new BtnListener();
jbtAdd.addActionListener(listener);
jbtSub.addActionListener(listener);
}

class BtnListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
int int1;
try {
int1 = Integer.parseInt(jtfNumber1.getText());
if (e.getSource().equals(jbtAdd)) {
int1++;
jtfNumber1.setText(String.valueOf(int1));
}

else if (e.getSource().equals(jbtSub)) {
int1--;
jtfNumber1.setText(String.valueOf(int1));
}
} catch (NumberFormatException e1) {
JOptionPane.showMessageDialog(jtfNumber1,
"Please enter a number in the text field",
"Text Field Without Number", JOptionPane.ERROR_MESSAGE);
jtfNumber1.selectAll();
jtfNumber1.requestFocusInWindow();
}
}
}
}

关于java - 为什么操作监听器会阻止我的程序连续递增/递减数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31223136/

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