我目前有兩個問題,在這兩個問題上我一直試圖解決過去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