我要从客户端传一个文件到服务器去计算,然后再把计算好的文件从服务器传回客户端(传输过程通过socket实现)。这个过程需要点时间,所以想做个进度条。
我是这样设计的:当在客户端选择好要传输的文件后,调用负责传输的socket类,然后再写一个frame类(内容在下面)来实现进度条的功能,类名是 Csocket。在socket类中调用Csocket类.
问题是当传输时显示不出frame里的jlabel和JProgressBar组件,只显示一个空白的frame,但是计算完后可以显示出来,并会显示进度条的值是100/100,而且单独运行Csocket类时可以显示出
请问高手是怎么一回事?(执行步骤的截图在附件,分别命名为1、2、3、4)
Csocket类如下:
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JProgressBar;public class Csocket extends javax.swing.JFrame {  
    public JProgressBar jProgressBar=new JProgressBar();    
    /** Creates new form Csocket */
    public Csocket() {   
        int height,width;
        Dimension dd;
        dd=Toolkit.getDefaultToolkit().getScreenSize();
        initComponents();
        height=this.getHeight();
        width=this.getWidth();
        System.out.println(dd.height+"  "+ dd.width+"  "+height+"  "+width);
        height=(dd.height-height)/2;
        width=(dd.width-width)/2;
        
        this.setLocation(width, height);     
        jProgressBar.setSize(250,25);   
        jProgressBar.setLocation(25, 70);
        jProgressBar.setVisible(true);
        this.add(jProgressBar);  
    }
    private void initComponents() {        jLabel1 = new javax.swing.JLabel();        setTitle("Data is sending, please wait..");        jLabel1.setFont(new java.awt.Font("宋体", 0, 14)); // NOI18N
        jLabel1.setText("Data is sending, please wait...");        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(56, 56, 56)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 239, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(36, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(52, Short.MAX_VALUE))
        );        pack();
    }// </editor-fold>
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        
        java.awt.EventQueue.invokeLater(new Runnable() {            
            public void run() {
           //     new Csocket().add(jProgressBar);
                new Csocket().setVisible(true);   
            }
        });
    }    
    
    private javax.swing.JLabel jLabel1;   
}
调用部分这样写的:
 Csocket pBar = new Csocket();
 pBar.setVisible(true);
 pBar.jProgressBar.setValue(20);    

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【regxx】截止到2008-06-29 20:45:44的历史汇总数据(不包括此帖):
    发帖数:3                  发帖分:120                
    结贴数:3                  结贴分:120                
    未结数:0                  未结分:0                  
    结贴率:100.00%            结分率:100.00%            
    敬礼!
      

  2.   

    标题写错了 想重新编辑 竟然说我没有权限 晕啊
    标题应该是:做一个进度条 但是frame里的组件开始时显示不出来,一直到最后才出来???   
      

  3.   

    给你个现成的,具体怎么用请看main方法中的注释
    如果你想弄的漂亮一点,可以自己发挥一下喽,我项目中用到的效果可以参看http://craky003.51.com
    import java.awt.Dialog;
    import java.awt.Frame;
    import java.awt.Window;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.ComponentAdapter;
    import java.awt.event.ComponentEvent;import javax.swing.JButton;
    import javax.swing.JDialog;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JProgressBar;public class LoadingDialog implements ActionListener
    {
        /**
         * 缺省状态提示
         */
        private static final String DEFAULT_STATUS = "Please Waiting";
        
        /**
         * “线程正在运行”提示框
         */
        private JDialog dialog;    /**
         * 进度条
         */
        private JProgressBar progressBar;    /**
         * 显示提示信息的Label
         */
        private JLabel lbStatus;    /**
         * 取消按钮
         */
        private JButton btnCancel;    /**
         * 父窗体
         */
        private Window parent;
        
        /**
         * 需要关联进度条的线程
         */
        private Thread thread;    /**
         * 提示框的提示信息
         */
        private String statusInfo;    /**
         * 结果提示
         */
        private String resultInfo;
        
        /**
         * 取消时的提示
         */
        private String cancelInfo;
        
        /**
         * 显示一个缺省状态提示且没有结果提示的等待框
         * @param parent 父窗体
         * @param thread 需要关联进度条的线程
         */
        public static void show(Window parent, Thread thread)
        {
            new LoadingDialog(parent, thread, DEFAULT_STATUS, null, null);
        }
        
        /**
         * 显示一个没有结果提示的等待框
         * @param parent 父窗体
         * @param thread 需要关联进度条的线程
         * @param statusInfo 当前线程动作提示信息
         */
        public static void show(Window parent, Thread thread, String statusInfo)
        {
            new LoadingDialog(parent, thread, statusInfo, null, null);
        }
        
        /**
         * 显示一个有结果提示的等待框
         * @param parent 父窗体
         * @param thread 需要关联进度条的线程
         * @param statusInfo 当前线程动作提示信息
         * @param resultInfo 线程结束后的提示信息(若为null则不提示)
         * @param cancelInfo 点击取消按钮后的提示信息(若为null则不提示)
         */
        public static void show(Window parent, Thread thread, String statusInfo, String resultInfo, String cancelInfo)
        {
            new LoadingDialog(parent, thread, statusInfo, resultInfo, cancelInfo);
        }    /**
         * 构造方法
         * @param parent 父窗体
         * @param thread 需要关联进度条的线程
         * @param statusInfo 当前线程动作提示信息
         * @param resultInfo 线程结束后的提示信息(若为null则不提示)
         * @param cancelInfo 点击取消按钮后的提示信息(若为null则不提示)
         */
        private LoadingDialog(Window parent, Thread thread, String statusInfo, String resultInfo, String cancelInfo)
        {
            this.parent = parent;
            this.thread = thread;
            this.statusInfo = statusInfo;
            this.resultInfo = resultInfo;
            this.cancelInfo = cancelInfo;
            initUI();
            startThread();
            dialog.setVisible(true);
        }    private void initUI()
        {
            if(parent instanceof Dialog)
            {
                dialog = new JDialog((Dialog)parent, statusInfo, true);
            }
            else if(parent instanceof Frame)
            {
                dialog = new JDialog((Frame)parent, statusInfo, true);
            }
            else
            {
                dialog = new JDialog((Frame)null, statusInfo, true);
            }        final JPanel mainPane = new JPanel(null);
            progressBar = new JProgressBar();
            lbStatus = new JLabel("<html>" + statusInfo);
            btnCancel = new JButton("Cancel");
            progressBar.setIndeterminate(true);
            btnCancel.addActionListener(this);        mainPane.add(progressBar);
            mainPane.add(lbStatus);
            mainPane.add(btnCancel);        dialog.getContentPane().add(mainPane);
            dialog.setUndecorated(true);
            dialog.setResizable(false);
            dialog.setSize(390, 100);
            dialog.setLocationRelativeTo(parent);
            
            mainPane.addComponentListener(new ComponentAdapter()
            {
                public void componentResized(ComponentEvent e)
                {
                    layout(mainPane.getWidth(), mainPane.getHeight());
                }
            });
        }    private void startThread()
        {
            new Thread()
            {
                public void run()
                {
                    try
                    {
                        thread.start();
                        // 等待事务处理线程结束
                        thread.join();
                    }
                    catch(InterruptedException e)
                    {
                        e.printStackTrace();
                    }
                    finally
                    {
                        // 关闭进度提示框
                        dialog.dispose();
        
                        if(resultInfo != null && !resultInfo.trim().equals(""))
                        {
                            String title = "Info";
                            JOptionPane.showMessageDialog(parent, resultInfo, title, JOptionPane.INFORMATION_MESSAGE);
                        }
                    }
                }
            }.start();
        }    private void layout(int width, int height)
        {
            progressBar.setBounds(20, 20, 350, 15);
            lbStatus.setBounds(20, 50, 350, 25);
            btnCancel.setBounds(width - 85, height - 31, 75, 21);
        }    @SuppressWarnings("deprecation")
        public void actionPerformed(ActionEvent e)
        {
            resultInfo = cancelInfo;
            thread.stop();
        }    public static void main(String[] args) throws Exception
        {
            Thread thread = new Thread()
            {
                public void run()
                {
                    int index = 0;
                    
                    while(index < 5)
                    {
                        try
                        {
                            System.out.println(++index);
                            sleep(1000);
                        }
                        catch(InterruptedException e)
                        {
                            e.printStackTrace();
                        }
                    }
                }
            };
            
            
            //上面的thread就是你要做的事,比如你要传文件,那你就把传文件的代码封闭到一个单独的线程中,然后只要下面这一行就行了
            //具体的参数可以参看方法注释,其实上面那一大堆实现都可以不用去看
            LoadingDialog.show((Frame)null, thread, "Status", "Result", "Cancel");
        }
    }
      

  4.   

    呵呵,随便改改就行了
    import java.awt.Dialog;
    import java.awt.Frame;
    import java.awt.Window;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.ComponentAdapter;
    import java.awt.event.ComponentEvent;import javax.swing.JButton;
    import javax.swing.JDialog;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JProgressBar;public class ProgressDialog implements ActionListener
    {
        /**
         * 缺省状态提示
         */
        private static final String DEFAULT_STATUS = "Please Waiting";    /**
         * “线程正在运行”提示框
         */
        private JDialog dialog;    /**
         * 进度条
         */
        private JProgressBar progressBar;    /**
         * 显示提示信息的Label
         */
        private JLabel lbStatus;    /**
         * 取消按钮
         */
        private JButton btnCancel;    /**
         * 父窗体
         */
        private Window parent;    /**
         * 需要关联进度条的线程
         */
        private Thread thread;    /**
         * 提示框的提示信息
         */
        private String statusInfo;    /**
         * 结果提示
         */
        private String resultInfo;    /**
         * 取消时的提示
         */
        private String cancelInfo;    /**
         * 显示一个缺省状态提示且没有结果提示的等待框
         * @param parent 父窗体
         */
        public static void show(Window parent)
        {
            new ProgressDialog(parent, DEFAULT_STATUS, null, null);
        }    /**
         * 显示一个没有结果提示的等待框
         * @param parent 父窗体
         * @param statusInfo 当前线程动作提示信息
         */
        public static void show(Window parent, String statusInfo)
        {
            new ProgressDialog(parent, statusInfo, null, null);
        }    /**
         * 显示一个有结果提示的等待框
         * @param parent 父窗体
         * @param statusInfo 当前线程动作提示信息
         * @param resultInfo 线程结束后的提示信息(若为null则不提示)
         * @param cancelInfo 点击取消按钮后的提示信息(若为null则不提示)
         * @return 进度条对话框实例
         */
        public static ProgressDialog show(Window parent, String statusInfo, String resultInfo, String cancelInfo)
        {
            return new ProgressDialog(parent, statusInfo, resultInfo, cancelInfo);
        }    /**
         * 构造方法
         * @param parent 父窗体
         * @param statusInfo 当前线程动作提示信息
         * @param resultInfo 线程结束后的提示信息(若为null则不提示)
         * @param cancelInfo 点击取消按钮后的提示信息(若为null则不提示)
         */
        private ProgressDialog(Window parent, String statusInfo, String resultInfo, String cancelInfo)
        {
            this.parent = parent;
            this.statusInfo = statusInfo;
            this.resultInfo = resultInfo;
            this.cancelInfo = cancelInfo;
            initUI();
        }    private void initUI()
        {
            if(parent instanceof Dialog)
            {
                dialog = new JDialog((Dialog)parent, statusInfo, true);
            }
            else if(parent instanceof Frame)
            {
                dialog = new JDialog((Frame)parent, statusInfo, true);
            }
            else
            {
                dialog = new JDialog((Frame)null, statusInfo, true);
            }        final JPanel mainPane = new JPanel(null);
            progressBar = new JProgressBar();
            lbStatus = new JLabel("<html>" + statusInfo);
            btnCancel = new JButton("Cancel");
            progressBar.setStringPainted(true);
            btnCancel.addActionListener(this);        mainPane.add(progressBar);
            mainPane.add(lbStatus);
            mainPane.add(btnCancel);        dialog.getContentPane().add(mainPane);
            dialog.setUndecorated(true);
            dialog.setResizable(false);
            dialog.setSize(390, 100);
            dialog.setLocationRelativeTo(parent);        mainPane.addComponentListener(new ComponentAdapter()
            {
                public void componentResized(ComponentEvent e)
                {
                    layout(mainPane.getWidth(), mainPane.getHeight());
                }
            });
        }    /**
         * 执行事务
         * @param thread 事务线程
         */
        public void startThread(final Thread thread)
        {
            this.thread = thread;        new Thread()
            {
                public void run()
                {
                    try
                    {
                        thread.start();
                        // 等待事务处理线程结束
                        thread.join();
                    }
                    catch(InterruptedException e)
                    {
                        e.printStackTrace();
                    }
                    finally
                    {
                        // 关闭进度提示框
                        dialog.dispose();                    if(resultInfo != null && !resultInfo.trim().equals(""))
                        {
                            String title = "Info";
                            JOptionPane.showMessageDialog(parent, resultInfo, title, JOptionPane.INFORMATION_MESSAGE);
                        }
                    }
                }
            }.start();        dialog.setVisible(true);
        }    private void layout(int width, int height)
        {
            progressBar.setBounds(20, 20, 350, 15);
            lbStatus.setBounds(20, 50, 350, 25);
            btnCancel.setBounds(width - 85, height - 31, 75, 21);
        }    @SuppressWarnings("deprecation")
        public void actionPerformed(ActionEvent e)
        {
            if(thread != null)
            {
                resultInfo = cancelInfo;
                thread.stop();
            }
        }    public void setProgress(int value)
        {
            progressBar.setValue(value);
        }    public int getProgress()
        {
            return progressBar.getValue();
        }    public static void main(String[] args) throws Exception
        {
            final ProgressDialog dailog = ProgressDialog.show((Frame)null, "Status", "Result", "Cancel");        Thread thread = new Thread()
            {
                public void run()
                {
                    int index = 0;                while(index < 5)
                    {
                        try
                        {
                            sleep(1000);
                            dailog.setProgress(dailog.getProgress() + 20);
                            System.out.println(++index);
                        }
                        catch(InterruptedException e)
                        {
                            e.printStackTrace();
                        }
                    }
                }
            };        dailog.startThread(thread);
        }
    }
      

  5.   

    狂人三号 你好 你给我改的那个进度递加的 show方法里面没有thread参数的话 看到的进度只是假象 无法实现的 我试了好久了