2013-10-24 30 views
0

我有以下程序,我正在使用一個JFrame,在框架上貼一個按鈕,然後單擊「launchbutton」以觀察該按鈕在拋物線弧中滑過框架。然而我的按鈕並沒有出現。我已將按鈕動作移至單獨的線程,並在actionlistener中調用該線程,但似乎無法工作,因爲我只看到按鈕(要移動)出現,然後出現在其終點處。爲什麼我的按鈕不會移動。需要幫助實現線程移動JButton

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; 
    } 
} 
} 
+0

再說什麼Debojit說,你也試圖從一個線程,是不是[EDT(HTTP調用Swing代碼://en.wikipedia.org/wiki/Event_dispatching_thread)。這是一個禁忌。 – predi

回答

0
g.run(); 
t.run(); 

將不會導致GravitationalThread tDumbthread g在兩個不同的執行線程中運行。他們將從他們被調用的地方執行相同的執行線程。要在執行兩個單獨的線程啓動它們,你需要開始爲下面兩個獨立的線程:

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(); 
       } 
      }); 
相關問題