本帖最后由 zhen1606 于 2012-01-19 08:47:38 编辑

解决方案 »

  1.   

    Class MainWindow
    package dialog;import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    import javax.swing.JButton;
    import javax.swing.JCheckBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JOptionPane;
    import javax.swing.JRadioButton;
    import javax.swing.JTextField;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    import net.miginfocom.swing.MigLayout;public class MainWindow extends JFrame {
    private JMenuBar menuBar = new JMenuBar();
    private JMenu fileMenu = new JMenu("File");
    private JMenu fileEdit = new JMenu("Edit");
    private JMenu fileHelp = new JMenu("Help");
    private JMenuItem newMenuItem = new JMenuItem("New Customer");
    private JMenuItem openMenuItem = new JMenuItem("Open...");
    private JMenuItem saveMenuItem = new JMenuItem("Save");
    private JButton showAllButton = new JButton("Display all customers");

    private ArrayList<Customer> customers = new ArrayList <Customer> (); //Constructor
    public MainWindow(){
    this.setLayout(new MigLayout());
    this.setJMenuBar(menuBar);
    menuBar.add(fileMenu);
    menuBar.add(fileEdit);
    menuBar.add(fileHelp);
    fileMenu.add(newMenuItem);
    fileMenu.add(openMenuItem);
    fileMenu.add(saveMenuItem);
    this.add(showAllButton);



    showAllButton.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent event)
    {
    if (customers.isEmpty())
    {
    JOptionPane.showMessageDialog(MainWindow.this,"No customers!"); }
    else 
    {
    System.out.println(customers.toString());
    }
    }
    }); newMenuItem.addActionListener(new ActionListener() 
    {
    public void actionPerformed(ActionEvent event) 
    {
    CustomerDialog dialog = new CustomerDialog(MainWindow.this,true);
    dialog.setLocationRelativeTo(MainWindow.this);
    dialog.setVisible(true);
    Customer customer = dialog.getCustomer();
    if (customer != null)
    {
    customers.add(customer); } 
    }
    });
    } //Main method
    public static void main(String[] args) {
    try {
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

    catch (Exception ignored){ }
    MainWindow mainWindow = new MainWindow();
    mainWindow.setDefaultCloseOperation(EXIT_ON_CLOSE);
    mainWindow.setSize(400,300);
    mainWindow.setLocationRelativeTo(null);
    mainWindow.setVisible(true);
    }
    }
      

  2.   

    Class Customer
    package dialog;public class Customer {
    private String name;
    private String address;
    private double balance; public Customer(String name, String address, double balance) 
    {
    this.name = name;
    this.address = address;
    this.balance = balance;
    } public String getName() {
    return name;
    } public void setName(String name) {
    this.name = name;
    } public String getAddress() {
    return address;
    } public void setAddress(String address) {
    this.address = address;
    } public double getBalance() {
    return balance;
    } public void setBalance(double balance) {
    this.balance = balance;
    } public String toString() {
    return "Customer [address=" + address + ", balance=" + balance
    + ", name=" + name + "]";
    }
    }