为什么Jlabel未被添加到Jframe中?
我尝试从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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论