java编程开发技巧与实例的编译测试通过的所有例程
源代码在线查看: invoicer.java
import java.awt.Dimension;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
public class Invoicer extends JFrame
{
protected NoEditTableModel data = new NoEditTableModel();
protected JTable table = new JTable(data);
protected 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());
validate();
pack();
setLocationRelativeTo(this);
setVisible(true);
table.addMouseListener(new InvoicePopup(this));
table.setToolTipText("right-click for menu");
}
private void createDataColumns()
{
data.addColumn("ID");
data.addColumn("Description");
data.addColumn("Unit Price");
data.addColumn("Quantity");
data.addColumn("Price");
}
private void createSampleData()
{
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, 128M", 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, "清华同方,滥键盘", 160, 1));
data.addRow(dataRow(45, "ibm port replicator", 1200, 1));
}
private Object[] dataRow(int id, String desc, double price, int num)
{
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()
{
double sum = 0.0;
for (int row = 0; row < data.getRowCount(); row ++)
sum += ((Double)data.getValueAt(row, 4)).doubleValue();
return sum;
}
public static void styleCtrl(String style)
{
try
{
String lf_wind = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
String lf_unix = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
String lf_java = "javax.swing.plaf.metal.MetalLookAndFeel";
if (style.equals("java"))
UIManager.setLookAndFeel(lf_java);
else if (style.equals("unix"))
UIManager.setLookAndFeel(lf_unix);
else if (style.equals("windows"))
UIManager.setLookAndFeel(lf_wind);
}
catch (Exception e)
{
System.err.println("Exception: " + e);
}
}
public static void main(String args[])
{
styleCtrl("java");
Invoicer st = new Invoicer();
}
}