?? updatepanel.java
字號:
/* * @(#) UpdatePanel.java * Copyright 2004 HWStudio. All rights reserved. */package hws.item.smart.panel.function.admin;//導入核心Java類庫import java.awt.Insets;import java.awt.FlowLayout;import java.awt.GridBagLayout;import java.awt.GridBagConstraints;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JTable;import javax.swing.JButton;import javax.swing.JSplitPane;import javax.swing.JScrollPane;import javax.swing.ListSelectionModel;import javax.swing.event.ListSelectionEvent;import javax.swing.event.ListSelectionListener;import javax.swing.table.DefaultTableModel;//導入自定義Java類庫import hws.item.smart.misc.ImageShop;import hws.item.smart.misc.SBChanger;import hws.item.smart.panel.function.admin.misc.OptionalInfoPanel;import hws.item.smart.action.admin.update.StopUpdateAction;import hws.item.smart.action.admin.update.StartUpdateAction;import hws.item.smart.action.admin.update.RestartUpdateAction;import hws.item.smart.utility.chat.OptionalInfo;import hws.item.smart.utility.admin.XMLAccessor;import hws.item.smart.utility.admin.UpdateService;/** * 更新服務面板 * * @version 0.1 2005-08-08 * @author Hwerz */public class UpdatePanel extends JPanel { /*------------------------------------------------------------------------* * 屬性定義 * *------------------------------------------------------------------------*/ /** * 該類自身的一個靜態引用 */ private static UpdatePanel panel; /** * 基本信息面板 */ private BasicInfoPanel basicInfoPanel; /** * 可選信息面板 */ private OptionalInfoPanel optionalInfoPanel; /*------------------------------------------------------------------------* * 構造函數 * *------------------------------------------------------------------------*/ /** * 構造函數為私有,這樣在整個運行過程中該類就只能有一個實例 */ private UpdatePanel() { super(new GridBagLayout()); //啟動更新服務 UpdateService.getInstance().start(); //工具欄面板 GridBagConstraints constraints = new GridBagConstraints( //gridx, gridy 0, 0, //gridwidth, gridheight 1, 1, //weightx, weighty 1.0, 0.0, //anchor GridBagConstraints.NORTH, //fill GridBagConstraints.HORIZONTAL, //insets new Insets(5, 0, 0, 0), //ipadx, ipady 0, 0); add(new Toolbar(), constraints); //分割條面板 optionalInfoPanel = new OptionalInfoPanel(); basicInfoPanel = new BasicInfoPanel(); JSplitPane spliter = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, false, basicInfoPanel, optionalInfoPanel); spliter.setOneTouchExpandable(true); spliter.setDividerLocation(200); constraints.gridy = 1; constraints.weighty = 1.0; constraints.fill = GridBagConstraints.BOTH; constraints.insets = new Insets(5, 5, 5, 5); add(spliter, constraints); } /*------------------------------------------------------------------------* * 公共方法 * *------------------------------------------------------------------------*/ /** * 對該類提供的一個全局訪問點,用來實例化該對象 * * @return 該類唯一的一個實例 */ public static UpdatePanel getInstance() { if (panel == null) { panel = new UpdatePanel(); } return panel; } /** * 更新 * * @param id ID * @param nickname 昵稱 * @param password 密碼 */ public void update(String id, String nickname, String password) { basicInfoPanel.addRow(id, nickname, password); } /*------------------------------------------------------------------------* * 內部類 * *------------------------------------------------------------------------*/ /** * 工具欄面板 */ class Toolbar extends JPanel { /** * Create a new instance of this class */ public Toolbar() { super(new FlowLayout(FlowLayout.CENTER, 5, 0)); //啟動服務 JButton button = new JButton(StartUpdateAction.getInstance()); button.setIcon(ImageShop.START_IMAGEICON); button.addMouseListener(new SBChanger( StartUpdateAction.getInstance().getHintInfo(), false)); add(button); //停止服務 button = new JButton(StopUpdateAction.getInstance()); button.setIcon(ImageShop.STOP_IMAGEICON); button.addMouseListener(new SBChanger( StopUpdateAction.getInstance().getHintInfo(), false)); add(button); //重啟服務 button = new JButton(RestartUpdateAction.getInstance()); button.setIcon(ImageShop.RESTART_IMAGEICON); button.addMouseListener(new SBChanger( RestartUpdateAction.getInstance().getHintInfo(), false)); add(button); } } /** * 基本信息面板 */ class BasicInfoPanel extends JPanel implements ListSelectionListener { /** * 基本信息表格的視圖 */ private JTable userTable; /** * 基本信息表格的模型 */ private DefaultTableModel userModel; /** * Create a new instance of this class */ public BasicInfoPanel() { super(new GridBagLayout()); //更新用戶標簽 GridBagConstraints constraints = new GridBagConstraints( //gridx, gridy 0, 0, //gridwidth, gridheight 1, 1, //weightx, weighty 0.0, 0.0, //anchor GridBagConstraints.NORTHWEST, //fill GridBagConstraints.NONE, //insets new Insets(0, 5, 0, 0), //ipadx, ipady 0, 0); add(new JLabel("更新用戶"), constraints); //基本信息表格 Object[] header = {"ID", "昵稱", "密碼"}; userModel = new DefaultTableModel(); userModel.setColumnIdentifiers(header); userTable = new JTable(userModel) { public boolean isCellEditable(int row, int column) { return false; } }; userTable.getSelectionModel().addListSelectionListener(this); userTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); JScrollPane scroller = new JScrollPane(userTable, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); constraints.gridy = 1; constraints.weightx = 1.0; constraints.weighty = 1.0; constraints.fill = GridBagConstraints.BOTH; constraints.insets = new Insets(0, 0, 0, 0); add(scroller, constraints); } /** * 添加記錄 * * @param id ID * @param nickname 昵稱 * @param password 密碼 */ public void addRow(String id, String nickname, String password) { int count = userModel.getRowCount(); userModel.addRow(new Object[] {id, nickname, password}); userTable.getSelectionModel().setSelectionInterval(count, count); } /** * 判斷指定的用戶是否已經存在 * * @param id 指定用戶的ID * @return 如果指定的用戶已經存在則返回true,否則返回false */ public boolean isRowExist(String id) { boolean logout = false; for (int i = 0; i < userModel.getRowCount(); i++) { String id2 = userModel.getValueAt(i, 0).toString(); if (id2.equals(id) == true) { logout = true; break; } } return logout; } /** * 實現接口ListSelectionListener的方法 * * @param event the event that characterizes the change */ public void valueChanged(ListSelectionEvent event) { int row = userTable.getSelectedRow(); String id = userModel.getValueAt(row, 0).toString(); OptionalInfo info = XMLAccessor.getUser(id).getOptionalInfo(); optionalInfoPanel.setValue(info); } }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -