日期:2014-05-20 浏览次数:21005 次
import 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(); } } }