我有以下程序用于制作 JFrame,在框架上粘贴一个按钮,然后单击“启动按钮”以观看该按钮以抛物线弧线滑过框架。但是我的按钮没有出现。我已将按钮动作移动到一个单独的线程,并在 actionlistener 中调用该线程,但这似乎不起作用,因为我只看到按钮(要移动)出现然后显示在其端点。
package kinetic.energy.viewer;
import javax.swing.*;
import java.applet.*;
import java.awt.event.*;
import java.math.*;
public class KineticEnergyViewer {
public static void main(String[] args) {
final JFrame viewframe = new JFrame("Welcome to Gravitational Launcher");
viewframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
viewframe.setSize(700,500);
viewframe.setVisible(true);
viewframe.setLayout(null);
JButton launchbutton = new JButton("Launch");
launchbutton.setSize(100,100);
launchbutton.setLocation(600, 400);
viewframe.add(launchbutton);
final JButton rock = new JButton("YES!");
rock.setSize(50,50);
final GravitationalThread t = new GravitationalThread(9.8, (Math.PI)/4, 100, 500, 0, viewframe, rock);
final Dumbthread g = new Dumbthread();
launchbutton.addActionListener(new ActionListener()
{
//GravitationalThread t = new GravitationalThread(9.8, (Math.PI)/4, 100, 500, 0, viewframe, rock);
public void actionPerformed(ActionEvent e){
g.run();
t.run();
}
});
}
}
--
package kinetic.energy.viewer;
import javax.swing.*;
import java.applet.*;
import java.awt.event.*;
import java.math.*;
public class KineticEnergyViewer {
public static void main(String[] args)
{
final JFrame viewframe = new JFrame("Welcome to Gravitational Launcher");
viewframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
viewframe.setSize(700,500);
viewframe.setVisible(true);
viewframe.setLayout(null);
JButton launchbutton = new JButton("Launch");
launchbutton.setSize(100,100);
launchbutton.setLocation(600, 400);
viewframe.add(launchbutton);
final JButton rock = new JButton("YES!");
rock.setSize(50,50);
final GravitationalThread t = new GravitationalThread(9.8, (Math.PI)/4, 100, 500, 0, viewframe, rock);
final Dumbthread g = new Dumbthread();
launchbutton.addActionListener(new ActionListener()
{
//GravitationalThread t = new GravitationalThread(9.8, (Math.PI)/4, 100, 500, 0, viewframe, rock);
public void actionPerformed(ActionEvent e){
g.run();
t.run();
}
});
}
}
一个可运行的类:
package kinetic.energy.viewer;
import java.lang.Thread.*;
import javax.swing.*;
import java.math.*;
public class GravitationalThread implements Runnable {
double grav, ang, vel,y,x;
JFrame targframe;
JButton targbutton;
public void run()
{
double step = 0.01;
double k = 0;
double yoriginal = y;
double xoriginal = x;
while(y < 600 && x < 800)
{
System.out.println((int)x + " " + (int)y);
y = yoriginal - vel*(Math.sin(ang))*k + (grav/2)*k*k;
x = xoriginal + vel*(Math.cos(ang))*k;
targframe.remove(targbutton);
targbutton.setLocation((int)x, (int)y);
targframe.add(targbutton);
targframe.repaint();
k = k + step;
int j = 0;
int i = 0;
while(j < 10000)
{
while(i < 10000)
{
i++;
}
j++;
}
}
}
GravitationalThread(double gravity, double angle, double velocity, double ypos, double xpos, JFrame whereat, JButton who)
{
targbutton = who; //initialize variables
targframe = whereat;
grav = gravity;
ang = angle;
vel = velocity;
y = ypos;
x = xpos;
targbutton.setLocation((int)x, (int)y);
targframe.add(targbutton);
targframe.validate();
}
}
和我正在尝试使用的空线程:
package kinetic.energy.viewer;
import java.lang.Thread.*;
import javax.swing.*;
import java.math.*;
public class Dumbthread implements Runnable {
Dumbthread()
{
}
public void run()
{
int i = 0;
while(i < 1000)
{
i = i + 1;
}
}
}