1.程式碼
import java.awt.*;
import javax.swing.*;
public class Test extends JFrame {
public Test() {
super("Test");
Container container = getContentPane();
container.setLayout(new FlowLayout());
JPanel grid_panel = new JPanel(new GridLayout(3, 3));
JButton[] buttons = new JButton[9];
ImageIcon image = new ImageIcon("C360_2013-06-23-15-43-36-777.jpg");
Dimension dimension = new Dimension(image.getIconWidth(), image.getIconHeight());
for (int i = 0; i < buttons.length; i++) {
buttons[i] = new JButton(image);
buttons[i].setPreferredSize(dimension);
buttons[i].setMaximumSize(dimension);
buttons[i].setMinimumSize(dimension);
grid_panel.add(buttons[i]);
}
container.add(grid_panel);
setSize(500, 500);
setVisible(true);
}
public static void main(String[] args) {
Test app = new Test();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
2.完成圖
沒有留言:
張貼留言