JLabel没有去我想去的地方

时间:2013-04-02 23:41:14

标签: java swing jframe jpanel jlabel

我正在制作一款游戏并且无法让JLabel在特定地点展示enter image description here

我希望JLabel位于关卡面板之上。当我尝试做我认为可行的工作时,它会显示一个带有标签的空白屏幕但没有水平面板。另外,当我拖动窗口时,为什么我的游戏会重新正确地重绘?

游戏

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;


public class Game {

    private Level l;
    private Player p;
    private ArrayList<Monster> m;
    private static JLabel status = new JLabel();
    private JLabel score = new JLabel();
    private static LevelPanel lp;
    private Timer timer;
    public Game()
    {
        l = new Level(this,4,11);
        p = new Player(this,1,1);
        m = new ArrayList<Monster>();
        Monster monster = new SimpleMonster(this,0,0,0,1);
        Monster m1 = new SimpleMonster(this);
        m.add(monster);
        m.add(m1);
        lp = new LevelPanel(this);
        status = new JLabel("null");
        lp.add(status);
        timer = new Timer(10, new TimerListener());
        timer.start();
    }


    public void win()
    {
        timer.stop();
        status.setText("Congrats you won!");
        System.out.println("You win!");
    }

    public void lose()
    {
        timer.stop();
        status.setText("Sorry you lose.");
//      System.out.println("Sorry");
    }

    public static void main(String[] args)
    {
        JFrame jframe = new JFrame();

        jframe.add(new LevelPanel(new Game()));
//      jframe.add(lp);
        jframe.getContentPane().add(lp);
//      jframe.getContentPane().add(status);
//      jframe.add(status);
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jframe.setPreferredSize(new Dimension(500, 500));   
        jframe.pack();
        jframe.setVisible(true);

    }

    private class TimerListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent ae) {
            // TODO Auto-generated method stub
//          panel.repaint();

            //update score and remainder
            for (Monster m : getMonsters())
            {
                m.update();
            }
            //call update() of all monsters
        }
    }
    public Level getLevel()
    {
        return l;
    }

    public Player getPlayer()
    {
        return p;
    }

    public ArrayList<Monster> getMonsters()
    {
        return m;
    }
}

我的关卡面板

import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JPanel;


public class LevelPanel extends JPanel implements KeyListener{

    private Game game;
    public LevelPanel(Game game)
    {
        this.game = game;
        setFocusable(true);
        addKeyListener(this);

    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        game.getLevel().draw(g);
        repaint();
    }
    @Override
    public void keyPressed(KeyEvent arg0) {

        if (arg0.getKeyCode() == KeyEvent.VK_UP)
        {
            game.getPlayer().move(-1, 0);
        }
        if (arg0.getKeyCode() == KeyEvent.VK_DOWN)
        {
            game.getPlayer().move(1, 0);
        }
        if (arg0.getKeyCode() == KeyEvent.VK_LEFT)
        {
            game.getPlayer().move(0, -1);
        }
        if (arg0.getKeyCode() == KeyEvent.VK_RIGHT)
        {
            game.getPlayer().move(0, 1);
        }
//      System.out.print(game.getLevel().getItemsLeft());
//      repaint();
    }
    @Override
    public void keyReleased(KeyEvent arg0) {
        // TODO Auto-generated method stub

    }
    @Override
    public void keyTyped(KeyEvent arg0) {
        // TODO Auto-generated method stub

    }

}

1 个答案:

答案 0 :(得分:1)

问题是你只需使用add()添加JPanel和JLabel,这样它们就可以到达默认值的任何地方。如果要将它们放置在某个对齐位置,则需要使用布局管理器。最简单的一个是BorderLayout,但如果你想要完全控制,你应该使用GridBagLayout。使用GridBag,您可以为每个单独的Component设置constaints。要了解GridBagLayout并从长远来看节省时间,我建议你查看Constraints和Insets以及GridBagLayout。