posts - 38,  comments - 22,  trackbacks - 0
                Swing的事件處理中,對(duì)于非常費(fèi)時(shí)的處理如果不用多線程會(huì)造成主界面的阻塞,即界面一片白色,這樣非常不好的,也是不提倡的,而起用多線程進(jìn)行事件處理又面臨一個(gè)問(wèn)題即如果通知用戶事件處理的進(jìn)度,是否處理完成,所以最好在處理的過(guò)程中給用戶一提示,標(biāo)明處理的進(jìn)度信息。這種提示可以用進(jìn)度條的信息來(lái)表示出來(lái),以下就是一個(gè)用進(jìn)度條來(lái)標(biāo)示事件進(jìn)度的一個(gè)簡(jiǎn)單實(shí)例,這個(gè)例子中還使用了Observer模式
          進(jìn)度窗口:
          package pro;

          import java.awt.BorderLayout;
          import java.awt.Color;
          import java.awt.event.ActionEvent;
          import java.awt.event.ActionListener;
          import java.util.Observable;
          import java.util.Observer;

          import javax.swing.JButton;
          import javax.swing.JLabel;
          import javax.swing.JPanel;
          import javax.swing.JProgressBar;

          public class Progress extends javax.swing.JDialog implements ActionListener {
              
          /**
               * 
               
          */

              
          private static final long serialVersionUID = 1L;

              
          private JProgressBar pro_JPB;

              
          private JPanel btn_JP;

              
          private JButton cancel_JB;

              
          private JLabel show_JL;

              NewJFrame jf;

              
          public Progress(NewJFrame frame) {
                  
          super(frame, true);
                  
          this.jf = frame;
                  initGUI();
              }


              set se 
          = new set();

              
          private void initGUI() {
                  
          try {

                      se.addObserver(
          new setObserver());
                      BorderLayout thisLayout 
          = new BorderLayout();
                      getContentPane().setLayout(thisLayout);
                      
          {
                          show_JL 
          = new JLabel();
                          getContentPane().add(show_JL, BorderLayout.NORTH);
                          show_JL.setPreferredSize(
          new java.awt.Dimension(35422));
                      }

                      
          {
                          pro_JPB 
          = new JProgressBar();
                          pro_JPB.setMinimum(
          0);
                          pro_JPB.setMaximum(
          100);
                          getContentPane().add(pro_JPB, BorderLayout.CENTER);
                          pro_JPB.setPreferredSize(
          new java.awt.Dimension(39227));
                      }

                      
          {
                          btn_JP 
          = new JPanel();
                          getContentPane().add(btn_JP, BorderLayout.SOUTH);
                          
          {
                              cancel_JB 
          = new JButton();
                              btn_JP.add(cancel_JB);
                              cancel_JB.setText(
          "\u53d6\u6d88");
                              cancel_JB.addActionListener(
          this);
                          }

                      }

                      
          this.setBackground(Color.DARK_GRAY.brighter());
                      
          this.setUndecorated(true);
                      
          this.setSize(39290);
                      
          this.setLocationRelativeTo(null);
                  }
           catch (Exception e) {
                      e.printStackTrace();
                  }

              }


              
          public void setMinmum(int i) {
                  pro_JPB.setMinimum(i);
              }


              
          public void setMaxmem(int i) {
                  pro_JPB.setMaximum(i);
              }


              
          public void setVal(int i) {
                  se.setPrice(i);
              }


              
          /**
               * 
               * set Class->function:觀察者模式的具體應(yīng)用
               * 2007-4-17
               
          */

              
          class set extends Observable {
                  
          private int price;

                  
          public int getPrice() {
                      
          return price;
                  }


                  
          public void setPrice(int price) {
                      
          this.price = price;
                      setChanged();
                      notifyObservers(
          new Integer(price));
                  }


              }


              
          class setObserver implements Observer {
                  
          public void update(Observable obj, Object arg) {
                      
          if (arg instanceof Integer) {
                          
          int i = ((Integer) arg).intValue();
                          pro_JPB.setValue(i);
                          show_JL.setText((i 
          - pro_JPB.getMinimum()) * 100
                                  
          / (float) (pro_JPB.getMaximum() - pro_JPB.getMinimum())
                                  
          + "%");
                      }

                  }


              }


              
          public void actionPerformed(ActionEvent e) {
                  jf.setTh_Flag(
          false);
                  
          this.setVisible(false);
              }


          }


          主程序:
          package pro;

          import java.awt.BorderLayout;
          import java.awt.event.ActionEvent;
          import java.awt.event.ActionListener;

          import javax.swing.JButton;
          import javax.swing.JPanel;
          import javax.swing.WindowConstants;

          public class NewJFrame extends javax.swing.JFrame {
              
          /**
               * 
               
          */

              
          private static final long serialVersionUID = 1L;

              
          private JPanel cen_JP;

              
          private JButton jb;

              
          public static void main(String[] args) {
                  NewJFrame inst 
          = new NewJFrame();
                  inst.setVisible(
          true);
              }


              
          public NewJFrame() {
                  
          super();
                  initGUI();
              }


              Progress pro;

              
          private void initGUI() {
                  
          try {
                      setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
                      
          {
                          cen_JP 
          = new JPanel();
                          getContentPane().add(cen_JP, BorderLayout.CENTER);
                          
          {
                              jb 
          = new JButton();
                              cen_JP.add(jb);
                              jb.setText(
          "jButton1");
                              jbAction ja 
          = new jbAction();
                              pro 
          = new Progress(this);
                              jb.addActionListener(ja);
                          }

                      }

                      pack();
                      setSize(
          400300);
                  }
           catch (Exception e) {
                      e.printStackTrace();
                  }

              }


              
          boolean th_Flag = true;

              
          class jbAction implements ActionListener {
                  
          public void actionPerformed(ActionEvent e) {
                      pro.setMaxmem(
          100);
                      pro.setMinmum(
          0);
                      
          new Thread() {
                          
          public void run() {
                              
          for (int i = 1; i < 100; i++{
                                  
          if (!isTh_Flag()) {
                                      setTh_Flag(
          true);

                                      
          break;
                                  }

                                  
          try {
                                      Thread.sleep(
          1000);
                                  }
           catch (InterruptedException e) {
                                      e.printStackTrace();
                                  }

                                  pro.setVal(i);
                                  System.out.println(
          "===" + i);
                              }

                          }

                      }
          .start();
                      pro.setVal(
          0);
                      pro.setVisible(
          true);
                  }


              }


              
          public boolean isTh_Flag() {
                  
          return th_Flag;
              }


              
          public void setTh_Flag(boolean th_Flag) {
                  
          this.th_Flag = th_Flag;
              }

          }


          發(fā)現(xiàn)了一個(gè)1.5的新特性
          如果傳給dialog一個(gè)組件參數(shù)Com后,無(wú)論是否是模態(tài)都顯示在Com之上
          posted on 2007-04-17 11:44 aaabbb 閱讀(3031) 評(píng)論(0)  編輯  收藏

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 米脂县| 黄大仙区| 酒泉市| 长乐市| 桃园市| 库伦旗| 上思县| 临高县| 宿迁市| 通化市| 冕宁县| 上林县| 武平县| 嵊泗县| 木里| 贵溪市| 桃园县| 来安县| 定日县| 石家庄市| 沐川县| 侯马市| 杭州市| 濮阳县| 南投县| 东乡| 高平市| 顺平县| 黑山县| 涞源县| 高阳县| 久治县| 宁夏| 辽宁省| 池州市| 饶阳县| 通渭县| 兴山县| 铜陵市| 肥乡县| 通江县|