试图移动的矩形箭头键在核心Java中使用JFrames帮助 [英] Trying to move a rectangle with the help of arrow keys in Core Java using JFrames
问题描述
我已经在我试图用箭头键的帮助下向下移动一个矩形的程序。但它是不动的。这里是我的code。任何人都可以请帮我,并告诉它为什么不动?我也曾在按键按下事件添加了一个System.out.print(),但它没有显示在控制台上的任何输出。看来该KeyEvent部分是行不通的。
`进口java.awt.event.KeyAdapter;
进口java.awt.event.KeyEvent中;
进口java.awt.event.KeyListener; 进口javax.swing.ImageIcon中;
进口javax.swing.JFrame中;
进口javax.swing.JLabel中;公共类MovingBoxWithArrowKeys扩展的JFrame
{
JLabel的升;公共MovingBoxWithArrowKeys(字符串名称)
{
超(职称);
L =新的JLabel(新的ImageIcon(下载盒的icon.png));
//l.setLocation(10,10);
l.setBounds(10,10,400,400);
l.setVisible(真);
l.isOptimizedDrawingEnabled();
l.requestFocusInWindow();
//l.requestFocus();
l.addKeyListener(新KeyAdapter()
{
公共无效键pressed(KeyEvent的K)
{
如果(k.getKey code()== KeyEvent.VK_DOWN)
{
l.setLocation(l.getX(),l.getY()+ 1);
重绘();
System.out.print(下来pressed);
}
}
}); setLayout的(NULL);
的setSize(this.getMaximumSize());
setDefaultCloseOperation(EXIT_ON_CLOSE);
的setResizable(假);
调用setVisible(真);
// requestFocus()方法; 。的getContentPane()加(L);
//加(L);
}
公共静态无效的主要(字符串ARGS [])
{
MovingBoxWithArrowKeys M =新MovingBoxWithArrowKeys(移动框键);}
}`
您的KeyListener
需要被添加到的JFrame
,因为它只能与具有实际的焦点的组件。并且那不是你的的JLabel
,是你的的JFrame
。结果
即使你要求与 l.requestFocusInWindow()的重点;在初始化code
你将失去它的的JFrame
,因为它是不是能够保持一个焦点搜索结果的组件之一
所以,简单地改变这样的:
l.addKeyListener(新KeyAdapter(){...}
这个
addKeyListener(新KeyAdapter(){...}
I have made a program in which I am trying to move a rectangle down with the help of arrow keys. But it is not moving. Here is my code. Can anyone please help me out and tell why is it not moving ? I have also added a System.out.print( ) in the key down event but its not showing any output on the console. It seems the keyevent part is not working.
`import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class MovingBoxWithArrowKeys extends JFrame
{
JLabel l ;
public MovingBoxWithArrowKeys(String title)
{
super(title);
l = new JLabel(new ImageIcon("download-box-icon.png"));
//l.setLocation(10 , 10 );
l.setBounds(10 , 10 , 400 , 400 );
l.setVisible(true);
l.isOptimizedDrawingEnabled();
l.requestFocusInWindow();
//l.requestFocus();
l.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent k)
{
if(k.getKeyCode() == KeyEvent.VK_DOWN)
{
l.setLocation(l.getX(), l.getY()+1);
repaint();
System.out.print("Down Pressed");
}
}
});
setLayout(null);
setSize(this.getMaximumSize());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
//requestFocus();
getContentPane().add(l);
//add(l);
}
public static void main(String args[])
{
MovingBoxWithArrowKeys m = new MovingBoxWithArrowKeys("Moving Box With Keys");
}
}`
Your KeyListener
needs to be add to the JFrame
because it only works with the component that has the actual focus. And thats not your JLabel
its your JFrame
.
Even if you request the Focus with l.requestFocusInWindow();
in your initialization code you will lose it to the JFrame
because it is one of the components that is not capable of holding a focus
So simply change this:
l.addKeyListener(new KeyAdapter() {...}
to this
addKeyListener(new KeyAdapter() {...}
这篇关于试图移动的矩形箭头键在核心Java中使用JFrames帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!