2016-04-14 47 views
0

任務是編寫Belisha Beacon的代碼,該代碼以閃爍和顏色在淺灰色和橙色之間切換並具有閃爍和穩定兩個按鈕開始。所以當我點擊穩定按鈕時,燈塔必須保持橙色,但對於我的程序,燈塔保持穩定,無論點擊什麼顏色,當我點擊了穩定按鈕,並且點擊了Flash按鈕後,點擊了穩定的按鈕,信標不會再次閃爍。這是我的代碼到目前爲止,有人請幫我,我哪裏出了問題,謝謝你:)。Belisha Beacon Java程序按鈕查詢

import java.awt.*; 
import java.awt.geom.*; 
import java.awt.event.*; 
import javax.swing.Timer; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class BelishaBeacon { 
    private static Timer timer; 
    public class Drawing extends JPanel { 

     private int x = 125; 
     private int y = 80; 
     private boolean changeColors = false; 


     public void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      Graphics2D g2 = (Graphics2D) g; 
      //creating the shapes 
      Rectangle box1 = new Rectangle(165, 180, 20, 45); 
      Rectangle box2 = new Rectangle(165, 225, 20, 45); 
      Rectangle box3 = new Rectangle(165, 270, 20, 45); 
      Rectangle box4 = new Rectangle(165, 315, 20, 45); 
      Rectangle box5 = new Rectangle(165, 360, 20, 45); 
      Rectangle box6 = new Rectangle(165, 405, 20, 45); 

      //drawing the shapes 
      Ellipse2D.Double ball = new Ellipse2D.Double(x, y, 100, 100); 
      g2.draw(ball); 
      g2.draw(box1); 
      g2.draw(box2); 
      g2.draw(box3); 
      g2.draw(box4); 
      g2.draw(box5); 
      g2.draw(box6); 
      //coloring the shapes 
      g2.setColor(Color.BLACK); 
      g2.fill(box1); 
      g2.fill(box3); 
      g2.fill(box5); 
      g2.setColor(Color.ORANGE); 
      g2.fill(ball); 
      changeColors = !changeColors; 
      if (changeColors) { 
       g2.setColor(Color.darkGray); 
       g2.fill(new Ellipse2D.Double(x, y, 100, 100)); 
      } 
     } 




     public void changeColors() { 
      changeColors = true; 
      repaint(); 
     } 
    } 

    public BelishaBeacon() { 
     //Creation of frame 
     JFrame frame = new JFrame(); 
     frame.setSize(350, 570); 
     frame.setTitle("Belisha Beacon"); 
     frame.setLayout(new BorderLayout(0, 0)); 
     final Drawing shapes = new Drawing(); 

     timer = new Timer(500, new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       shapes.repaint(); 
      } 
     }); 



     JButton jbtFlash = new JButton("Flash"); 
     jbtFlash.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 


      } 
     }); 




     final JButton jbtSteady = new JButton("Steady"); 
     jbtSteady.addActionListener(
       new ActionListener() { 
        public void actionPerformed(ActionEvent e) { 
         timer.stop(); 
        } 
       }); 

     //Positioning 
     JPanel controlPanel = new JPanel(); 
     controlPanel.setLayout(new GridLayout(1, 2, 0, 0)); 
     controlPanel.add(jbtFlash); 
     controlPanel.add(jbtSteady); 

     frame.add(controlPanel, BorderLayout.SOUTH); 
     frame.add(shapes); 

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     new BelishaBeacon(); 
     timer.start(); 
    } 
} 

回答

1

因此,對於您的Flash按鈕,偵聽器當前爲空。你想要做的是檢查當按下閃光燈按鈕時,再次啓動定時器。

試試這個

JButton jbtFlash = new JButton("Flash"); 
    jbtFlash.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      System.out.println("Flashing"); 
      if (!timer.isRunning()) { 
       timer.start(); 
      } 

     } 
    }); 

併爲你的穩固按鈕的問題。目前沒有邏輯來檢查當前的顏色是灰色還是黃色。所以你需要修改代碼來檢查當前的顏色狀態,並在黃色時停止它。有一些想法:)

+1

非常感謝你bili :),這幫了我很多:D。 – Federer

+0

@Federer歡迎您:) – bili

+0

@Federer現在有一個穩定的按鈕,如果你仍然堅持,那麼請點擊這裏。 – bili