2016-04-15 58 views
0

基本上,當我單擊穩定按鈕時,我的Belisha Beacon必須保持橙色,但在我的程序中單擊穩定按鈕時,信標保持爲淺灰色。有人可以請確定我哪裏錯了嗎?謝謝你:)。這裏是我的代碼:Belisha Beacon計劃中的JButton查詢

package Homework; 


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; 

      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); 


      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); 

      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.lightGray); 
       g2.fill(new Ellipse2D.Double(x, y, 100, 100)); 
      } 
     } 




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

    public BelishaBeacon() { 

     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) { 
       System.out.println("Flashing"); 
       if (!timer.isRunning()) { 
        timer.start(); 
       } 

      } 
     }); 




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

         timer.stop(); 
        } 
       }); 


     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(); 
    } 
} 
+0

這是完全一樣的這個http://stackoverflow.com/questions/36651192/jbutton-query-in-belisha-beacon-program。你有沒有可能成爲費德勒的雙胞胎? – bili

+0

你大概可以用我留給費德勒的提示:) – bili

+0

@bili我和他在同一個班級啊哈,我和他都試過,但沒有到哪裏:(. –

回答

2

試試這個:

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; 

      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); 


      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); 

      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.lightGray); 
       g2.fill(new Ellipse2D.Double(x, y, 100, 100)); 
      } 
     } 




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

     public boolean getChangeColors() { 
      return changeColors; 
     } 
    } 

    public BelishaBeacon() { 

     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(); 
       shapes.changeColors(); 
      } 
     }); 



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

      } 
     }); 




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

         timer.stop(); 
         if(shapes.getChangeColors()) { 
          shapes.changeColors(); 
         } 
        } 
       }); 


     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(); 
    } 
} 
+0

我嘗試過,但是我需要Beacon先手動閃爍,並且只有當我點擊穩定按鈕時它纔是橙色的,當我再次單擊閃光燈按鈕時,它必須再次閃爍。 –

+0

我剛剛編輯了代碼。它的工作 – Paul

+0

我只是現在試了一下,它的工作原理。哦,我非常感謝你的幫助:)。 –

2

,而不是試圖切換的顏色paintComponent(),給Drawing一個Color,並用它來渲染球:

private Color color = Color.lightGray; 

    @Override 
    public void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     Graphics2D g2 = (Graphics2D) g; 
     … 
     g2.setColor(color); 
     g2.fill(ball); 
     … 
    } 

品牌changeColors()實際改變顏色:

public void changeColors() { 
     if (Color.orange.equals(color)) { 
      color = Color.lightGray; 
     } else { 
      color = Color.orange; 
     } 
     repaint(); 
    } 

,並添加makeSteady()方法:

public void makeSteady() { 
    color = Color.orange; 
    repaint(); 
} 

現在,你的計時器的動作可以做shapes.changeColors(),你閃存按鈕處理程序可以做timer.restart()和你穩定按鈕處理程序可以做這:

timer.stop(); 
shapes.makeSteady(); 

此外,請不要忘記invokeLater()

+0

非常感謝:D。 –