gpt4 book ai didi

java - JLabel 没有去我想要它去的地方

转载 作者:行者123 更新时间:2023-12-01 14:42:48 26 4
gpt4 key购买 nike

我正在制作游戏,但无法在特定位置显示 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

}

}

最佳答案

问题是您只是使用 add() 添加了 JPanel 和 JLabel,因此它们只会转到默认位置。如果您想将它们放置在特定的对齐方式中,您需要使用布局管理器。最简单的使用方法是 BorderLayout,但如果您想要完全控制,则应该使用 GridBagLayout。使用 GridBag,您可以为每个单独的组件设置约束。为了理解 GridBagLayout 并从长远来看节省您的时间,我建议您查找 Constraints 和 Insets 以及 GridBagLayout。

关于java - JLabel 没有去我想要它去的地方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15776407/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com