一个图形化界面上有两个文本区textarea1和textarea2,开始时在textarea1中显示5条记录,当点击按钮时,将textarea1中的每条记录移到textarea2中。不是一次全部移去,而是当移完一条记录后,停一段时间(比如3秒),这主要是为了可以看到两个文本区里内容的变化。
比如,我从textarea1中移去第一条记录,加入textarea2中。然后停止运行(sleep)三秒,为了看到两个文本区内容的变化。可是现在用了JFrame框架类做图形化界面时,无法动态显示。一直到全部运行完(睡完),才一次性地显示在textarea2中(也就是,先隐形地完成工作,最后给你个惊喜,将五条记录一次显示在textarea2中,变化过程一点也看不见)。可是,用Applet做图形化界面,却又可以。两者方法完全一样,只是一个采用Applet,一个采用JFrame。这到底是为什么???谁能给出用JFrame实现上述问题的代码,高分相赐。

解决方案 »

  1.   

    class Thread1 extends Thread{
      JFrame1 frame;
      public Thread1(JFrame1 frame){
        this.frame = frame;
      }  public void run(){
        frame.move();
      }
    }在JFrame1类里面定义一个move方法,把你的移动代码复制到move里面在你的按钮click里面调用 new Thread1(this).start();这样就行了。
      

  2.   

    先用两个list一个存一个显示,再加上一个setTimer,每隔一段时间就调用一下显示一条信息就可以了
      

  3.   

    对了,有时间请大家帮我看看我的那个读者写者的问题。 
    http://topic.csdn.net/u/20091006/20/e9985d3b-4dea-4293-ad04-977e9cbf86cf.
      

  4.   

    是不是没有repaint()  面板不能自动刷新 等运行结束才显示结果  
    你应该启动一个线程 线程里面repaint(),接着sleep(3000);
    试试吧
      

  5.   


    1楼的方案完全可以的。更具体一点说,就是你可以定义一个move方法,每当点击按钮时,即调用move方法。而move方法的内容是创建一个线程并启动,而该线程的run方法里,每次复制一条记录,然后休眠(sleep) 3秒,接着复制下一条。
      

  6.   

    多了解一下swing吧,
    不要用event-dispatching thread 去做移动的操作, 用SwingWorker 单独起一个线程完成move的操作
      

  7.   

    不能sleep 主线程 需要另开一个线程,做了个例子,看看吧import java.util.Arrays;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class ThreadTest{
        
        public static void main(String [] args){
            ThreadTest pro=new ThreadTest();
            pro.initVariable();
            pro.createFrame();       
        }
        
        private int LEFT=0;
        private int TOP=0;
        private int WIDTH=400;
        private int HEIGHT=300;
            
        JButton moveRightBtn=new JButton(" >> ");
        JButton moveLeftBtn=new JButton(" << ");       
        DefaultListModel leftM=new DefaultListModel();
        DefaultListModel rightM=new DefaultListModel();
        JList leftList=new JList(leftM); 
        JList rightList=new JList(rightM); 
        JScrollPane leftScroll=new JScrollPane(leftList);
        JScrollPane rightScroll=new JScrollPane(rightList);
            
        //初始化
        public void initVariable(){
            Toolkit kit=Toolkit.getDefaultToolkit();
            Dimension dimension=kit.getScreenSize();
            LEFT=(int)(dimension.getWidth()/2-WIDTH/2);
            TOP=(int)(dimension.getHeight()/2-HEIGHT/2);
            
            moveRightBtn.setMargin(new Insets(0,0,0,0));
            moveLeftBtn.setMargin(new Insets(0,0,0,0));
            
            String[] listData={"one","two","three","four","five"};
            for(String data:listData){
                leftM.addElement(data);
            }  
            
            leftScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
            rightScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);                   
        }
        //创建窗体
        public void createFrame(){
            JFrame frame=new JFrame();
            frame.setTitle("JList");
            frame.setLocation(LEFT,TOP);
            frame.setSize(WIDTH,HEIGHT);
            
            Container pane=frame.getContentPane();        
            pane.setLayout(new GridLayout(1,3));                
                    
            pane.add(leftScroll);
            
            JPanel btns=new JPanel();  
            btns.setLayout(new GridLayout(2,1));     
            btns.add(moveRightBtn);               
            btns.add(moveLeftBtn);
            pane.add(btns);
                    
            pane.add(rightScroll);                
            
            //添加动作处理
            BtnListener btnLis=new BtnListener();
            moveRightBtn.setActionCommand("moveRight");
            moveRightBtn.addActionListener(btnLis);
            
            moveLeftBtn.setActionCommand("moveLeft");
            moveLeftBtn.addActionListener(btnLis);
            
            //设置默认值        
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
        //action 事件处理
        public class BtnListener implements ActionListener{
            public void actionPerformed(ActionEvent e){
                Object source=e.getSource();
                if(source instanceof JButton){
                    String cmd=e.getActionCommand();
                    TimeThread thread=new TimeThread();
                    if(cmd.equals("moveRight")){
                        thread.start();
                    }else if(cmd.equals("moveLeft")){
                        Object[] objs=rightList.getSelectedValues();
                        for(Object o:objs){
                            rightM.removeElement(o);
                            leftM.addElement(o);
                        }
                    }
                }
            }
        } 
        class TimeThread extends Thread{
            public void run(){
                Object[] objs=leftList.getSelectedValues();
                for(Object o:objs){
                    leftM.removeElement(o);
                    rightM.addElement(o);
                    try{
                        this.sleep(1000);
                    }catch(Exception eT){
                    }
                }
            }
        }   
    }
      

  8.   

    非常感谢楼上,可是我的意思不是这个。我是单击“>>”时,要让他自己每隔3秒钟将leftM中的一个自动地添加到rightM中。而不是选中哪个,再单击“>>”,才添加到rightM中。