2016-07-05 42 views
1

我目前有两个问题,在这两个问题上我一直试图解决过去3个小时。ActionListener和IF语句的问题

  1. 我不能让input--;递减if input is not == to 0

  2. 我不能让JTextField input更新我分配给它,一旦程序运行值。生病类型22在运行的程序中点击开始,它会转到“test99”。 enter image description here图片是我如何进入66然后我按下开始,test99值的例子上来,而不是test66

我希望我能解释我的问题,你将能够理解的程度。我已阅读了很多关于动作监听器的文档,但目前这个想法不会为我点击。任何建设性的批评都是受欢迎的。

我也简化了我的问题尽我所能。

package test; 

import java.awt.EventQueue; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import java.awt.BorderLayout; 
import javax.swing.SwingConstants; 
import javax.swing.Timer; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JTextField; 
import javax.swing.JButton; 

public class test { 

private JFrame frame; 
private JButton btnStart; 

/** 
* 
* Launch the application. 
*/ 
public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       test window = new test(); 
       window.frame.setVisible(true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

/** 
* Create the application. 
*/ 
public test() { 
    initialize(); 
} 

/** 
* Initialize the contents of the frame. 
*/ 

private void initialize() { 
    frame = new JFrame(); 
    JLabel Output = new JLabel("Time left: "); 
    Output.setHorizontalAlignment(SwingConstants.CENTER); 
    frame.getContentPane().add(Output, BorderLayout.CENTER); 
    JTextField Input = new JTextField(); 
    btnStart = new JButton("start"); 

    Input.setText("99"); 
    int input = (int) (Double.parseDouble(Input.getText())); 

    Input.setHorizontalAlignment(SwingConstants.CENTER); 
    frame.getContentPane().add(Input, BorderLayout.SOUTH); 
    Input.setColumns(10); 
    frame.getContentPane().add(btnStart, BorderLayout.NORTH); 
    frame.setBounds(100, 100, 300, 300); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    Input.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 

      } 
     }); 


    Timer t = new Timer(1000, new ActionListener() { 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      Output.setText("test" + input); 

      if (input == 0) { 
       ((Timer) e.getSource()).stop(); 
      } 
      input--; 
     } 

    }); 

    btnStart.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      t.start(); 
     } 
    }); 

} 

}

+0

你可能包括问题的照片吗? – Queue

+0

@Queue这是否给你一个更好的主意? – Jakob991

+0

行:'字符串t = Input.getText();'和'Input.setText(t);'没有任何意义。你想在这里做什么? – Hackerdarshi

回答

1

我要推荐你的声明输入变量不是在你的功能,但在你的类。否则,你会遇到范围问题。例如:

public class test { 

    private JFrame frame; 
    private JButton btnStart; 
    private int input; 
    private JTextField Input; 

    //... 

} 

应该可以解决这个问题:)

我不enterily肯定第二个问题,但如果你想从输入值倒计数,你必须更新你的动作监听:

btnStart.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 
     input = (int) (Double.parseDouble(Input.getText())); 
     t.start(); 
    } 
}); 
+0

解决了递减问题非常感谢您对JTextField Input的动作侦听器的任何想法? – Jakob991

+0

@ Jakob991我不确定我是否正确地低估了你,但我更新了我的帖子 –

+0

你理解得非常好,谢谢你把时间放在一边帮我! – Jakob991

0

有几个批评我对你的代码:

  1. 变量的名字应该与开始小写字母和类名应以大写字母开头。
  2. int input = (int) (Double.parseDouble(Input.getText()));为什么要使用Double.parseDouble,然后强制转换为int当您只能使用Integer.parseInt()
  3. 因为您将输入JTextField的文本设置为99,所以在您的GUI中出现“test99”而不是“test66”,然后解析该值一次然后在您的计时器中使用它。你永不更新这个值,所以它是总是 99.而不是这个,你应该每次用户按下“开始”并更新您的计时器使用的值int解析输入JTextField中的文本。

见下面我纠正解决方案:

import javax.swing.*; 
import java.awt.*; 
import java.util.Timer; 
import java.util.TimerTask; 

public class Test { 

    private Test() { 
     initialize(); 
    } 

    public static void main(String[] args) { 
     EventQueue.invokeLater(Test::new); 
    } 

    private void initialize() { 

     JFrame frame = new JFrame(); 
     frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
     frame.setLocationRelativeTo(null); 

     JPanel timePanel = new JPanel(new FlowLayout()); 

     timePanel.add(new JLabel("Time left: ")); 

     JLabel timeOutput = new JLabel(""); 
     timePanel.add(timeOutput); 

     frame.add(timePanel, BorderLayout.CENTER); 

     JPanel inputPanel = new JPanel(new FlowLayout()); 

     JButton startButton = new JButton("Start"); 

     JButton stopButton = new JButton("Stop"); 
     stopButton.setEnabled(false); 

     JTextField timeField = new JTextField(5); 

     inputPanel.add(startButton); 
     inputPanel.add(stopButton); 
     inputPanel.add(timeField); 

     frame.add(inputPanel, BorderLayout.SOUTH); 

     Timer timer = new Timer(); 

     startButton.addActionListener(e -> { 

      startButton.setEnabled(false); 
      stopButton.setEnabled(true); 

      timer.scheduleAtFixedRate(new TimerTask() { 

       int time = Integer.parseInt(timeField.getText()); 

       @Override 
       public void run() { 
        if(time < 0) timer.cancel(); 
        timeOutput.setText(String.valueOf(time--)); 
       } 
      }, 0, 1000); 
     }); 

     stopButton.addActionListener(e -> { 
      timer.cancel(); 
      stopButton.setEnabled(false); 
      startButton.setEnabled(true); 
      timeOutput.setText(""); 
     }); 

     frame.pack(); 
     frame.setVisible(true); 
    } 
}