Timer Listener类号错误

时间:2016-02-27 18:40:39

标签: java

我在我的计时器监听器类中遇到错误,我希望有人可以看看,并解释它有什么问题。我添加数字+ 1时收到错误。此外,停止按钮似乎无法正常工作。

 public class Main
{

/**
 * @param args the command line arguments
 */
public static void main(String[] args)
{
    JFrame frame = new JFrame("Odometer Program");
    frame.setLayout(new FlowLayout());
    // Creates Frame and a array of textfields
    JTextField[] tf1 = new JTextField[3];
    JPanel northPanel = new JPanel();

    for (int i = 0; i < tf1.length; i++) // adds texfields to frame
    {
        tf1[i] = new JTextField(1);
        northPanel.add(tf1[i]);
    }
    JButton DriveButton = new JButton(" Drive Car");
    JButton StopButton = new JButton("Stop Car");
    JPanel SouthPanel = new JPanel();
    SouthPanel.add(DriveButton);
    SouthPanel.add(StopButton);

    frame.add(northPanel, BorderLayout.NORTH);
    frame.add(SouthPanel, BorderLayout.SOUTH);
    frame.pack();  // sets default seize and width to the frame
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

    //  Create the timer
    ActionListener timerListener = new TimerListener(tf1);
    Timer timer = new Timer(100, timerListener);// 100 for tenth of a second
    timer.start();

    // create Action Listener for two buttons
    ActionListener buttonListener = new DrivecarStopcarListener(timer);
    DriveButton.addActionListener(buttonListener);
    StopButton.addActionListener(buttonListener);
    StopButton.setActionCommand("Stop Car");
    DriveButton.setActionCommand("Drive Car");
    }

}

class TimerListener implements ActionListener

{

private int[] numbers; // array of numbers
private JTextField[] textFields;

TimerListener(JTextField[] tf1)  // copy position into constructor
{
    // copy array to text fields
    textFields = tf1;

    // new int to create integers, the same size of text fields
    numbers = new int[textFields.length];

    for (int pos = 0; pos < numbers.length; pos++) //
    {
        textFields[pos].setText(String.valueOf(numbers[pos]));
        numbers + 1;
    }

}

@Override
public void actionPerformed(ActionEvent ae)
{
    for (int pos = 0; pos < numbers.length; pos++)
    {
        textFields[pos].setText(String.valueOf(numbers[pos]));
        numbers + 1;
    }

}
}

class DrivecarStopcarListener implements ActionListener
{

private Timer myTimer;

DrivecarStopcarListener(Timer t)
{
    myTimer = t;
}

@Override
public void actionPerformed(ActionEvent ae)
{
    String action = ae.getActionCommand();
    switch (action)
    {
        case "Drive car":
            myTimer.start();
            break;
        case "Stop car":
            myTimer.stop();
            break;
        }
}
}

1 个答案:

答案 0 :(得分:0)

在您的代码中:

          textFields[pos].setText(String.valueOf(numbers[pos]));
          numbers+1;

您将数字递增1,但不将其指定给任何内容。此外,数字是[],增量是非法的。我相信你打算在数组中增加一个元素的值。

此外,您从未在数字[]中设置任何值,因此所有元素都默认为0。 假设您尝试增加数字中每个元素的值,您首先需要设置数字:

          numbers[pos] = pos; //? not sure what value you want here
          textFields[pos].setText(String.valueOf(numbers[pos]));