[英]Java moving a circle in a gui with arrow keys
I am trying to move a circle left with a keyEvent. 我正在尝试通过keyEvent向左移动一个圆圈。 So far, the circle is drawn on the window but it does not move left! 到目前为止,圆已在窗口上绘制,但不会向左移动! I feel like the problem is where I add the Window() constructor to the container. 我觉得问题是我在容器中添加了Window()构造函数。 The is no output on the console to tell me that it is working. 在控制台上没有输出告诉我它正在工作。 So I dont think it even reaches the KeyEvent class. 因此,我认为它甚至不会到达KeyEvent类。 Here is my code: 这是我的代码:
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import javax.swing.*;
public class Window extends JPanel {
private static Ellipse2D.Double circle;
public Window() {
super();
int width = 400;
int height = 400;
circle = new Ellipse2D.Double(0.5 * width, 0.9 * height,
0.1 * width, 0.05 * height);
addKeyListener(new MoveCircle());
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponents(g);
Graphics2D brush = (Graphics2D) g;
int width = getWidth();
int height = getHeight();
g.clearRect(0, 0, width, height);
brush.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
brush.draw(circle);
}
public class MoveCircle implements KeyListener {
@Override
public void keyPressed(KeyEvent e) {
System.out.println("Working on top!");
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
System.out.println("Working on bottom!");
circle.x++;
repaint();
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
}
public static void main(String[] args) {
Window window = new Window();
JFrame frame = new JFrame();
Container container = frame.getContentPane();
container.add(new Window());
frame.addKeyEvent(window.new MoveCircle());
frame.setSize(800, 700);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
}
}
Actually what is happening is this, you are adding Window
to the JFrame
, but the focus lies with the JFrame
, so when you type on your Keyboard
that thing goes to the JFrame
not the KeyListener
attached to the Window
class. 实际上,这是在向JFrame
添加Window
,但是焦点在JFrame
,因此,当您在Keyboard
上键入内容时,该内容将移至JFrame
而不是附属于Window
类的KeyListener
。 So in order to get over it, you simply have to call requestFocusInWindow()
on the Window
class's Object. 因此,为了克服它,您只需要在Window
类的Object上调用requestFocusInWindow()
。 Try this code, I had done some modification regarding EDT and stuff. 试试这个代码,我已经对EDT和东西做了一些修改。
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import javax.swing.*;
public class Window extends JPanel {
private static Ellipse2D.Double circle;
private JFrame frame;
public Window() {
super();
int width = 400;
int height = 400;
circle = new Ellipse2D.Double(0.5 * width, 0.9 * height,
0.1 * width, 0.05 * height);
}
public Dimension getPreferredSize()
{
return (new Dimension(frame.getWidth(), frame.getHeight()));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponents(g);
Graphics2D brush = (Graphics2D) g;
int width = getWidth();
int height = getHeight();
g.clearRect(0, 0, width, height);
brush.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
brush.draw(circle);
}
public class MoveCircle implements KeyListener {
@Override
public void keyPressed(KeyEvent e) {
System.out.println("Working on top!");
if (e.getKeyCode() == Event.ENTER) {
System.out.println("Working on bottom!");
double newX = circle.x - 1;
circle.x = newX;
repaint();
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
}
private void createAndDisplayGUI(Window window)
{
frame = new JFrame();
Container container = frame.getContentPane();
container.add(window);
window.addKeyListener(new MoveCircle());
frame.setSize(800, 700);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
window.requestFocusInWindow();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
Window window = new Window();
window.createAndDisplayGUI(window);
}
});
}
}
Only the focussed component will get key events. 只有关注的组件才能获得关键事件。 You need to call requestFocus() at some point. 您需要在某个时候调用requestFocus()。
A solution would be, to add the KeyListener to the JFrame. 一种解决方案是将KeyListener添加到JFrame。 This way all the key strokes will throw an event, when the JFrame has the focus. 这样,当JFrame获得焦点时,所有按键都会引发一个事件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.