我想使用paint方法将图像添加到屏幕上?我不想使用 JLabel,因为我想自己设置位置。
板级:
public class Board extends JPanel{
BufferedImage image;
public Board() {
try {
image = ImageIO.read(new File("C:\\Users\\alexa_000\\Pictures\\RocketShip.png"));
}catch(IOException e) {
e.printStackTrace();
}
}
protected void paintComponent(Graphics g) {
super.paintComponents(g);
g.drawImage(image, 0, 0, this);
}
public Dimension getPreferredSize() {
return new Dimension(image.getWidth(),image.getHeight());
}
}
RType.class:
public class RType extends JFrame{
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
Panel panel = new Panel();
frame.setContentPane(panel);
frame.setSize(800,600);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}