dialogs.java

来自「Java the UML Way 书中所有源码」· Java 代码 · 共 243 行

JAVA
243
字号
/*
 * Dialogs.java    E.L. 2001-08-23
 *
 * Dialogs for use in the Renovation Example in Chap15.
 * Many of the variables are declared in the Constants interface.
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;

import myLibrary.*;

class SurfaceDialog extends MyDialog implements Constants {
  private JTextField nameField = new JTextField(textFieldLength);
  private JTextField lengthField = new JTextField(textFieldLength);
  private JTextField widthField = new JTextField(textFieldLength);
  private double length;
  private double width;

  public SurfaceDialog(JFrame parent) {
    super(parent, "Surface");
    Container guiContainer = getContentPane();
    guiContainer.add(new JPanel(), BorderLayout.NORTH);
    guiContainer.add(new SurfaceDataPanel(), BorderLayout.CENTER);
    guiContainer.add(getButtonPanel(), BorderLayout.SOUTH);
    pack();
  }

  private class SurfaceDataPanel extends JPanel {
    public SurfaceDataPanel() {
      setLayout(new GridLayout(3,2));
      add(new JLabel("Name: ", JLabel.RIGHT));
      add(nameField);
      add(new JLabel("Width: ", JLabel.RIGHT));
      add(widthField);
      add(new JLabel("Length: ", JLabel.RIGHT));
      add(lengthField);
    }
  }

  protected boolean okData() {
    try {
      length = numberFormat.parsePositivDouble(lengthField.getText());
      width = numberFormat.parsePositivDouble(widthField.getText());
    } catch (ParseException e) {
      JOptionPane.showMessageDialog(null,
          "Number input could not be converted, Try again!");
      widthField.requestFocus();
      return false;
    }
    return true;
  }

  public Surface showDialog() {
    nameField.setText("");
    widthField.setText("");
    lengthField.setText("");
    setOk(false);
    setVisible(true);
    nameField.requestFocus();
    if (isOk()) return new Surface(nameField.getText(), length, width);
    else return null;
  }
}

class PaintDialog extends MyDialog implements Constants {
  private JTextField nameField = new JTextField(textFieldLength);
  private JTextField priceField = new JTextField(textFieldLength);
  private JTextField noOfCoatsField = new JTextField(textFieldLength);
  private JTextField noOfSqmPerLiterField = new JTextField(textFieldLength);
  private double price;
  private int noOfCoats;
  private double noOfLiters;

  private MyNumberFormatInput numberFormat = new MyNumberFormatInput();

  public PaintDialog(JFrame parent) {
    super(parent, "Paint");
    Container guiContainer = getContentPane();
    guiContainer.add(new JPanel(), BorderLayout.NORTH);
    guiContainer.add(new PaintDatapanel(), BorderLayout.CENTER);
    guiContainer.add(getButtonPanel(), BorderLayout.SOUTH);
    pack();
  }

  private class PaintDatapanel extends JPanel {
    public PaintDatapanel() {
      setLayout(new GridLayout(4,2));
      add(new JLabel("Name: ", JLabel.RIGHT));
      add(nameField);
      add(new JLabel("Price per Liter: ", JLabel.RIGHT));
      add(priceField);
      add(new JLabel("No. of Coats: ", JLabel.RIGHT));
      add(noOfCoatsField);
      add(new JLabel("No. of sqm per Liter: ", JLabel.RIGHT));
      add(noOfSqmPerLiterField);
    }
  }

  protected boolean okData() {
    try {
      price = numberFormat.parsePositivDouble(priceField.getText());
      noOfCoats = numberFormat.parsePositivInteger(noOfCoatsField.getText());
      noOfLiters = numberFormat.parsePositivDouble(noOfSqmPerLiterField.getText());
    } catch (ParseException e) {
      JOptionPane.showMessageDialog(null,
          "Number input could not be converted, Try again!");
      priceField.requestFocus();
      return false;
    }
    return true;
  }

  public Paint showDialog() {
    nameField.setText("");
    priceField.setText("");
    noOfCoatsField.setText("");
    noOfSqmPerLiterField.setText("");
    setOk(false);
    setVisible(true);
    nameField.requestFocus();
    if (isOk()) return new Paint(nameField.getText(), price, noOfCoats, noOfLiters);
    else return null;
  }
}

class WallpaperDialog extends MyDialog implements Constants {
  private JTextField nameField = new JTextField(textFieldLength);
  private JTextField priceField = new JTextField(textFieldLength);
  private JTextField lengthField = new JTextField(textFieldLength);
  private JTextField widthField = new JTextField(textFieldLength);
  private double price;
  private double length;
  private double width;

  private MyNumberFormatInput numberFormat = new MyNumberFormatInput();

  public WallpaperDialog(JFrame parent) {
    super(parent, "Wallpaper");
    Container guiContainer = getContentPane();
    guiContainer.add(new JPanel(), BorderLayout.NORTH);
    guiContainer.add(new WallpaperDataPanel(), BorderLayout.CENTER);
    guiContainer.add(getButtonPanel(), BorderLayout.SOUTH);
    pack();
  }

  private class WallpaperDataPanel extends JPanel {
    public WallpaperDataPanel() {
      setLayout(new GridLayout(4,2));
      add(new JLabel("Name: ", JLabel.RIGHT));
      add(nameField);
      add(new JLabel("Price per Roll: ", JLabel.RIGHT));
      add(priceField);
      add(new JLabel("The width of the Roll: ", JLabel.RIGHT));
      add(widthField);
      add(new JLabel("The length of the Roll: ", JLabel.RIGHT));
      add(lengthField);
    }
  }

  protected boolean okData() {
    try {
      price = numberFormat.parsePositivDouble(priceField.getText());
      length = numberFormat.parsePositivDouble(lengthField.getText());
      width = numberFormat.parsePositivDouble(widthField.getText());
    } catch (ParseException e) {
      JOptionPane.showMessageDialog(null,
          "Number input could not be converted, Try again!");
      priceField.requestFocus();
      return false;
    }
    return true;
  }

  public Wallpaper showDialog() {
    nameField.setText("");
    priceField.setText("");
    widthField.setText("");
    lengthField.setText("");
    setOk(false);
    setVisible(true);
    nameField.requestFocus();
    if (isOk()) return new Wallpaper(nameField.getText(), price, length, width);
    else return null;
  }
}

class FlooringDialog extends MyDialog implements Constants {
  private JTextField nameField = new JTextField(textFieldLength);
  private JTextField priceField = new JTextField(textFieldLength);
  private JTextField widthField = new JTextField(textFieldLength);
  private double price;
  private double width;

  private MyNumberFormatInput numberFormat = new MyNumberFormatInput();

  public FlooringDialog(JFrame parent) {
    super(parent, "Flooring");
    Container guiContainer = getContentPane();
    guiContainer.add(new JPanel(), BorderLayout.NORTH);
    guiContainer.add(new FlooringDataPanel(), BorderLayout.CENTER);
    guiContainer.add(getButtonPanel(), BorderLayout.SOUTH);
    pack();
  }

  private class FlooringDataPanel extends JPanel {
    public FlooringDataPanel() {
      setLayout(new GridLayout(3,2));
      add(new JLabel("Name: ", JLabel.RIGHT));
      add(nameField);
      add(new JLabel("Price per Meter: ", JLabel.RIGHT));
      add(priceField);
      add(new JLabel("Width: ", JLabel.RIGHT));
      add(widthField);
    }
  }

  protected boolean okData() {
    try {
      price = numberFormat.parsePositivDouble(priceField.getText());
      width = numberFormat.parsePositivDouble(widthField.getText());
    } catch (ParseException e) {
      JOptionPane.showMessageDialog(null,
          "Number input could not be converted, Try again!");
      priceField.requestFocus();
      return false;
    }
    return true;
  }

  public Flooring showDialog() {
    nameField.setText("");
    priceField.setText("");
    widthField.setText("");
    setOk(false);
    setVisible(true);
    nameField.requestFocus();
    if (isOk()) return new Flooring(nameField.getText(), price, width);
    else return null;
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?