如何将面板添加到JFrame

时间:2016-01-18 02:40:59

标签: java swing jframe jpanel

所以我有点新的编程,我有一个奇怪的问题。因此,我正在尝试将带有标签的“信息”面板添加到我的“JFrame”中,但是当我运行该程序时,面板不显示。另外,我如何将它显示在面板出现在JFrame左上角的位置。

private Lilac player;
private int[] xPos;
private JLabel lblShowLives, lblShowScore, lblTimer;
private int lives, score, seconds, minutes;
private ImageIcon[] imgBackground;
private Timer backgroundTimer, cutsceneTimer, Timer;
private AudioStream auStream;
private AudioPlayer auPlayer;
private InputStream in;
private DecimalFormat df, df2;


public FreedomPlanet() {

    df = new DecimalFormat("00");
    df2 = new DecimalFormat("0000");

    auPlayer = AudioPlayer.player;

    // creates the player
    player = new Lilac();

    // 
    lives = 3;
    score = 0;
    seconds = 0;
    minutes = 0;

    imgBackground = new ImageIcon[2];
    // gets the image for the background inside of a for loop   
    for (int i = 0; i < imgBackground.length; i++)
    {
        imgBackground[i] = new ImageIcon("Stage\\Forest.gif");
    } 
    xPos = new int[] {0, imgBackground[0].getIconWidth()};

    // sets the amount of time for each timer
    backgroundTimer = new Timer(50, this);
    Timer = new Timer(1000, this);

    // creates a label that displays "TIME"
    JLabel lblTime = new JLabel("TIME");
    lblTime.setFont(new Font("Britannic Bold", Font.BOLD, 18));

    // label that displays the time
    lblTimer = new JLabel();
    lblTimer.setPreferredSize(new Dimension(100, 30));
    lblTimer.setFont(new Font("Britannic Bold", Font.BOLD, 18));
    lblTimer.setHorizontalAlignment(SwingConstants.CENTER);
    lblTimer.setText(df.format(minutes) + ":" + df.format(seconds));

    // creates a label that says "SCORE"
    JLabel lblScore = new JLabel("SCORE");
    lblTime.setFont(new Font("Britannic Bold", Font.BOLD, 18));

    // label that displays the users score
    lblShowScore = new JLabel();
    lblShowScore.setPreferredSize(new Dimension(100, 30));
    lblShowScore.setFont(new Font("Britannic Bold", Font.BOLD, 18));
    lblShowScore.setHorizontalAlignment(SwingConstants.CENTER);
    lblShowScore.setText(df.format(score));

    // creates a panel called Info and and adds components to them
    JPanel Info = new JPanel();
    Info.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
    Info.setPreferredSize(new Dimension(270, 180));
    Info.add(lblTime);
    Info.add(lblTimer);
    Info.add(lblScore);
    Info.add(lblShowScore);


    addKeyListener(this);
    setFocusable(true);
    setLayout(null);
    JFrame frame = new JFrame();
    frame.add(Info);
    frame.add(this, BorderLayout.CENTER);
    frame.setContentPane(this);
    frame.setTitle("Freedom Planet");
    frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    frame.addWindowListener(this);
    frame.setResizable(false);
    frame.setSize(500, 500);
    frame.setSize(imgBackground[0].getIconWidth(), imgBackground[0].getIconHeight());
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

    // starts the background timer
    backgroundTimer.start();
    //Timer.start();

    // sets the location of the player
    player.setLocation(170, 395);

    Timer.start();

}

0 个答案:

没有答案