为什么Jlabel未被添加到Jframe中?

发布于 2025-01-25 04:55:43 字数 2572 浏览 2 评论 0原文

我尝试从jlabel创建火箭,还向其添加了keyListener。但是,当我将其添加到JPanel中时,火箭不会在面板中显示。

但是,如果我仅运行标签,火箭将显示并可以移动。

Jlabel

package boss;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;

public class Label  extends JLabel implements KeyListener{
Image image;
Image background;
//int x=0;
//int y=0;
Label(){

    image=new ImageIcon("rocket.png").getImage();
    background=new ImageIcon("space.jpg").getImage();
}

public void paint(Graphics g) {
    Graphics2D g2D =(Graphics2D)g;
    //g2D.drawImage(background, x, y, null);

g2D.drawImage(image, 0, 0, 60, 90, null);
}

@Override
public void keyTyped(KeyEvent e) {
    //keyTyped = Invoked when a key is typed. Uses KeyChar, char output
    switch(e.getKeyChar()) {
        case 'a': this.setLocation(this.getX()-10, this.getY());
        
            break;
        case 'w': this.setLocation(this.getX(), this.getY()-10);
            break;
        case 's': this.setLocation(this.getX(), this.getY()+10);
            break;
        case 'd': this.setLocation(this.getX()+10, this.getY());
            break;
    }
}

@Override
public void keyPressed(KeyEvent e) {
    //keyPressed = Invoked when a physical key is pressed down. Uses KeyCode, int output
    switch(e.getKeyCode()) {
    case 37: this.setLocation(this.getX()-10, this.getY());
        break;
    case 38: this.setLocation(this.getX(), this.getY()-10);
        break;
    case 39: this.setLocation(this.getX()+10, this.getY());
        break;
    case 40: this.setLocation(this.getX(), this.getY()+10);
        break;
}
}

@Override
public void keyReleased(KeyEvent e) {
    //keyReleased = called whenever a button is released
    System.out.println("You released key char: " + e.getKeyChar());
    System.out.println("You released key code: " + e.getKeyCode());
}
}

Jpanel

package boss;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class Panel extends JPanel{
Image background;

Label label=new Label();
Panel(){
background=new ImageIcon("space.jpg").getImage();
this.setLayout(null);

this.add(label);
}

public void paint(Graphics g) {
Graphics2D g2D =(Graphics2D)g;

g2D.drawImage(background, 0, 0, 900, 720, null);
}
}

I have tried creating a rocket from a JLabel and also added a KeyListener to it. But when I add it to the JPanel, the rocket does not get displayed in the panel.

But if I solely run the label, the rocket is showing and can be moved.

JLabel

package boss;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;

public class Label  extends JLabel implements KeyListener{
Image image;
Image background;
//int x=0;
//int y=0;
Label(){

    image=new ImageIcon("rocket.png").getImage();
    background=new ImageIcon("space.jpg").getImage();
}

public void paint(Graphics g) {
    Graphics2D g2D =(Graphics2D)g;
    //g2D.drawImage(background, x, y, null);

g2D.drawImage(image, 0, 0, 60, 90, null);
}

@Override
public void keyTyped(KeyEvent e) {
    //keyTyped = Invoked when a key is typed. Uses KeyChar, char output
    switch(e.getKeyChar()) {
        case 'a': this.setLocation(this.getX()-10, this.getY());
        
            break;
        case 'w': this.setLocation(this.getX(), this.getY()-10);
            break;
        case 's': this.setLocation(this.getX(), this.getY()+10);
            break;
        case 'd': this.setLocation(this.getX()+10, this.getY());
            break;
    }
}

@Override
public void keyPressed(KeyEvent e) {
    //keyPressed = Invoked when a physical key is pressed down. Uses KeyCode, int output
    switch(e.getKeyCode()) {
    case 37: this.setLocation(this.getX()-10, this.getY());
        break;
    case 38: this.setLocation(this.getX(), this.getY()-10);
        break;
    case 39: this.setLocation(this.getX()+10, this.getY());
        break;
    case 40: this.setLocation(this.getX(), this.getY()+10);
        break;
}
}

@Override
public void keyReleased(KeyEvent e) {
    //keyReleased = called whenever a button is released
    System.out.println("You released key char: " + e.getKeyChar());
    System.out.println("You released key code: " + e.getKeyCode());
}
}

JPanel

package boss;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class Panel extends JPanel{
Image background;

Label label=new Label();
Panel(){
background=new ImageIcon("space.jpg").getImage();
this.setLayout(null);

this.add(label);
}

public void paint(Graphics g) {
Graphics2D g2D =(Graphics2D)g;

g2D.drawImage(background, 0, 0, 900, 720, null);
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文