2012-02-14 31 views
-1

我想附加一個2秒(2000毫秒)計時器到這個燈光,以便它自動前進到下一個顏色。我一直在閱讀Java文檔,我不明白如何使用它。如何爲此附加定時器?

import java.awt.event.*; 
import javax.swing.*; 
import java.awt.*; 



public class Lab4Frame extends JFrame { 

    Lab4Frame(){ 
     this.setLayout(new BorderLayout()); 
     setTitle("Lab 4 - Application #1"); 
     Lab4Panel p = new Lab4Panel(); 
     Lab4RadioButtonPanel p2 = new Lab4RadioButtonPanel(p); 

     add(p, BorderLayout.CENTER); 
     add(p2, BorderLayout.SOUTH); 
    } 

    public static void main(String[] args){ 

      Lab4Frame frame = new Lab4Frame(); 
      frame.setTitle("Lab4 Application # 1"); 
      frame.setLocationRelativeTo(null); 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      frame.setSize(600, 600); 
      frame.setVisible(true); 
    } 

} 

class Lab4RadioButtonPanel extends JPanel { 
    Lab4Panel canvas; 
    //Lab4RadioButtonPanel canvas2 = new Lab4RadioButtonPanel(); 

    public Lab4RadioButtonPanel(Lab4Panel canvas) { 
    this.canvas = canvas; 
     boolean red, green, yellow; 
     this.setLayout(new FlowLayout()); 
     JRadioButton jrbRed = new JRadioButton("Red", true); 
     JRadioButton jrbYellow = new JRadioButton("Yellow"); 
     JRadioButton jrbGreen = new JRadioButton("Green"); 

     this.setBorder(BorderFactory.createLineBorder(Color.black)); 

     ButtonGroup group = new ButtonGroup(); 
     group.add(jrbRed); 
     group.add(jrbYellow); 
     group.add(jrbGreen); 


     this.add(jrbRed); 
     this.add(jrbYellow); 
     this.add(jrbGreen); 

     jrbRed.addActionListener(new RedListener(canvas)); 
     jrbYellow.addActionListener(new YellowListener(canvas)); 
     jrbGreen.addActionListener(new GreenListener(canvas)); 

    } 
} 


class Lab4Panel extends JPanel{ 


    public Lab4Panel(){} 



    boolean red = true; 
    boolean green = false; 
    boolean yellow = false; 
    int radius = 5; 
    int x = -1; 
    int y = -1; 

    public void setRed(){ 
     yellow = false; 
     green = false; 
     red = true; 
    } 

    public void setYellow(){ 
     red = false; 
     green = false; 
     yellow = true; 
    } 

    public void setGreen(){ 
     red = false; 
     yellow = false; 
     green = true; 
    } 

    protected void paintComponent(Graphics g){ 
     if (x<0 || y<0) { 
      x = getWidth()/2 - radius; 
      y = getHeight()/2 - radius; 
     } 
     super.paintComponent(g); 
     g.drawRect(x - 10,y - 90, 40, 120); 
     g.drawRect(x - 5,y - 90, 40, 120); 
     g.setColor(Color.RED); 
     g.drawOval(x,y - 80, 4 * radius, 4 * radius); 
     g.setColor(Color.YELLOW); 
     g.drawOval(x,y - 40, 4 * radius, 4 * radius); 
     g.setColor(Color.GREEN); 
     g.drawOval(x,y, 4 * radius, 4 * radius); 


     if(red){ 
      g.setColor(Color.RED); 
      g.fillOval(x,y - 80, 4 * radius, 4 * radius); 
      repaint(); 
     } 

     else if (yellow){ 
      g.setColor(Color.YELLOW); 
      g.fillOval(x,y - 40, 4 * radius, 4 * radius); 
      repaint(); 
     } 

     if(green){ 
      g.setColor(Color.GREEN); 
      g.fillOval(x,y, 4 * radius, 4 * radius); 
      repaint(); 
     } 

    } 


} 


class RedListener implements ActionListener{ 
    private Lab4Panel canvas; 

    RedListener(Lab4Panel canvas) { 
    this.canvas = canvas; 
    } 

    public void actionPerformed(ActionEvent e){ 
     canvas.setRed(); 
    } 
} 

class YellowListener implements ActionListener{ 
    private Lab4Panel canvas; 

    YellowListener(Lab4Panel canvas) { 
    this.canvas = canvas; 
    } 

    public void actionPerformed(ActionEvent e){ 
     canvas.setYellow(); 
    } 
} 

class GreenListener implements ActionListener{ 
    //private Lab4RadioButtonPanel canvas; 
    private Lab4Panel canvas; 

    GreenListener(Lab4Panel canvas) { 
    this.canvas = canvas; 
    } 

    public void actionPerformed(ActionEvent e){ 
     canvas.setGreen(); 
    } 
} 
+0

你可能想在後的代碼修剪只相關部分 – yurib 2012-02-14 23:58:29

+2

編輯:你在你的代碼張貼以上沒有計時器代碼。如果你不告訴我們你已經嘗試了什麼,我們如何幫助你?再次,你讀過[Swing Timer教程](http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html)嗎?再一次,特別讓你困惑的是什麼? – 2012-02-15 00:07:39

+0

我正在讀幾個價值的搖擺計時器信息,沒有一個是合理的。我希望有人能把我送到一個較低端的網頁。 – Robert 2012-02-15 00:11:26

回答

-1
class Lab4Panel extends JPanel{ 

public Lab4Panel(){ 

Timer timer = new Timer(2000, new TimerListener()); 
timer.start(); 

} 
+0

原作者的這種迴應並沒有說明它是如何解決他的問題的。沒有足夠的代碼來演示它如何適應原始代碼片段。對於最初引發一些有意義的信息查詢的問題,這是一種無關緊要的迴應。 – 2012-07-27 06:20:30