• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
    • 軟件測試技術
    • 軟件測試博客
    • 軟件測試視頻
    • 開源軟件測試技術
    • 軟件測試論壇
    • 軟件測試沙龍
    • 軟件測試資料下載
    • 軟件測試雜志
    • 軟件測試人才招聘
      暫時沒有公告

    字號: | 推薦給好友 上一篇 | 下一篇

    Java設計模式之修飾模式篇(3)

    發布: 2008-6-04 14:48 | 作者: 不詳 | 來源: code365 | 查看: 38次 | 進入軟件測試論壇討論

    領測軟件測試網 關鍵字:Java設計模式

     //Test.java
      import java.awt.*;
      import java.awt.event.*;
      import java.util.Locale;
      import java.util.ResourceBundle;

      import javax.swing.*;
      import javax.swing.table.*;

      public class Test extends JFrame {
      public static void main(String args[]) {
      SwingApp.launch(new Test(), "排序修飾者",
      300, 300, 450, 250);
      }
      public Test() {
      // 生成修飾者的實例,該實例用于修飾Swing Table原有的表模型
      // 該實例必須是final的,因為它會被內嵌類引用。
      final TableSortDecorator decorator =
      new TableBubbleSortDecorator(table.getModel());
      // 將表的模型設定為修飾者。因為修飾者實現了TableModel接口,
      // 因此Swing Table對象不知道修飾者和真實對象之間的差別。
      table.setModel(decorator);
      getContentPane().add(new JScrollPane(table),
      BorderLayout.CENTER);
      // 在界面中添加一個狀態區
      getContentPane().add(SwingApp.getStatusArea(),
      BorderLayout.SOUTH);
      SwingApp.showStatus("進行排序前");
      // 獲得對表中列頭的引用。
      JTableHeader hdr = (JTableHeader)table.getTableHeader();
      // 當單擊鼠標單擊列頭時,調用修飾者的sort()方法。
      hdr.addMouseListener(new MouseAdapter() {
      public void mouseClicked(MouseEvent e) {
      TableColumnModel tcm = table.getColumnModel();
      int vc = tcm.getColumnIndexAtX(e.getX());
      int mc = table.convertColumnIndexToModel(vc);
      // 進行排序
      decorator.sort(mc);
      // 更新狀態區
      SwingApp.showStatus(headers[mc] + " 排序中");
      }
      });
      }
      final String[] headers = { "品名", "價格/每斤." };
      JTable table = new JTable(new Object[][] {
      {"蘋果", "1.2"}, {"芒果", "4"},
      {"檸檬", "2.5"},{"香蕉", "0.8"},
      {"桔子", "1.8"}, {"西瓜", "0.5"},
      {"橘子", "2.5"}, {"櫻桃", "3.6"},
      {"柚子", "0.8"}, {"葡萄", "2.2"},
      }, headers);
      }
      class SwingApp extends WindowAdapter {
      private SwingApp() {} // 該類不能被初始化
      public static void launch(final JFrame f, String title,
      final int x, final int y,
      final int w, int h) {
      launch(f,title,x,y,w,h,null);
      }
      public static void launch(final JFrame f, String title,
      final int x, final int y,
      final int w, int h,
      String propertiesFilename) {
      statusArea.setBorder(BorderFactory.createEtchedBorder());
      statusArea.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
      statusArea.add(status);
      status.setHorizontalAlignment(JLabel.LEFT);
      if(propertiesFilename != null) {
      resources = ResourceBundle.getBundle(
      propertiesFilename, Locale.getDefault());
      }

      f.setTitle(title);
      f.setBounds(x,y,w,h);
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      f.setVisible(true);
      }
      static public JPanel getStatusArea() {
      return statusArea;
      }
      static public void showStatus(String s) {
      status.setText(s);
      }
      static Object getResource(String key) {
      if(resources != null) {
      return resources.getString(key);
      }
      return null;
      }
      static private JPanel statusArea = new JPanel();
      static private JLabel status = new JLabel(" ");
      static private ResourceBundle resources;
      }
       在上面的代碼中,排序修飾者修飾了Swing表原有的模型。當用戶在列頭上單擊鼠標的時候,程序調用修飾者的sort()方法。在研究排序修飾者的代碼前,然我們來看一下圖7中的類結構圖:

    延伸閱讀

    文章來源于領測軟件測試網 http://www.kjueaiud.com/

    TAG: java JAVA Java 模式 設計

    21/212>

    關于領測軟件測試網 | 領測軟件測試網合作伙伴 | 廣告服務 | 投稿指南 | 聯系我們 | 網站地圖 | 友情鏈接
    版權所有(C) 2003-2010 TestAge(領測軟件測試網)|領測國際科技(北京)有限公司|軟件測試工程師培訓網 All Rights Reserved
    北京市海淀區中關村南大街9號北京理工科技大廈1402室 京ICP備10010545號-5
    技術支持和業務聯系:info@testage.com.cn 電話:010-51297073

    軟件測試 | 領測國際ISTQBISTQB官網TMMiTMMi認證國際軟件測試工程師認證領測軟件測試網

    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>