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

發表于:2008-06-04來源:作者:點擊數: 標簽:設計javaJAVAJava模式
關鍵字: 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 voi
關鍵字: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.anti-gravitydesign.com

国产97人人超碰caoprom_尤物国产在线一区手机播放_精品国产一区二区三_色天使久久综合给合久久97