简体   繁体   中英

Why Won't My Button Move. Need help implementing threads to move JButton

I have the following program that I'm using to make a JFrame, stick a button on the frame, and click "launchbutton" to watch that button glide across the frame in a parabolic arc. However my button does not appear whatsoever. I have moved the button motion to a separate thread and am invoking that thread in the actionlistener but that doesn't seem to work as I only see the button (to be moved) appear and then show up at its end points.

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

}
}

A runnable class:

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

and an empty thread that I'm trying to use:

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;
    }
}
}
g.run();
t.run();

will not cause GravitationalThread t and Dumbthread g to run in two different threads of execution. They will run in the same thread of execution from where they were called. To start them in two separate thread of execution, you need to start two separate threads as below:

launchbutton.addActionListener(new ActionListener()
                {
                //GravitationalThread t = new GravitationalThread(9.8, (Math.PI)/4, 100, 500, 0, viewframe, rock);
                public void actionPerformed(ActionEvent e){
                    new Thread(g).start();
                    new Thread(t).start();
                }
            });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM