基于RMI的客户管理程序- - JavaEye技术网站

参考:

运行效果:

?

?

// mvc.view.StoreGUI.java

?

package mvc.view;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.awt.event.WindowListener;
import java.io.Serializable;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.DefaultCaret;

import data.Customer;

/**
 *@date 2010-3-14 下午05:12:35
 *@author dycc
 *@file mvc.view.StoreGUI.java
 */
public class StoreGUI implements Serializable{
	/**
     * 
     */
    private static final long serialVersionUID = 1L;
	// 界面的主要窗体组件
	protected JFrame frame;
	protected JTabbedPane tabbedPane = new JTabbedPane();
	protected JPanel logPanel = new JPanel();
	// 单个客户面板及组件
	protected JPanel pan_singleCust = new JPanel();
	protected JLabel lab_id = new JLabel("id:",SwingConstants.RIGHT);
	protected JLabel lab_name = new JLabel("客户姓名:",SwingConstants.RIGHT);
	protected JLabel lab_address = new JLabel("客户地址:",SwingConstants.RIGHT);
	protected JLabel lab_age = new JLabel("年    龄:",SwingConstants.RIGHT);
	protected JTextField txt_id = new JTextField(32);
	protected JTextField txt_name = new JTextField(32);
	protected JTextField txt_address = new JTextField(32);
	protected JTextField txt_age = new JTextField(32);
	protected JButton btn_add = new JButton("添加客户");
	protected JButton btn_del = new JButton("删除客户");
	protected JButton btn_get = new JButton("查询客户");
	protected JButton btn_upd = new JButton("更新客户");
	// 所有客户面板及组件
	protected JPanel pan_allCust = new JPanel();
	protected JLabel lab_allCust = new JLabel("所有客户清单",SwingConstants.CENTER);
	protected JScrollPane srp_tab;
	protected JTable tab;
	protected String[] tab_header = new String[]{"id","姓名","地址","年龄"};
	protected DefaultTableModel tabM;
	// 日志面板及组件
	protected JLabel lab_log = new JLabel("日志信息",SwingConstants.CENTER);
	protected JTextArea txA_log = new JTextArea(9,50);
	protected JScrollPane srp_log = new JScrollPane(txA_log);
	
	/**
	 * 构造方法
	 */
	public StoreGUI(){
		JFrame.setDefaultLookAndFeelDecorated(true);
		buildDisplay();
	}
	/**
	 * 创建主窗体
	 */
	public void buildDisplay(){
		tabbedPane.add("客户详细信息", createSingleCust());
		tabbedPane.add("所有客户清单",createdAllCust());
		logPanel.setLayout(new BorderLayout());
		logPanel.add(lab_log,BorderLayout.NORTH);
		logPanel.add(srp_log,BorderLayout.CENTER);
		DefaultCaret caret = (DefaultCaret)txA_log.getCaret();
		caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);      // 设置为自动滚动
		
		frame = new JFrame("客户管理系统");
		frame.setSize(600, 400);
		
		GridBagLayout gridbag = new GridBagLayout();
		GridBagConstraints cons = new GridBagConstraints();
		cons.fill = GridBagConstraints.BOTH;
		
		Container content_panel = frame.getContentPane();
		content_panel.setLayout(gridbag);
		
		cons.gridx = 0;
		cons.gridy = 0;
		cons.weightx = 1.0;
		cons.weighty = 0.6;
		gridbag.setConstraints(tabbedPane, cons);
		content_panel.add(tabbedPane);
		
		cons.gridy = 1;
		cons.weighty = 0.4;
		gridbag.setConstraints(logPanel, cons);
		content_panel.add(logPanel);
		// 此处必须设置为 DO_NOTHING_ON_CLOSE,否则窗体无法正常关闭
		frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		frame.setVisible(true);
		centerFrame(frame);
	}
	/**
	 * 创建客户详细信息 Panel
	 * @return
	 */
	public JPanel createSingleCust(){
		GridBagLayout gridbag = new GridBagLayout();
		GridBagConstraints cons = new GridBagConstraints();
		
		cons.fill = GridBagConstraints.BOTH;
		cons.weighty = 0.20;
		
		pan_singleCust.setLayout(gridbag);
		
		cons.gridx = 0;
		cons.gridy = 0;
		cons.weightx = 0.25;
		cons.insets = new Insets(2,6,2,12);
		gridbag.setConstraints(lab_id, cons);
		pan_singleCust.add(lab_id);
		
		cons.gridy = 1;
		gridbag.setConstraints(lab_name, cons);
		pan_singleCust.add(lab_name);
		
		cons.gridy = 2;
		gridbag.setConstraints(lab_address, cons);
		pan_singleCust.add(lab_address);
		
		cons.gridy = 3;
		gridbag.setConstraints(lab_age, cons);
		pan_singleCust.add(lab_age);
		
		cons.gridx = 1;
		cons.gridy = 0;
		cons.gridwidth = 3;
		cons.weightx = 0.75;
		cons.insets = new Insets(0,0,0,0);
		gridbag.setConstraints(txt_id, cons);
		pan_singleCust.add(txt_id);
		
		cons.gridy = 1;
		gridbag.setConstraints(txt_name, cons);
		pan_singleCust.add(txt_name);
		
		cons.gridy = 2;
		gridbag.setConstraints(txt_address, cons);
		pan_singleCust.add(txt_address);
		
		cons.gridy = 3;
		gridbag.setConstraints(txt_age, cons);
		pan_singleCust.add(txt_age);
		
		cons.gridx = 0;
		cons.gridy = 4;
		cons.gridwidth = 1;
		cons.weightx = 0.25;
		gridbag.setConstraints(btn_add, cons);
		pan_singleCust.add(btn_add);
		
		cons.gridx = 1;
		gridbag.setConstraints(btn_del, cons);
		pan_singleCust.add(btn_del);
		
		cons.gridx = 2;
		gridbag.setConstraints(btn_get, cons);
		pan_singleCust.add(btn_get);
		
		cons.gridx = 3;
		gridbag.setConstraints(btn_upd, cons);
		pan_singleCust.add(btn_upd);
		return pan_singleCust;
	}
	/**
	 * 创建所有客户信息 Panel
	 * @return
	 */
	public JPanel createdAllCust(){
		pan_allCust.setLayout(new BorderLayout());
		
		tabM = new DefaultTableModel(tab_header,10);
		tab = new JTable(tabM);
		srp_tab = new JScrollPane(tab);
		tab.setRowHeight(32);
		tab.getTableHeader().setReorderingAllowed(false);  // 表头不可拖拽
		tab.getTableHeader().setResizingAllowed(true);     // 可调整列宽
		
		pan_allCust.add(lab_allCust,BorderLayout.NORTH);
		pan_allCust.add(srp_tab,BorderLayout.CENTER);
		
		return pan_allCust;
	}
	/**
	 * 居中显示窗体
	 * @param frame
	 */
	public void centerFrame(JFrame frame){
		double width = Toolkit.getDefaultToolkit().getScreenSize().getWidth();
		double height = Toolkit.getDefaultToolkit().getScreenSize().getHeight();
		int left = (int)(width - frame.getWidth())/2;
		int top = (int)(height - frame.getHeight())/2;
		frame.setLocation(left, top);
	}
	/**
	 * 添加 TabbedPanel 监听
	 * @param listener
	 */
	public void addTabChangeListener(ChangeListener listener){
		this.tabbedPane.addChangeListener(listener);
	}
	/**
	 * 添加 增删查改 监听
	 * @param listeners
	 */
	public void addCURDListener(ActionListener[] listeners){
		if(listeners.length != 4){
			return;
		}
		btn_add.addActionListener(listeners[0]);
		btn_del.addActionListener(listeners[1]);
		btn_get.addActionListener(listeners[2]);
		btn_upd.addActionListener(listeners[3]);
	}
	/**
	 * 添加窗体关闭监听
	 * @param listener
	 */
	public void addWindowListener(WindowListener listener){
		frame.addWindowListener(listener);
	}
	/**
	 * 销毁窗体
	 */
	public void destroyWindow(){
		frame.dispose();
		System.exit(0);
	}
	/**
	 * 更新客户数据
	 * @param customer
	 */
	public void refreshCustomer(Customer customer){
		if(customer == null || customer.getId() == -1){
			txt_id.setText("");
			txt_name.setText("");
			txt_address.setText("");
			txt_age.setText("");
		}else{
			txt_id.setText(((Long)customer.getId()).toString());
			txt_name.setText(customer.getName());
			txt_address.setText(customer.getAddress());
			txt_age.setText(((Integer)customer.getAge()).toString());
		}
	}
	/**
	 * 刷新所有客户信息
	 * @param custs
	 */
	public void refreshAllCustomer(List<Customer> custs){
		String[][] data = new String[custs.size()][4];
		for(int idx=0;idx < custs.size();idx++){
			Customer temp = custs.get(idx);
			data[idx][0] = new Long(temp.getId()).toString();
			data[idx][1] = temp.getName();
			data[idx][2] = temp.getAddress();
			data[idx][3] = new Integer(temp.getAge()).toString();
		}
		tabM.setDataVector(data,tab_header);
	}
	/**
	 * 获取当前 tab 页下标
	 * @param customer
	 */
	public int getTabbedIndex(){
		return tabbedPane.getSelectedIndex();
	}
	/**
	 * 更新日志
	 * @param msg
	 */
	public void updateLog(String msg){
		txA_log.append(msg + "\r\n");
		// 将光标置于{zh1}
		txA_log.setCaretPosition(txA_log.getText().length());
	}
	/**
	 * 获取客户Id
	 * @return
	 */
	public long getCustomerId(){
		try{
			return Long.parseLong(txt_id.getText().trim());
		} catch(Exception e){
			updateLog(e.getMessage());
			return -1;
		}
	}
	/**
	 * 获取客户信息
	 * @return
	 */
	public Customer getCustomer(){
		try{
			Customer customer = new Customer();
			customer.setId(Long.parseLong(txt_id.getText().trim()));
			customer.setName(txt_name.getText().trim());
			customer.setAddress(txt_address.getText().trim());
			customer.setAge(Integer.parseInt(txt_age.getText().trim()));
			return customer;
		} catch(Exception e){
			updateLog(e.getMessage());
			return null;
		}
	}
}

?

附件中是完整的应用,导入到 eclipse 后,

先运行 mvc.server.StoreServer.java

再运行 mvc.client.StoreClient.java

?

郑重声明:资讯 【基于RMI的客户管理程序- - JavaEye技术网站】由 发布,版权归原作者及其所在单位,其原创性以及文中陈述文字和内容未经(企业库qiyeku.com)证实,请读者仅作参考,并请自行核实相关内容。若本文有侵犯到您的版权, 请你提供相关证明及申请并与我们联系(qiyeku # qq.com)或【在线投诉】,我们审核后将会尽快处理。
—— 相关资讯 ——