gpt4 book ai didi

Java用箭头键在gui中移动一个圆圈

转载 作者:行者123 更新时间:2023-11-29 07:15:47 24 4
gpt4 key购买 nike

我正在尝试使用 keyEvent 向左移动一个圆圈。至此,圆画在了窗口上,但它没有向左移动!我觉得问题出在我将 Window() 构造函数添加到容器的地方。控制台上没有输出告诉我它正在工作。所以我认为它甚至没有到达 KeyEvent 类。这是我的代码:

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);
}
}

最佳答案

实际上发生的事情是这样的,您正在将 Window 添加到 JFrame,但是焦点在 JFrame 上,所以当您在您的 Keyboard 上键入该内容会转到 JFrame 而不是附加到 Window 类的 KeyListener。因此,为了克服它,您只需在 Window 类的对象上调用 requestFocusInWindow()。试试这个代码,我对 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);
}
});
}
}

关于Java用箭头键在gui中移动一个圆圈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9727192/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com