java线程和画图_java多线程画图,界面空白
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
尝试过使用事件分发线程,但还是空白,线程确实是运行着的。
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class test1 extends JPanel {
public void paint(Graphics g){
Thread t1=new Thread(new Runnable(){
public void run(){
g.fillRect(50, 50, 50, 50);
for(int i=0;;i++){
g.setColor(Color.BLACK);
g.fillRect(100+i*20,65,20,20);
try {
Thread.sleep(100); }
catch (InterruptedException e) {
e.printStackTrace(); }
g.setColor(Color.WHITE);
g.fillRect(100+i*20,65,20,20);
if((100+(i+1)*20)>=1500){
System.out.println("over1");
break; }
}}});
Thread t2=new Thread(){
public void run(){
g.fillRect(50, 500, 50, 50);
for(int i=0;;i++){
g.setColor(Color.BLACK);
g.fillRect(100+i*20,515,20,20);
try {
Thread.sleep(100); }
catch (InterruptedException e) {
e.printStackTrace(); }
g.setColor(Color.WHITE);
g.fillRect(100+i*20,515,20,20);
if((100+(i+1)*20)>=1500){
System.out.println("over2");
break; }
}}};
t1.start();
t2.start();
// SwingUtilities.invokeLater(t1);
// SwingUtilities.invokeLater(t2);
}
public test1() {
setBounds(50,50,1500,1000);
setVisible(true);
repaint();}
public static void main(String[] args) { new test1();}
}
多线程为什么不行呢
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 尝试过使用事件分发线程,但还是空白,线程确实是运行着的。 import java.awt.Color; import java.awt.Graphics; import javax.swing.JPanel; import javax.swing.SwingUtilities; public class test1 extends JPanel { public void paint(Graphics g){ Thread t1=new Thread(new Runnable(){ public void run(){ g.fillRect(50, 50, 50, 50); for(int i=0;;i++){ g.setColor(Color.BLACK); g.fillRect(100+i*20,65,20,20); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } g.setColor(Color.WHITE); g.fillRect(100+i*20,65,20,20); if((100+(i+1)*20)>=1500){ System.out.println("over1"); break; } }}}); Thread t2=new Thread(){ public void run(){ g.fillRect(50, 500, 50, 50); for(int i=0;;i++){ g.setColor(Color.BLACK); g.fillRect(100+i*20,515,20,20); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } g.setColor(Color.WHITE); g.fillRect(100+i*20,515,20,20); if((100+(i+1)*20)>=1500){ System.out.println("over2"); break; } }}}; t1.start(); t2.start(); // SwingUtilities.invokeLater(t1); // SwingUtilities.invokeLater(t2); } public test1() { setBounds(50,50,1500,1000); setVisible(true); repaint();} public static void main(String[] args) { new test1();} } 多线程为什么不行呢