你能帮我用这个java程序吗?
我刚开始使用swing和events 我想要的是一个基本的程序,在窗口中有一个按钮,当你点击按钮时,一个椭圆形会在屏幕上移动一段时间,就像我想要动画一样。 这就是我为此所做的你能帮我用这个java程序吗?,java,swing,Java,Swing,我刚开始使用swing和events 我想要的是一个基本的程序,在窗口中有一个按钮,当你点击按钮时,一个椭圆形会在屏幕上移动一段时间,就像我想要动画一样。 这就是我为此所做的 package testmode; import java.awt.BorderLayout; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.s
package testmode;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Ainmationtester implements ActionListener {
JFrame frame;
JButton Button;
int x = 30, y = 30;
Ainmationtester tester = new Ainmationtester();
Ainmationtester.MyDrawPanels test = tester.new MyDrawPanels();
public static void main(String[] args) {
// TODO Auto-generated method stubc
Ainmationtester test = new Ainmationtester();
test.go();
}
public void go() {
frame = new JFrame("Aniamtor");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setVisible(true);
Button = new JButton("CLick me to Animate");
Button.addActionListener(this);
frame.getContentPane().add(BorderLayout.NORTH, Button);
frame.getContentPane().add(BorderLayout.CENTER, test);
}
public class MyDrawPanels extends JPanel {
public void paintComponent(Graphics g) {
g.fillOval(x, y, 10, 10);
}
}
public void actionPerformed(ActionEvent event) {
for (int i = 0; i < 200; i++){
test.repaint();
x++;
y++;
}
}
}
我根据您的代码编写了一个示例
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Ainmationtester implements ActionListener {
JFrame frame;
JButton Button;
int x = 30, y = 30;
MyDrawPanels draw = new MyDrawPanels();
public static void main(String[] args) {
Ainmationtester test = new Ainmationtester();
test.go();
}
public void go() {
frame = new JFrame("Aniamtor");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
Button = new JButton("CLick me to Animate");
Button.addActionListener(this);
frame.getContentPane().add(BorderLayout.NORTH, Button);
frame.getContentPane().add(BorderLayout.CENTER, draw);
frame.setVisible(true);
}
public class MyDrawPanels extends JPanel {
private static final long serialVersionUID = 1L;
public void paintComponent(Graphics g) {
g.fillOval(x, y, 50, 50);
}
}
public void actionPerformed(ActionEvent event) {
x += 5;
y += 5;
frame.repaint();
}
}
最后的跑步截图如下
希望这有帮助这就是我为此所做的。。嗯。。好啊你在哪里被困的?你能帮我用这个java程序吗?当然可以,但你需要明确自己的处境,并提出更具体的问题。听到这听起来像是你能完成我的代码吗?你能指定你的问题在哪里吗?我想他想要一个简单的线程/计时器,带有一个for/while循环,可以为球设置动画。如果你能向他解释你改变了什么,以及为什么他的代码不起作用,那也太好了。因为否则,他不会从中学到任何东西。除了他真的将他的代码与你的代码进行比较并查找错误,但你不能指望他会这么做,我想:谢谢你给我时间。但是你能告诉我我的程序有什么问题吗?错误是什么。我不能在eventhandler方法中运行循环吗?我不能在循环中运行重新绘制吗?@NitinSekhar你的循环不会产生任何可见的动画。如果你用它的话,那就太快了。代码没有运行的主要错误是,由于行Ainmationtester tester=new Ainmationtester;,导致出现StackOverflower错误;。基本上,每次创建一个新的AinMatiContaster实例时,都会创建一个新的AinMatiContaster实例;i<200;i++{Graphics g=draw.getGraphics;g.clearRect0,0,draw.getWidth,draw.getHeight;draw.updateg;}//frame.repaint;当您需要repiant时,首先确保清除图形。