java 刷新jpanel,关于JPanel的刷新有关问题.

Java codeimport java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Dimension;

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JDialog;

import javax.swing.JPanel;

import javax.swing.SwingUtilities;

public class forLP extends JDialog implements ActionListener{

JPanel panel_total ;

JPanel panel_center ;

JPanel panel_north;

public static void main(String[] args) throws InterruptedException{

new forLP().doIt();

}

public forLP(){

super();

}

public void doIt() throws InterruptedException {

panel_total = new JPanel(new BorderLayout());

panel_north = getNorthPanel();

panel_total.add(panel_north,BorderLayout.NORTH);

panel_center = new JPanel();

panel_total.add(panel_center,BorderLayout.CENTER);

this.add(panel_total);

this.setSize(500, 500);

this.setVisible(true);

}

private void setCenterPanel() throws InterruptedException {

int x = 250 ;

int y = 250 ;

int flag = 1 ;

panel_center.setLayout(null);

JButton jb = getButton(x,y);

jb.setBackground(getRandomColor());

panel_center.add(jb);

panel_center.updateUI();

/**

* 新建两个线程,每割1秒刷新出一个按钮

* 但是为什么总是到第二秒的时候两个按钮一起出来呢。。

*/

SwingUtilities.invokeLater(new Runnable(){

public void run() {

try {

Thread.sleep(1000);

JButton jb = getButton(250, 300);

panel_center.add(jb);

panel_center.repaint();

} catch (InterruptedException e) {

e.printStackTrace();

}

}

});

SwingUtilities.invokeLater(new Runnable(){

public void run() {

try {

Thread.sleep(2000);

JButton jb = getButton(350, 300);

panel_center.add(jb);

panel_center.repaint();

} catch (InterruptedException e) {

e.printStackTrace();

}

}

});

// for( int i = 1 ; i < 2 ; i ++){

// if( i%2 == 1){

// for(int j = 0 ; j < i ; j ++){

// y = y + 20;

// addButton(x,y,flag);

// flag ++ ;

// }

// for(int k = 0 ; k < i ; k ++){

// x = x + 20 ;

// addButton(x,y,flag);

// flag ++ ;

// }

// }else {

// for(int j = 0 ; j < i ; j ++){

// y = y - 20;

// addButton(x,y,flag);

// flag ++ ;

// }

// for(int k = 0 ; k < i ; k ++){

// x = x - 20 ;

// addButton(x,y,flag);

// flag ++ ;

// }

// }

// }

}

public Color getRandomColor(){

Color color = new Color(

(new Double(Math.random() * 128)).intValue() + 128,

(new Double(Math.random() * 128)).intValue() + 128,

(new Double(Math.random() * 128)).intValue() + 128);

return color ;

}

private void addButton(final int x, final int y, final int flag) {

SwingUtilities.invokeLater(new Runnable(){

public void run() {

try {

Thread.sleep(1000*flag);

System.out.println("执行第"+flag+" 坐标:"+x+" " +y);

JButton jb = getButton(x, y);

panel_center.add(jb);

panel_center.repaint();

panel_center.updateUI();

} catch (InterruptedException e) {

e.printStackTrace();

}

}

});

}

private JButton getButton(int x,int y) {

JButton jb = new JButton();

jb.setPreferredSize(new Dimension(10,10));

jb.setBounds(x, y, 10, 10);

jb.setBackground(getRandomColor());

jb.setVisible(true);

return jb;

}

private JPanel getNorthPanel() {

JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));

JButton btn_1 = new JButton("点我");

btn_1.addActionListener(this);

panel.add(btn_1);

return panel;

}

@Override

public void actionPerformed(ActionEvent e) {

try {

setCenterPanel();

} catch (InterruptedException e1) {

e1.printStackTrace();

}

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值