2016-11-28 14 views
0

オレンジ色の「selectedButton」を除いて、すべて同じ色、緑色のボタンの行があります。ボタンをクリックすると、新しいselectedButtonになるので、オレンジ色に変わり、他は緑色に変わります。私のコードは新しいJButtonをオレンジ色に正しく変更しますが、前の色はオレンジ色から緑色に変更されません。色を変えてもJButtonの色は変わりません

paintComponent()メソッドでrevalidate()とrepaint()を呼び出すと完全に動作しますが、repaint()メソッドはpaintComponent()メソッドを再度呼び出すので、それはひどい考えです。何らかの無限回帰に終わるかもしれませんが、私はそれほど遠くないので、なぜ私は確信していません。私はrevalidate()とrepaint()が自分のコードで最良の場所に配置されていれば問題を解決すると確信していますが、どこに追加すればいいですか? mouseReleased()イベントのボタンの色を変更した後、両方を追加するのに疲れましたが、動作しませんでした。

class StudentButton extends JButton{ 
    private int index; 
    private Color startGradient, endGradient; //Used for colouring JButton 
    private static StudentButton selectedButton; 

    public StudentButton(int i, boolean isSelected) { this("", i, isSelected); } 
    public StudentButton(String text, int i, boolean isSelected){ 
     super(text); index = i; 
     if(isSelected) 
      StudentButton.setSelectedButton(this); 
      //Colour set to orange 
      startGradient = new Color(234, 249, 99); 
      endGradient = new Color(230, 252, 35); 
     } 
     else { 
      //Colour set to green 
      startGradient = new Color(159, 255, 76); 
      endGradient = new Color(101, 183, 29); 
     } 

     addMouseListener(new MouseListener() { 
      public void mouseEntered(MouseEvent e) {} 
      public void mouseExited(MouseEvent e) {} 

      //mouseReleased better than mouseClicked, since it always triggers for an attempted click 
      public void mouseReleased(MouseEvent e) { 
       StudentButton selButton = StudentButton.getSelectedButton(); 
       selButton.setColourGradients(new Color(159, 255, 76), new Color(101, 183, 29)); 

       StudentButton newSelButton = (StudentButton)(e.getSource()); 
       newSelButton.setColourGradients(new Color(234, 249, 99), new Color(230, 252, 35)); 
       StudentButton.setSelectedButton(newSelButton); 
       //////////////////////////////////////////////// 
       //////////////////////////////////////////////// 
       //////////////////////////////////////////////// 
       //tried adding revalidate() and repaint() here, wouldn't work 
       //////////////////////////////////////////////// 
       //////////////////////////////////////////////// 
       //////////////////////////////////////////////// 
      } 
      public void mouseClicked(MouseEvent e) {} 
      public void mousePressed(MouseEvent e) {} 
     }); 

     setContentAreaFilled(false); ///removes ugly button border 
    } 

    public static StudentButton getSelectedButton() { return selectedButton; } 
    public static void setSelectedButton(StudentButton sb) { selectedButton = sb; } 

    public void setColourGradients(Color start, Color end) { 
     startGradient = start; endGradient = end; 
     System.out.println("Button "+index+", set to colour"+start); 
    } 
    protected void paintComponent(Graphics g) { 
     Graphics2D g2 = (Graphics2D) g; 

     GradientPaint p; 
     p = new GradientPaint(0, 0, startGradient, 
       0, getHeight(), endGradient); 

     g2.setPaint(p); 
     g2.fillRect(0, 0, getWidth(), getHeight()); 
     g2.setPaint(g2.getPaint()); 
     //////////////////////////////////////////////// 
     //////////////////////////////////////////////// 
     //This makes the buttons change correctly, but seems like a very bad idea     
     revalidate(); repaint(); 
     //////////////////////////////////////////////// 
     //////////////////////////////////////////////// 
     //////////////////////////////////////////////// 

     super.paintComponent(g); 
    } 

    public int getIndex() {return index;} 
} 
+0

paintメソッド(このメソッドの外) 'this.repaint()'ではなく、JButtonがこの場合のコンテナです。 –

+0

私はちょうど今、私はparent.revalidate()とparent.repaint()と呼ばれていたが、それでもまだdeosn'tの作業をStudentButton parent = this、そしてmouseReleasedメソッドの中に追加しなければならなかった – user217339

+0

すぐに良い助けのために、[MCVE]または[Short、Self Contained、Correct Example](http://www.sscce.org/)を投稿してください。 –

答えて

0

のmouseReleased()メソッドに変更された単一StudentButtonオブジェクトではなく、両方の私は(再描画呼び出し)と再確認されたことに気づきました@Bo_Halimのおかげで()。私は以前の選択されたボタンと新しい選択されたボタンをボットに再描画するように、mousePressedメソッドを変更しました。

  public void mousePressed(MouseEvent e) { 
       StudentButton selButton = StudentButton.getSelectedButton(); 
       selButton.setColourGradients(new Color(159, 255, 76), new Color(101, 183, 29)); 
       selButton.revalidate(); selButton.repaint(); 

       StudentButton newSelButton = (StudentButton)(e.getSource()); 
       newSelButton.setColourGradients(new Color(234, 249, 99), new Color(230, 252, 35)); 
       StudentButton.setSelectedButton(newSelButton); 
       newSelButton.revalidate(); newSelButton.repaint(); 
      } 
関連する問題