我目前有两个问题,在这两个问题上我一直试图解决过去3个小时。ActionListener和IF语句的问题
我不能让
input--;
递减if input is not == to 0
我不能让
JTextField input
更新我分配给它,一旦程序运行值。生病类型22在运行的程序中点击开始,它会转到“test99”。 图片是我如何进入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();
}
});
}
}
你可能包括问题的照片吗? – Queue
@Queue这是否给你一个更好的主意? – Jakob991
行:'字符串t = Input.getText();'和'Input.setText(t);'没有任何意义。你想在这里做什么? – Hackerdarshi