为什么我的 JLabel 没有显示?
Why isn't my JLabel showing?
我已经做了很多研究,但我仍然不确定如何解决这个问题。我正在尝试制作游戏,在其中我需要一个图标显示在屏幕上的不同位置。现在,我只是想让一个图标可见。
我有一个 class 处理来自键盘的输入(名为 KeyInputHandler),另一个 class (名为 DrawGameBoard)创建 window 和背景。在 DrawGameBoard 中,我有一个名为 moveIcon 的方法,它应该显示一个图标。
public class DrawGameBoard extends JPanel
{
public static DrawGameBoard panel = new DrawGameBoard();
public static JFrame window = new JFrame("Fill the Boxes");
public static Container c = window.getContentPane();
public void moveIcon(int x, int y, JLabel label)
{
c.add(label);
//label.setLocation(x,y);
c.validate();
c.repaint();
System.out.println("tried to show a label");
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.GRAY);
g.fillRoundRect(5, 5, 585, 585, 15, 15);
for(int x = 25; x < 515; x+=79)
{
for(int y = 25; y< 515; y+=79)
{
g.setColor(Color.BLACK);
g.fillRect(x, y, 68, 68);
g.setColor(Color.WHITE);
g.fillRect(x+10, y+10, 48, 48);
}
}
}
public static void main(String[] args)
{
KeyInputHandler k = new KeyInputHandler();
//create the window
//JFrame window = new JFrame("Fill the Boxes");
window.setBounds(0, 0, 600, 630);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setResizable(false);
//create the panel
//DrawGameBoard panel = new DrawGameBoard();
//Container c = window.getContentPane();
panel.setBackground(new Color(0,222,222));
panel.addKeyListener(k);
panel.setFocusable(true);
//panel.setLayout(null);//doesnt use default locations
c.add(panel);
window.setVisible(true);
}
}
}
这在 KeyInputHandler 中被调用
public void keyPressed(KeyEvent e)
{
System.out.println("keyPressed: ");
BoxGame b = new BoxGame();
DrawGameBoard d = new DrawGameBoard();
//create icons to be moved
MovingPlayerIcon icon1 = new MovingPlayerIcon(1,0);
JLabel p1fill = new JLabel();
p1fill.setIcon(icon1);
////////////////////////////////////////////////////////////////
//controls for p1
if (e.getKeyCode() == KeyEvent.VK_A) {
if(b.getX1()>0){
b.setPos(b.getX1()-1, b.getY1(), 1);
}
System.out.println("A");
d.moveIcon(0,0,p1fill);
}
////////////////////////////////////////////////////////////////
}
因此,当我按下 'A' 键时,会调用 moveIcon 方法。我知道正在调用 moveIcon 方法,因为在按下 A 键时,将打印 "A" 并打印 "tried to show a label" 。我已经尝试用普通文本 JLabel 替换我的图标,但也没有显示。
附加信息:
我在 moveIcon class 中注释掉了 setLocation(),因为在我将 JPanel 的布局设置为 null 之前(它不再以这种方式设置)。 (我想把图标放在特定的位置,但我现在不担心这个)
BoxGame 是一个 class,它处理有关玩家位置的信息,例如当前的 X 和 Y 值。应该不会影响显示。
MovingPlayerIcon 是一个 class,它根据参数绘制带有颜色的图标。同样,我认为这不会影响显示,因为我尝试用普通文本 JLabel 替换图标,但那里也没有任何反应。
那么,知道为什么 JLabel 没有出现吗?
我在 java 编程已经一年多了。我真的很感谢你的时间和帮助。如果您需要任何其他信息,请告诉我(我已尽力提供具体信息)。非常感谢!
因此,您需要在按下 'A' 键时绘制标签。
您可以使用 'paintComponent' 方法在所需位置和所需时间简单地绘制标签,而不是使用您的方法 'moveIcon'。
在 class..
中声明这些变量
boolean paint = false;
int x = 100;
int y = 100;
现在,将以下代码行添加到您的 'paintComponent' 方法
如果(油漆==真){
g.drawString("Your Desired Text",x,y);
}
将'KeyInputHandler'class作为'Draw Game Board'class的内层class
现在,在您的 'keyPressed' 方法中,当按下 'A' 键时,执行以下代码行
DrawGameBoard.this.paint = 真;
DrawGameBoard.this.x = 100;
DrawGameBoard.this.y = 100;
重绘();
我已经做了很多研究,但我仍然不确定如何解决这个问题。我正在尝试制作游戏,在其中我需要一个图标显示在屏幕上的不同位置。现在,我只是想让一个图标可见。
我有一个 class 处理来自键盘的输入(名为 KeyInputHandler),另一个 class (名为 DrawGameBoard)创建 window 和背景。在 DrawGameBoard 中,我有一个名为 moveIcon 的方法,它应该显示一个图标。
public class DrawGameBoard extends JPanel
{
public static DrawGameBoard panel = new DrawGameBoard();
public static JFrame window = new JFrame("Fill the Boxes");
public static Container c = window.getContentPane();
public void moveIcon(int x, int y, JLabel label)
{
c.add(label);
//label.setLocation(x,y);
c.validate();
c.repaint();
System.out.println("tried to show a label");
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.GRAY);
g.fillRoundRect(5, 5, 585, 585, 15, 15);
for(int x = 25; x < 515; x+=79)
{
for(int y = 25; y< 515; y+=79)
{
g.setColor(Color.BLACK);
g.fillRect(x, y, 68, 68);
g.setColor(Color.WHITE);
g.fillRect(x+10, y+10, 48, 48);
}
}
}
public static void main(String[] args)
{
KeyInputHandler k = new KeyInputHandler();
//create the window
//JFrame window = new JFrame("Fill the Boxes");
window.setBounds(0, 0, 600, 630);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setResizable(false);
//create the panel
//DrawGameBoard panel = new DrawGameBoard();
//Container c = window.getContentPane();
panel.setBackground(new Color(0,222,222));
panel.addKeyListener(k);
panel.setFocusable(true);
//panel.setLayout(null);//doesnt use default locations
c.add(panel);
window.setVisible(true);
}
}
}
这在 KeyInputHandler 中被调用
public void keyPressed(KeyEvent e)
{
System.out.println("keyPressed: ");
BoxGame b = new BoxGame();
DrawGameBoard d = new DrawGameBoard();
//create icons to be moved
MovingPlayerIcon icon1 = new MovingPlayerIcon(1,0);
JLabel p1fill = new JLabel();
p1fill.setIcon(icon1);
////////////////////////////////////////////////////////////////
//controls for p1
if (e.getKeyCode() == KeyEvent.VK_A) {
if(b.getX1()>0){
b.setPos(b.getX1()-1, b.getY1(), 1);
}
System.out.println("A");
d.moveIcon(0,0,p1fill);
}
////////////////////////////////////////////////////////////////
}
因此,当我按下 'A' 键时,会调用 moveIcon 方法。我知道正在调用 moveIcon 方法,因为在按下 A 键时,将打印 "A" 并打印 "tried to show a label" 。我已经尝试用普通文本 JLabel 替换我的图标,但也没有显示。
附加信息:
我在 moveIcon class 中注释掉了 setLocation(),因为在我将 JPanel 的布局设置为 null 之前(它不再以这种方式设置)。 (我想把图标放在特定的位置,但我现在不担心这个)
BoxGame 是一个 class,它处理有关玩家位置的信息,例如当前的 X 和 Y 值。应该不会影响显示。
MovingPlayerIcon 是一个 class,它根据参数绘制带有颜色的图标。同样,我认为这不会影响显示,因为我尝试用普通文本 JLabel 替换图标,但那里也没有任何反应。
那么,知道为什么 JLabel 没有出现吗?
我在 java 编程已经一年多了。我真的很感谢你的时间和帮助。如果您需要任何其他信息,请告诉我(我已尽力提供具体信息)。非常感谢!
因此,您需要在按下 'A' 键时绘制标签。 您可以使用 'paintComponent' 方法在所需位置和所需时间简单地绘制标签,而不是使用您的方法 'moveIcon'。 在 class..
中声明这些变量boolean paint = false;
int x = 100;
int y = 100;
现在,将以下代码行添加到您的 'paintComponent' 方法
如果(油漆==真){ g.drawString("Your Desired Text",x,y); }
将'KeyInputHandler'class作为'Draw Game Board'class的内层class
现在,在您的 'keyPressed' 方法中,当按下 'A' 键时,执行以下代码行
DrawGameBoard.this.paint = 真; DrawGameBoard.this.x = 100; DrawGameBoard.this.y = 100; 重绘();