【发布时间】:2014-03-01 00:43:44
【问题描述】:
这就是我现在拥有的:
这是我希望图像看起来的样子:
我有两个 java 文件,一个扩展 JFrame 和 Jpanel 部分看起来基本上是
ShinyButtons panel = new ShinyButtons();
panel.setLocation(10, 10);
getContentPane().add(panel);
另一个用 JButtons 扩展了 JPanel 导入 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]; }
}
【问题讨论】:
标签: java swing user-interface jframe jbutton