Action Listener为什么停止我的程序不断递增/递减数字?

时间:2015-07-04 17:01:55

标签: java swing jframe actionlistener

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

示例:

用户类型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

1 个答案:

答案 0 :(得分:2)

您从一个JTextField获取数字并更改另一个,但您永远不会获得更改的数字。建议:不要从ActionListener中的任何JTextField获取数字。相反,创建一个int变量并将数字存储在变量中。使用文本字段仅显示。唯一的例外是我想如果你想从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();
         }
      }
   }
}