我做的背景没有出现,这对我来说是个大问题。继承人的代码:
//Package and imports...
public class startScreen extends JFrame
{
public static asteriod asteriod = new asteriod();
static JFrame Screen = new JFrame("Stars");
static boolean isShown = (boolean) Screen.isVisible();
static heart heart = new heart();
public static JPanel jpanel = new JPanel();
static Random rand = new Random();
static int suptogus = rand.nextInt(20)+1;
static enemy enemy = new enemy();
static player player = new player();
static JLabel creatoranddate = new JLabel("Copyright 2013© All rights reserved.");
public static int HEALTH = 100;
public static int health = 50;
public startScreen()
{
super("Stars");
setLayout(new FlowLayout());
Screen.setContentPane(new JPanel()
{
Image image = ImageIO.read(new File("src/stars/optionsBackground.png"));
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(image, 0, 0, 700, 600, this);
}
});
creatoranddate.setFont(new Font("Arial", Font.BOLD, 14));
creatoranddate.setForeground(Color.WHITE.brighter());
Point location = creatoranddate.getLocation();
System.out.println("Located JLabel, at position " + location);
add(creatoranddate);
//adds background picture
try{
//adds logo
JLabel logo = (new JLabel(new ImageIcon(ImageIO.read(new File("src/stars/logo.png")))));
add(logo);
System.out.println("Successfully added logo.\n");
}catch(IOException e){
System.out.println("Failed to display logo. This is prone for the Stars.jar program.\n");
e.printStackTrace();
}
try{
JLabel enemyLabel = new JLabel(new ImageIcon(ImageIO.read(new File("src/stars/enemyLabel.png"))));
JLabel playerLabel = new JLabel(new ImageIcon(ImageIO.read(new File("src/stars/playerLabel.png"))));
playerLabel.setForeground(null);
enemyLabel.setForeground(null);
enemyLabel.setBounds(0, 0, 700, 600);
playerLabel.setBounds(20, 20, 700, 600);
JLabel player = new JLabel(new ImageIcon(ImageIO.read(new File("src/stars/playerSprite.png"))));
JLabel enemy = new JLabel(new ImageIcon(ImageIO.read(new File("src/stars/enemySprite.png"))));
player.setForeground(null);
enemy.setOpaque(true);
player.setBounds(0, 0, 700, 600);
enemy.setBounds(0, -32, 700, 600);
}catch (Exception e){
e.printStackTrace();
}
}
public String getVersion(String str)
{
return str = "Version InDev 2.0";
}
public static void main(String[]args)
{
}
}
super("Stars");
问题在于方法末尾的声明正下方的所有代码paintComponent
。我不知道发生了什么,如果我什至throw IOException
,它仍然不显示背景图像。