renovationchap15.java
来自「Java the UML Way 书中所有源码」· Java 代码 · 共 306 行
JAVA
306 行
/*
* RenovationChap15.java E.L. 2001-08-23
*
* The data are stored in array lists, in an instance of the class RenovationProject.
* This instance is declared as "theProject".
*
* The surface data are displayed in the surfaceList object, which is an instance of
* the JTable class. The model behind is named "surfaceData" and is an instance of
* myLibrary.MyTableModel.
*
* Information about the materials is displayed in the materialList object, which is
* an instance of the JList class. The model behind is an instance of the DefaultListModel,
* and it is declared as materialData.
*
* The row numbers in the displayed lists should be the same as the indexes in "theProject".
*
* Many of the variables are declared in the Constants interface.
*/
import java.text.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import myLibrary.*;
class ProjectChap15 extends JFrame implements Constants {
private Container guiContainer;
private JToolBar toolbar = new JToolBar();
private JButton[] button = new JButton[buttonCommand.length];
/* Material data */
private DefaultListModel materialData = new DefaultListModel();
private JList materialList = new JList(materialData);
private JScrollPane scrollMaterialList = new JScrollPane(materialList);
/* Surface data */
private MyTableModel surfaceData =
new MyTableModel(columnNames); // from myLibrary
private JTable surfaceList = new JTable(surfaceData);
private JTextField sum = new JTextField(fieldLengthSum);
private SurfaceDialog theSurfaceDialog = new SurfaceDialog(this);
private PaintDialog thePaintDialog = new PaintDialog(this);
private FlooringDialog theFlooringDialog = new FlooringDialog(this);
private WallpaperDialog theWallpaperDialog = new WallpaperDialog(this);
private RenovationProject theProject;
public ProjectChap15(RenovationProject initProject) {
setTitle("Renovation");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
theProject = initProject; // N.B.! the link to the problem domain object
guiContainer = getContentPane();
initLists();
createToolbar();
createMenu();
guiContainer.setLayout(new GridBagLayout());
layoutGUI();
theSurfaceDialog.setLocation(dialogX, dialogY);
thePaintDialog.setLocation(dialogX, dialogY);
theFlooringDialog.setLocation(dialogX, dialogY);
theWallpaperDialog.setLocation(dialogX, dialogY);
setLocation(windowX, windowY);
setResizable(false); // the user is not allowed to change the size of the window
}
/* This class describes the listeners to the toolbar buttons */
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();
/* gets the command's index */
int index = 0;
boolean found = false;
while (index < buttonCommand.length && !found) {
if (command.equals(buttonCommand[index])) found = true;
else index++; // if not found, increase the index
}
if (index == buttonCommand.length) {
System.out.println
("Inexplicable error in the ButtonListener class in ProjectChap15");
return;
}
switch (index) {
case 0: // new surface
Surface newSurface = theSurfaceDialog.showDialog();
if (newSurface != null) {
Surface theSurface = theProject.addNewSurface(newSurface);
if (theSurface == newSurface) {
surfaceData.addRow(createRowData(newSurface));
} else JOptionPane.showMessageDialog(null,
newSurface.getName() + " is already registered.");
}
break;
case 1: // new paint
Paint newPaint = thePaintDialog.showDialog();
recordMaterial(newPaint);
break;
case 2: // new flooring
Flooring newFlooring = theFlooringDialog.showDialog();
recordMaterial(newFlooring);
break;
case 3: // new wallpaper
Wallpaper newWallpaper = theWallpaperDialog.showDialog();
recordMaterial(newWallpaper);
break;
case 4: // combine surface and material, calculate sum
int surfaceIndex = surfaceList.getSelectedRow();
Material material = theProject.getMaterial(materialList.getSelectedIndex());
if (surfaceIndex < 0 || material == null) {
JOptionPane.showMessageDialog(null,
"You have to select both surface and material before combining them!");
} else {
Surface theSurface = theProject.getSurface(surfaceIndex);
theSurface.setMaterial(material);
surfaceData.insertRow(surfaceIndex, createRowData(theSurface));
surfaceData.removeRow(surfaceIndex + 1);
sum.setText(currencyFormat.format(theProject.getTotalPrice()));
}
break;
case 5:
System.exit(0);
break;
default:
System.out.println("The program control should not enter this point!");
break;
}
if (surfaceData.getRowCount() > 0 && materialData.size() > 0) {
button[combineIndex].setEnabled(true);
}
}
/* Help method, is used in the switch-statement above */
private void recordMaterial(Material newMaterial) {
if (newMaterial != null) {
Material theMaterial = theProject.addNewMaterial(newMaterial);
if (theMaterial == newMaterial) materialData.addElement(newMaterial.toString());
else JOptionPane.showMessageDialog(null, newMaterial.getName()
+ " is already registered.");
}
}
}
/* This class describes listeners which react to the menu choices */
private class MenuListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();
if (command.equals(menuItem[0])) {
/* A very simple help */
JOptionPane.showMessageDialog(null,
"This program calculates the material requirement and cost for renovation\n"
+ "of an apartment. The apartment has to be input as a set of rectangular\n"
+ "surfaces. Each surface has to be covered with only one type of material.\n"
+ "You may choose between paint, wallpaper and flooring. You have to input\n"
+ "data about the surfaces (length and width) and the materials (price, etc.).\n"
+ "Use the toolbar or the menu to get input dialogs.\n"
+ "Click on 'Combine' to link a surface to a material.\n"
+ "The information in the window is continuously updated.");
} else {
JOptionPane.showMessageDialog(null,
"This program is one version of a continuous case in the book\n"
+ "'Java the UML Way, Integrating Object-Oriented Design and Programming'\n"
+ "written by Else Lervik and Vegard B. Havdal,\n"
+ "published by John Wiley & Sons, Ltd. 2002");
}
}
}
/* This private method creates a row with data customised to the surfaceData object */
private java.lang.Object[] createRowData(Surface theSurface) {
java.lang.Object[] columns = new java.lang.Object[columnNames.length];
columns[0] = theSurface.getName();
columns[1] = outputFormat.format(theSurface.getWidth());
columns[2] = outputFormat.format(theSurface.getLength());
Material theMaterial = theSurface.getMaterial();
if (theMaterial != null) {
columns[3] = theMaterial.getClass().getName() + ": " + theMaterial.getName();
columns[4] = outputFormat.format(theMaterial.getMaterialReq(theSurface));
columns[5] = outputFormat.format(theMaterial.getPricePerUnit());
columns[6] = outputFormat.format(theMaterial.getTotalPrice(theSurface));
}
return columns;
}
/* This private method initiates the surfaceList and materialList objects*/
private void initLists() {
/* A non proportional font */
Font defaultFont = surfaceList.getFont();
Font newFont = new Font("Monospaced",
defaultFont.getStyle(), defaultFont.getSize());
surfaceList.setFont(newFont);
sum.setFont(newFont);
surfaceList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
materialList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
/* The size of the window in which surfaceList is shown*/
surfaceList.setPreferredScrollableViewportSize(sizeSurfaceListWindow);
/* Heading for the material list */
JViewport jvp = new JViewport(); // see the online API documentation
jvp.setView(new JLabel("Materials"));
scrollMaterialList.setColumnHeader(jvp);
}
private void createToolbar() {
toolbar.setFloatable(false);
ButtonListener theButtonListener = new ButtonListener();
for (int i = 0; i < buttonCommand.length; i++) {
Icon icon = new ImageIcon(iconDirectory + iconfile[i]);
button[i] = new JButton(buttonCommand[i], icon);
button[i].setToolTipText(buttonDescription[i]);
button[i].setMnemonic(buttonMnemonic[i]);
button[i].addActionListener(theButtonListener);
toolbar.add(button[i]);
}
button[combineIndex].setEnabled(false);
}
private void createMenu() {
MenuListener theMenuListener = new MenuListener();
JMenu menu = new JMenu(menuName);
menu.setMnemonic(menuNameMnemonic);
for (int i = 0; i < menuItem.length; i++) {
JMenuItem menubar = menu.add(menuItem[i]);
menubar.setMnemonic(menuMnemonic[i]);
menubar.addActionListener(theMenuListener);
}
JMenuBar mbar = new JMenuBar();
/* Changes the layout manager to get the menu on the right hand side */
mbar.setLayout(new FlowLayout(FlowLayout.RIGHT));
mbar.add(menu);
setJMenuBar(mbar);
}
private void layoutGUI() {
GridBagConstraints constraints = new GridBagConstraints();
constraints.insets = new Insets(5, 5, 5, 5);// space around and between the components
constraints.weightx = 0.5;
constraints.weighty = 0.5;
/* The toolbar */
constraints.gridx = 0;
constraints.gridy = 0;
constraints.gridwidth = 4;
constraints.gridheight = 1;
constraints.fill = GridBagConstraints.NONE;
constraints.anchor = GridBagConstraints.WEST;
guiContainer.add(toolbar, constraints);
/* The surfaceList table */
constraints.gridx = 0;
constraints.gridy = 1;
constraints.gridwidth = 3;
constraints.gridheight = 1;
constraints.fill = GridBagConstraints.BOTH;
constraints.anchor = GridBagConstraints.CENTER;
guiContainer.add(new JScrollPane(surfaceList), constraints);
/* The materialList */
constraints.gridx = 3;
constraints.gridy = 1;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.fill = GridBagConstraints.BOTH;
constraints.anchor = GridBagConstraints.CENTER;
guiContainer.add(scrollMaterialList, constraints);
/* The background text */
constraints.gridx = 1;
constraints.gridy = 2;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.fill = GridBagConstraints.NONE;
constraints.anchor = GridBagConstraints.EAST;
guiContainer.add(new JLabel("Total price: "), constraints);
/* The sum textbox */
constraints.gridx = 2;
constraints.gridy = 2;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.anchor = GridBagConstraints.WEST;
guiContainer.add(sum, constraints);
}
}
class RenovationChap15 {
public static void main(String[] args) {
RenovationProject myProject
= new RenovationProject("The last version of the Renovation Case!");
ProjectChap15 window = new ProjectChap15(myProject);
window.pack();
window.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?