2013-04-06 25 views
2

按鈕的下面的網格定義爲畫的特定按鈕:從按鈕的格子

JButton button_x = new RoundButton(); 

enter image description here

其中RoundButton定義爲:

public class RoundButton extends JButton { 

    public RoundButton(String label) { 
     super(label); 
     this.setContentAreaFilled(false); 
     Dimension size = this.getPreferredSize(); 
     size.height = size.width = Math.max(size.height, size.width); 
     this.setPreferredSize(size); 
    } 

    @Override 
    protected void paintComponent(Graphics g) { 
     if(!GameState.getIfComplete()) { // If the game is not complete or has just started 
      this.setBorder(null); 
      g.setColor(Color.BLACK); 
      g.fillRect(0, 0, this.getSize().width, this.getSize().height); 
      if(this.getModel().isArmed()) { 
       g.setColor(Color.RED); 
      }else { 
       g.setColor(Color.GREEN); 
      } 
      g.fillOval(0,0,this.getSize().width-1,this.getSize().height-1); 
      super.paintComponent(g); 
     }else { 
      this.setBorder(null); 
      g.setColor(Color.BLACK); 
      g.fillRect(0, 0, this.getSize().width, this.getSize().height); 
      g.setColor(Color.WHITE); 
      g.fillOval(0,0,this.getSize().width-1,this.getSize().height-1); 
      super.paintComponent(g); 
     } 
    } 

}

目前所有的按鈕都塗成綠色,但在一個cer上tain條件我想繪製白色的特定按鈕(這是else部分中的代碼)。例如,當!GameState.getIfComplete()返回false我想在第一列用白色繪製按鈕。所以我打電話repaint爲:

buttons[0].repaint(); 
buttons[3].repaint(); 
buttons[6].repaint(); 

但這不起作用!在第一列中,其他一些按鈕也被塗成白色。這是爲什麼 ?

呼叫有什麼問題?我如何繪製一個特定的按鈕?

+0

你或許應該只是創造彩色圖標及將它們設置爲一個標準的按鈕。爲了更快地獲得更好的幫助,請發佈[SSCCE](http://sscce.org/)。 – 2013-04-06 04:30:18

+0

我確定我已經解決了調用'this.setBorder(null);'是一個可怕的壞主意。如果您不想設置邊框,請嘗試覆蓋'getBorder'方法並從中返回'null' – MadProgrammer 2013-04-06 04:46:22

+0

@MadProgrammer是啊!我也解決了這個問題。它只是反映在這裏。 – saplingPro 2013-04-06 05:06:09

回答

4

的問題是在GameState的依賴,你的所有圓形按鍵使用相同的邏輯來畫自己,也就是完成比賽的時候,他們都將被WHITE

而是畫,你應該依靠按鈕的屬性。設置它,以便顏色實際上是從按鈕本身派生的。

enter image description hereenter image description here

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.Graphics; 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class BadButton01 { 

    public static void main(String[] args) { 
     new BadButton01(); 
    } 

    public BadButton01() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(new TestPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 

     }); 
    } 

    public static class GameState { 

     private static boolean isComplete; 

     public static boolean getIfComplete() { 
      return isComplete; 
     } 

     public static void setComplete(boolean value) { 
      isComplete = value; 
     } 

    } 

    public class TestPane extends JPanel { 

     private RoundButton[] btns = new RoundButton[] 
     { 
      new RoundButton("1"), 
      new RoundButton("2"), 
      new RoundButton("3"), 
      new RoundButton("4"), 
      new RoundButton("5"), 
      new RoundButton("6"), 
      new RoundButton("7"), 
      new RoundButton("8"), 
      new RoundButton("9") 
     }; 

     public TestPane() { 
      setLayout(new GridLayout(3, 3)); 
      for (RoundButton btn : btns) { 
       add(btn); 
      } 
      btns[0].addActionListener(new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        GameState.setComplete(true); 
        btns[0].setBackground(Color.WHITE); 
        btns[1].setBackground(Color.WHITE); 
        btns[2].setBackground(Color.WHITE); 
        repaint(); 
       } 
      }); 
     } 

    } 

    public class RoundButton extends JButton { 

     public RoundButton(String label) { 
      super(label); 
      this.setContentAreaFilled(false); 
      setBorderPainted(false); 
      setFocusPainted(false); 
      setOpaque(false); 
      Dimension size = this.getPreferredSize(); 
      size.height = size.width = Math.max(size.height, size.width); 
      this.setPreferredSize(size); 
      setBackground(Color.GREEN); 
     } 

     @Override 
     protected void paintComponent(Graphics g) { 
//   if (!GameState.getIfComplete()) { // If the game is not complete or has just started 
//    this.setBorder(null); 
//    g.setColor(Color.BLACK); 
//    g.fillRect(0, 0, this.getSize().width, this.getSize().height); 
       if (this.getModel().isArmed()) { 
        g.setColor(Color.RED); 
       } else { 
//     g.setColor(Color.GREEN); 
        g.setColor(getBackground()); 
       } 
//   } else { 
//    this.setBorder(null); 
//    g.setColor(Color.BLACK); 
//    g.fillRect(0, 0, this.getSize().width, this.getSize().height); 
//    g.setColor(Color.WHITE); 
//    g.fillOval(0, 0, this.getSize().width - 1, this.getSize().height - 1); 
//    g.setColor(getBackground()); 
//   } 
      g.fillOval(0, 0, this.getSize().width - 1, this.getSize().height - 1); 
      super.paintComponent(g); 
     } 

    } 

} 
+0

爲什麼不調用'按鈕[btn_number] .repaint'不只繪製特定的按鈕 – saplingPro 2013-04-06 05:15:24

+1

因爲這不是繪畫的工作方式。 repaint管理器接受所有重繪請求,並將它們全部組合成單個請求,其中可能包含其他組件。克服你控制油漆過程的錯覺,你不重視重繪!看看[在AWT和Swing中繪畫](http://www.oracle.com/technetwork/java/painting-140037.html)瞭解更多細節的解釋 – MadProgrammer 2013-04-06 05:21:02

+0

好吧!謝謝 – saplingPro 2013-04-06 05:33:34