這是我現在有:如何正確設置JButton的大小與圖像?
這就是我想要的形象是這樣的:
我有兩個java文件,一個擴展的JFrame,並與部分Jpanel看起來基本上是
ShinyButtons panel = new ShinyButtons();
panel.setLocation(10, 10);
getContentPane().add(panel);
另一個擴展JPanel和JButtons import javax.swing。*;
public class ShinyButtons extends JPanel{
public static byte RED = 0;
public static byte ORANGE = 1;
public static byte YELLOW = 2;
public static byte GREEN = 3;
public static byte BLUE = 4;
public static byte LIGHT_GRAY = 5;
public static byte DARK_GRAY = 6;
public static byte ROWS = 8;
private byte[][] buttonTable;
public ShinyButtons() {
buttonTable = new byte[ROWS][ROWS];
resetButtons();
setBorder(BorderFactory.createEmptyBorder());
setSize(552,552);
ImageIcon[] icons = {new ImageIcon("RedButton.png"), new ImageIcon("OrangeButton.png"), new ImageIcon("YellowButton.png"),
new ImageIcon("GreenButton.png"), new ImageIcon("BlueButton.png"), new ImageIcon("LightGrayButton.png"),
new ImageIcon("DarkGrayButton.png")};
for (int row=0; row<8; row++){
for (int col=0; col<8; col++){
JButton buttons = new JButton();
buttons.setSize(69,69);
buttons.setIcon(icons[(byte)(Math.random()*7)]);
add(buttons);
buttons.setLocation(row*69, col*69);
buttons.setBorder(BorderFactory.createEmptyBorder());
}
}
}
private void resetButtons() {
for (int r=0; r<ROWS; r++)
for (int c=0; c<ROWS; c++)
buttonTable[r][c] = (byte)(Math.random()*7);
}
public byte getButton(int r, int c) { return buttonTable[r][c]; }
}