我希望圖像顯示在網格頂部,但它們似乎被打包到不同的面板中。如何使用Java GUI面板?
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;
@SuppressWarnings("serial")
public class test4 extends JPanel {
BufferedImage image;
Dimension size = new Dimension();
public test4(BufferedImage image) {
this.image = image;
JPanel content = new JPanel(new GridLayout(8,8));
for (int i = 0; i < 8*8; ++i) {
JPanel panel = new JPanel();
if (i % 2 == i/8 % 2) {
panel.setBackground(Color.WHITE);
} else {
panel.setBackground(Color.DARK_GRAY);
}
content.add(panel);
}
this.add(content);
}
protected void paintComponent(Graphics g) {
int x = 100;
int y = 300;
g.drawImage(image, x, y, this);
}
public static void main(String[] args) throws IOException {
String path = "images/Untitled.png";
BufferedImage image = ImageIO.read(new File(path));
test4 test = new test4(image);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_O…
f.add(test);
f.setSize(400,400);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
嗯,我終於得到了標籤來改變顏色,所以沒有必要再畫矩形。對不起,我不明白你在說什麼,但我最後修復了它。 – 2010-11-06 10:24:23