Java程序设计技巧与开发实例附书源代码。

源代码在线查看: invoicer.java

软件大小: 4400 K
上传用户: bling
关键词: Java 程序 开发实例 源代码
下载地址: 免注册下载 普通下载 VIP

相关代码

				
				import java.awt.*;
				import java.awt.event.*;
				import javax.swing.*;
				import javax.swing.table.*;
				
				public class Invoicer
				    extends JFrame {
				  public NoEditTableModel data = new NoEditTableModel();
				  public JTable table = new JTable(data);
				  public JLabel price = new JLabel("¥0.0", SwingConstants.RIGHT);
				
				  private class WindowCloser
				      extends WindowAdapter {
				    public void windowClosing(WindowEvent we) {
				      System.exit(0);
				    }
				  }
				
				  public Invoicer() {
				    super("Simple Table");
				    createDataColumns();
				    createSampleData();
				    JScrollPane scrollTable = new JScrollPane(table);
				    price.setText("Total Price: ¥" + total());
				    scrollTable.setPreferredSize(new Dimension(400, 100));
				
				    getContentPane().add("Center", new JPanelBox(scrollTable, "Data"));
				    getContentPane().add("South", new JPanelBox(price, "Total Cost"));
				    addWindowListener(new WindowCloser());
				    table.addMouseListener(new InvoicePopup(this));
				    table.setToolTipText("Right-Click for menu");
				
				    validate();
				    pack();
				    setVisible(true);
				  }
				
				  private void createDataColumns() {
				    /* create columns with appropriate headers */
				    data.addColumn("ID");
				    data.addColumn("Description");
				    data.addColumn("Unit Price");
				    data.addColumn("Quantity");
				    data.addColumn("Price");
				  }
				
				  private void createSampleData() {
				    /* create some sample data */
				    data.addRow(dataRow(19, "IBM 770 Laptop", 26800, 1));
				    data.addRow(dataRow(207, "External ZIP drive", 580, 1));
				    data.addRow(dataRow(1008, "SDRAM Memory Chips, 128MB", 150, 4));
				    data.addRow(dataRow(44, "IBM 21' Triniton Monitor", 3880, 1));
				    data.addRow(dataRow(105, "External Keyboard, black", 480, 1));
				    data.addRow(dataRow(207, "External Mouse, black", 160, 1));
				    data.addRow(dataRow(45, "IBM Port Replicator", 1200, 1));
				  }
				
				  private Object[] dataRow(int id, String desc, double price, int num) {
				    /* utility method to convert basic types into array of Objects */
				    Object row[] = new Object[5];
				    row[0] = new Integer(id);
				    row[1] = desc;
				    row[2] = new Double(price);
				    row[3] = new Integer(num);
				    row[4] = new Double(price * num);
				    return row;
				  }
				
				  protected double total() {
				    /* computes the total in the last column of the table */
				    double sum = 0.0;
				    for (int row = 0; row < data.getRowCount(); row++) {
				      sum += ( (Double) data.getValueAt(row, 4)).doubleValue();
				    }
				    return sum;
				  }
				
				  public static void main(String args[]) {
				    Invoicer st = new Invoicer();
				  }
				}
							

相关资源