2016-07-05 90 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); 
    } 
}