我是java GUI的新手,我正在努力学习它.我想自动在屏幕上移动一个圆圈(即不能通过按任何键或执行任何其他操作).我找到了一种通过做一些动作来移动它的方法,但这不是我需要的.请问有谁可以告诉我最简单的方法吗?
我想在以下代码中删除动作侦听器,以便圆圈自动移动:
public class MyFirstGraphics extends JFrame {
int x = 100;
int y = 100;
MyFirstGraphics() {
super("Circle");
setSize(800, 800);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setBackground(Color.pink);
JButton f = new JButton("circle");
f.addActionListener(new re());
add(f, BorderLayout.NORTH);
}
private class re implements ActionListener {
public void actionPerformed(ActionEvent e) {
for (int i = 1; i < 50; i++) {
x++;
y++;
repaint();
}
}
}
public void paint(Graphics g) {
super.paint(g);
g.drawOval(x, y, 100, 100);
}
public static void main(String[] args) {
MyFirstGraphics l …Run Code Online (Sandbox Code Playgroud)