public void layoutContainer (Container target)  {
        synchronized (target.getTreeLock()) {
            Insets insets = target.getInsets();
            int maxwidth = target.getSize().width - (insets.left + insets.right);
            int nmembers = target.getComponentCount();
            if (maxwidth < 0 || nmembers == 0) return;            int rowh = 0, lastVisible = -1;
            for (int i = nmembers - 1; i >= 0; i--) {
                Component m = target.getComponent(i);
                if (m.isVisible()) {
                    if (lastVisible == -1) lastVisible = i;
                    Dimension d = m.getPreferredSize();
                    if (d.height > rowh) rowh = d.height;
                }
            }       
            if (lastVisible == -1 || rowh == 0) return;            int x = 0, y = insets.top + vgap;
            for (int i = 0 ; i < nmembers ; i++) {
                Component m = target.getComponent(i);
                if (m.isVisible()) {
                    Dimension d = m.getPreferredSize();
                    if (i == lastVisible) d.width = maxwidth - x;
                    m.setBounds(x, y + (rowh - d.height) / 2, d.width, d.height);
                    x += hgap + d.width;
                }
            }
        }
    }
把这个变成多线程,新手求帮助

解决方案 »

  1.   

    Thread thread= new Thread("这里是你要进行多线程的那个实例化对象");
    thread.start();
    thread.join();
      

  2.   

    public class ThreadA extends Thread{
         public void run(){
             //do anything
         }
    }-----------下面是把ThreadA进行多线程------------
    ThreadA a= new ThreadA()Thread thread = new Thread(a)
    thread.start();
    thread.join();
      

  3.   

    这是我写的代码:
    public class GuiLastWideLayou{
    public void layoutContainer (Container target) {
        Thread t = new LayoutContainerThread(target);
        t.start();
      }
    }
    class LayoutContainerThread extends Thread{
       private Container target;
       public LayoutContainerThread(Container t){
         target = t;
       }
       public void run(){   Insets insets = target.getInsets();
       int maxwidth = target.getSize().width - (insets.left + insets.right);
      int nmembers = target.getComponentCount();
      if (maxwidth < 0 || nmembers == 0) return;  int rowh = 0, lastVisible = -1;
      for (int i = nmembers - 1; i >= 0; i--) {
      Component m = target.getComponent(i);
      if (m.isVisible()) {
      if (lastVisible == -1) lastVisible = i;
      Dimension d = m.getPreferredSize();
      if (d.height > rowh) rowh = d.height;
      }
      }   
      if (lastVisible == -1 || rowh == 0) return;  int x = 0, y = insets.top + vgap;
      for (int i = 0 ; i < nmembers ; i++) {
      Component m = target.getComponent(i);
      if (m.isVisible()) {
      Dimension d = m.getPreferredSize();
      if (i == lastVisible) d.width = maxwidth - x;
      m.setBounds(x, y + (rowh - d.height) / 2, d.width, d.height);
      x += hgap + d.width;
      }
      }
      }
    }
    }
      

  4.   

    这是另一种方法:
    public class GuiLastWideLayou{
    public void layoutContainer (Container target) {
      LayoutContainerThread t = new LayoutContainerThread(target);
      Thread thread = new Thread(t);
      t.start();
      }
    }
    class LayoutContainerThread implements Runnable{
      private Container target;
      public LayoutContainerThread(Container t){
      target = t;
      }
      public void run(){  Insets insets = target.getInsets();
      int maxwidth = target.getSize().width - (insets.left + insets.right);
      int nmembers = target.getComponentCount();
      if (maxwidth < 0 || nmembers == 0) return;  int rowh = 0, lastVisible = -1;
      for (int i = nmembers - 1; i >= 0; i--) {
      Component m = target.getComponent(i);
      if (m.isVisible()) {
      if (lastVisible == -1) lastVisible = i;
      Dimension d = m.getPreferredSize();
      if (d.height > rowh) rowh = d.height;
      }
      }   
      if (lastVisible == -1 || rowh == 0) return;  int x = 0, y = insets.top + vgap;
      for (int i = 0 ; i < nmembers ; i++) {
      Component m = target.getComponent(i);
      if (m.isVisible()) {
      Dimension d = m.getPreferredSize();
      if (i == lastVisible) d.width = maxwidth - x;
      m.setBounds(x, y + (rowh - d.height) / 2, d.width, d.height);
      x += hgap + d.width;
      }
      }
      }
    }
    }
    但是都不能通过编译
      

  5.   

    你问的是什么意思 啊。你方法中使用了,同步,就是为了防止多线程数据安全问题。
    如果做web开发,本身就是多线程,你的代码这样可以了,不需要改。
      

  6.   


    是这样的,我现在要的是将这个layoutContainer  这个方法的性能提高2X,当我点击某个按钮的时候,会执行到这个方法,执行一次这个方法弹出一个窗口,一共要弹出2个,在运行的时候2个窗口出来有个时间差,我要的就是把这个时间差降低,所以我就用多线程。各位支个招啊
      

  7.   


    我不知道你是想干什么, 但是既然你知道编译不过,难道不知道为什么编译不过么??很明显的错误啊.LayoutContainerThread t = new LayoutContainerThread(target);
    Thread thread = new Thread(t);
    t.start()既然你都把t对象丢到thread里面去了,为什么不是thread.start(),而要写成t.start()呢??
      

  8.   

    对,是thread.start(), 打错了, 但是还是不能通过编译