我一直在試圖通過展示一個JPanel,但圖像只顯示了,如果我調整的JFrame圖像不會出現,除非我調整的JFrame
顯示器上顯示的JFrame一個形象:
package display;
import javax.swing.JFrame;
import img.*;
public class Screen extends JFrame{
private static final long serialVersionUID = 1L;
Spaceship s = new Spaceship();
public static void main(String[]args){
new Screen();
}
public Screen(){
setTitle("Spaceships!");
setSize(700,605);
setLocationRelativeTo(null);
add(s);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
}
飛船
package img;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.JPanel;
public class Spaceship extends JPanel{
private static final long serialVersionUID = 1L;
Image spaceship = (Image)Toolkit.getDefaultToolkit().getImage(getClass().getResource("res/spaceship.png"));
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(spaceship,100,100,null);
}
}
我有知道線索怎麼回事所以任何幫助我得到的,將不勝感激