背景没有出现

时间:2013-07-03 00:23:40

标签: java swing background jframe jpanel

我所做的背景并未出现,这对我来说是个大问题。下面是代码:

//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");声明paintComponentthrow IOException方法末尾正下方的所有代码。我不知道发生了什么,如果我甚至{{1}},它仍然没有显示背景图像。

0 个答案:

没有答案