两个面板都是JPanel
对象。左侧的对象包含来自类ProductList.java
的对象,该对象扩展了awt.List
。右侧包含来自类ProductTable.java
的对象,该对象扩展了JScrollPane
并将TableModel
实现添加到其JViewPort
对象。 ProductList
和ProductTable
都被添加到面板中,如下所示:
pleft.add(new ProductList());
和
pright.add(new ProductTable());
现在必须更改。 Lister.java
类必须替换以下类:
pleft.add(new Lister(Lister.LIST));
和
pright.add(new Lister(Lister.TABLE));
我最近开始在Java中看到设计模式,因此我对它们的理解和经验非常有限。我可以想到一种工厂或装饰器设计模式,因为Lister类提供了两个有些相关的对象(它们都是awt.Component
对象)。让我感到困惑的是,new Lister()
如何仅使用String
构造函数参数来传递两个不同的对象。我被困在这里:
public class Lister{
public static final String LIST = "LIST";
public static final String TABLE = "TABLE";
public Lister(String type){
if(type.equals(LIST)) {
}
if(type.equals(TABLE)) {
}
}
}
主程序如下所示: ProductDisplay.java
public class ProductDisplay extends JFrame
{
private static final long serialVersionUID = 1L;
public ProductDisplay()
{
super("Die Telematik-Produkte");
setLF(); //set look and feel
setCloseClick(); //set close on window close click
InputFile f = new InputFile("products.txt");
Vector<String> prod = new Vector<String>();
//read in product list
String s = f.readLine();
while(s != null)
{
prod.addElement(s);
s = f.readLine();
}
JPanel p = new JPanel();
getContentPane().add(p);
p.setLayout(new GridLayout(1,2));
JPanel pleft = new JPanel();
JPanel pright = new JPanel();
p.add(pleft);
p.add(pright);
pleft.setLayout(new BorderLayout());
pright.setLayout(new BorderLayout());
//add in customer view as list box
pleft.add("North", new JLabel("Customer view"));
pleft.add("Center", new ProductList(prod)); // This row ...
//pleft.add("Center", new Lister(prod, Lister.LIST)); // ...should be replaced by this one
//add in execute view as table
pright.add("North", new JLabel("Executive view"));
pright.add("Center", new ProductTable(prod)); // And this row ...
//pright.add("Center", new Lister(prod, Lister.TABLE)); // ...should be replaced by this one
setSize(new Dimension(400,300));
setVisible(true);
}
//-----------------------------------------
private void setCloseClick()
{
//create window listener to respond to window close click
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e) {System.exit(0);}
});
}
//------------------------------------------
private void setLF()
{
// Force SwingApp to come up in the System L&F
String laf = UIManager.getSystemLookAndFeelClassName();
try {
UIManager.setLookAndFeel(laf);
}
catch (UnsupportedLookAndFeelException exc)
{System.err.println("Warning: UnsupportedLookAndFeel: " + laf);}
catch (Exception exc) {System.err.println("Error loading " + laf + ": " + exc);
}
}
//---------------------------------------------
static public void main(String argv[])
{
new ProductDisplay();
}
}
ProductList.java
public class ProductList extends java.awt.List
{
private static final long serialVersionUID = 1L;
public ProductList(Vector<String> products)
{
super(products.size()); //for compatibility
for (int i = 0; i < products.size(); i++)
{
//take each strig apart and keep only
//the product names, discarding the quntities
String s = products.elementAt(i);
int index = s.indexOf("--"); //separate qty from name
if(index > 0)
add(s.substring(0, index));
else
add(s);
}
}
}
ProductTable.java
public class ProductTable extends JScrollPane
{
private static final long serialVersionUID = 1L;
JTable table;
public ProductTable(Vector<String> list)
{
table = new JTable(new prodModel(list));
getViewport().add(table);
}
}
class prodModel implements TableModel
{
int rows;
int columns;
Vector<String> prodNames;
Vector<String> quantities;
public prodModel(Vector<String> products)
{
rows = products.size();
columns = 2;
prodNames = new Vector<String>();
quantities = new Vector<String>();
for(int i=0; i< products.size(); i++)
{
String s = products.elementAt(i);
int index = s.indexOf("--"); //separate qty from name
if(index > 0)
{
prodNames.addElement(s.substring(0, index));
quantities.addElement(s.substring(index+2).trim());
}
else
prodNames.addElement(s);
}
}
}