package programming;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class GuiMultiThread extends JFrame { public GuiMultiThread()
{
this.add(new MultiThreadPanel());
this.setSize(1000, 500);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new GuiMultiThread();
Thread thread=new Thread(new MultiThreadPanel());
thread.start();
}}
class MultiThreadPanel extends JPanel implements Runnable
{
double x1,x2=0,y=0,t=0;
public void paintComponent(Graphics g)
{
super.paintComponent(g);
y=9.8*t*t/2;
x2=26*t;
g.fillOval((int)x1,(int)y,40,40);
g.fillOval((int)x2, (int)y, 40, 40); }
public void run()
{
try{
while(true){
Thread.sleep(50);
t+=0.05;
this.repaint();
}
}catch(InterruptedException i)
{i.printStackTrace();}
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class GuiMultiThread extends JFrame { public GuiMultiThread()
{
this.add(new MultiThreadPanel());
this.setSize(1000, 500);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new GuiMultiThread();
Thread thread=new Thread(new MultiThreadPanel());
thread.start();
}}
class MultiThreadPanel extends JPanel implements Runnable
{
double x1,x2=0,y=0,t=0;
public void paintComponent(Graphics g)
{
super.paintComponent(g);
y=9.8*t*t/2;
x2=26*t;
g.fillOval((int)x1,(int)y,40,40);
g.fillOval((int)x2, (int)y, 40, 40); }
public void run()
{
try{
while(true){
Thread.sleep(50);
t+=0.05;
this.repaint();
}
}catch(InterruptedException i)
{i.printStackTrace();}
}
}
import java.awt.event.*;
import javax.swing.*;public class GuiMultiThread extends JFrame { public GuiMultiThread()
{
MultiThreadPanel display = new MultiThreadPanel();
this.add(display);
this.setSize(1000, 500);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
new Thread(display).start();
} public static void main(String[] args) {
new GuiMultiThread();
}
private static class MultiThreadPanel extends JPanel implements Runnable
{
double x1,x2=0,y=0,t=0; @Override public void paintComponent(Graphics g)
{
super.paintComponent(g);
y= 9.8*t*t/2;
x2 = 26*t;
g.fillOval((int)x1,(int)y,40,40);
g.fillOval((int)x2, (int)y, 40, 40);
} @Override public void run()
{
try{
while(true){
Thread.sleep(50);
t += 0.05;
this.repaint();
}
}catch(InterruptedException i)
{
i.printStackTrace();
}
}
}
}
Thread thread=new Thread(new MultiThreadPanel());
thread.start(); 这两句放在构造函数里面?