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

}

多线程为什么不行呢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值