2014-01-21 3 views
0

Я пытаюсь использовать сетку [x] [y] .setPressedBackgroundColor (getColor()); чтобы установить фон, но я получаю сообщение об ошибкеКак установить заданный цвет фона JButton?

error: cannot find symbol 
grid[x][y].setPressedBackgroundColor(getColor()); 
     ^
    symbol: method setPressedBackgroundColor(Color) 
    location: class JButton 
1 error 

Process completed. 

Вот код для JButton. Ошибка возникает в ButtonGrid конструктор:

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

public class ButtonGrid { 

class MyButton extends JButton { 

     public Color hoverBackgroundColor; 
     public Color pressedBackgroundColor; 

     public MyButton() { 
      this(null); 
     } 

     public MyButton(String text) { 
      super("text"); 
      super.setContentAreaFilled(false); 
     } 
     @Override 
     public void paintComponent(Graphics g) { 
      if (getModel().isPressed()) { 
       g.setColor(pressedBackgroundColor); 
      } else if (getModel().isRollover()) { 
       g.setColor(hoverBackgroundColor); 
      } else { 
       g.setColor(getBackground()); 
      } 
      g.fillRect(0, 0, getWidth(), getHeight()); 
      super.paintComponent(g); 
     } 

     @Override 
     public void setContentAreaFilled(boolean b) { 
     } 

     public Color getHoverBackgroundColor() { 
      return hoverBackgroundColor; 
     } 

     public void setHoverBackgroundColor(Color hoverBackgroundColor) { 
      this.hoverBackgroundColor = hoverBackgroundColor; 
     } 

     public Color getPressedBackgroundColor() { 
      return pressedBackgroundColor; 
     } 

     public void setPressedBackgroundColor(Color pressedBackgroundColor) { 
      this.pressedBackgroundColor = pressedBackgroundColor; 
     } 
    } 




     JFrame frame=new JFrame(); //creates frame 
     JButton[][] grid; //names the grid 

     public ButtonGrid(int width, int length){ 
      int count = 1; //counting variable 
      frame.setLayout(new GridLayout(width,length)); //sets the layout 
      grid=new JButton[width][length]; //sets the size of grid 
      for(int y=0; y<length; y++){ 
       for(int x=0; x<width; x++){ 
        grid[x][y]=new JButton("("+ count++ +")"); //creates new button  
        grid[x][y].setForeground(getColor()); //sets text color 
        grid[x][y].setHorizontalTextPosition(SwingConstants.CENTER); //centers text 
        grid[x][y].setBorder(null); //removes border 
        grid[x][y].setBackground(getColor()); //sets color of jbutton 
        grid[x][y].setPressedBackgroundColor(getColor()); 
        frame.add(grid[x][y]); //adds button to grid      
       } 
      } 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      frame.pack(); //sets appropriate size for frame 
      frame.setVisible(true); //makes frame visible 
     } 

     public Color getColor() { 
      int rval = (int)Math.floor(Math.random() * 256); 
      int gval = (int)Math.floor(Math.random() * 256); 
      int bval = (int)Math.floor(Math.random() * 256); 
      return new Color(rval, gval, bval); 
     } 

     public static void main(String[] args) { 
      new ButtonGrid(3,3);//makes new ButtonGrid with 2 parameters      
     } 
} 
+0

я бы, вероятно, сделать это, используя изображения, как показано в [этот ответ] (http://stackoverflow.com/a/21248363/418556). –

+0

BTW - Используйте последовательный и логический отступ для кодовых блоков. Отступ кода предназначен для того, чтобы помочь людям понять поток программы. –

+0

Это не должно быть JButton [] [] сетка; // называет сетку, но она должна быть сетью MyButton [] []; Вы преодолели класс JButton. – KernelPanic

ответ

Смежные вопросы