嗨,我正在尝试让一个红色框出现在 JPanel 的底部。我希望这个框移动到屏幕的一个角落并停止然后开始向另一个方向移动,但是我无法让框停止以下是我一直在使用的代码
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.Timer;
public class JavaApplication13 extends JPanel {
public static void main(String[] args) {
JFrame rough = new JFrame("Panamr");
rough.setVisible(true);
rough.setLocation(1, 1);
rough.setSize(500, 500);
rough.setContentPane(mamals);
}
public static int iomega(int x, int y) {
if (y == 1) {
diget = -5;
time.stop();
}
if (y == 0) {
diget = 5;
}
return diget;
}
static JavaApplication13 mamals = new JavaApplication13();
JavaApplication13() {
setBackground(Color.red);
}
static int oy = 400;
static int ox = 200;
static int diget;
static Timer time = new Timer(100, new ActionListener() {
public int xy = 1;
@Override
public void actionPerformed(ActionEvent e) {
iomega(ox, xy);
if (ox == 500) {
xy = 1;
}
if (ox == 0) {
xy = 0;
}
ox = ox - iomega(ox, oy);
/*if(ox!=500){
ox=ox-diget;
if(ox==0){
diget=-5;}
else {
diget=5;
}
}*/
}
});
boolean test = true;
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.black);
g.fillRect(ox, oy, 60, 60);
time.start();
repaint();
}
}