2010-06-08 35 views
1

我有一個簡單的表單設置,我跟隨着本書Core Java Vol I如何設置表單,它們有一個頂級類,它擴展了JFrame創建不同組件的對象,並將它們放在框架中。如果這不是「通常」應該這樣做的方式,請讓我知道。混淆了內部類,簡單的動作

當我按下按鈕時編寫內部類來執行操作時,我感到困惑。我可以把我的內部類放在我的AddressBookForm類中,因爲這是我放置按鈕的位置,它可以訪問JTextFields的私有變量。我有問題(假設到目前爲止其他部分都是'好'),我不知道如何獲取addressList.size(),所以我可以更新我的statusBar標籤與插入按鈕時的總人數按下。有什麼想法嗎?謝謝。

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import java.util.List; 
import java.util.ArrayList; 

public class AddressBook 
{ 
    public static void main(String[] args) 
    { 
     EventQueue.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       AddressBookFrame frame = new AddressBookFrame(); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

       JMenuBar menuBar = new JMenuBar(); 
       frame.setJMenuBar(menuBar); 

       JMenu fileMenu = new JMenu("File"); 
       JMenuItem openItem = new JMenuItem("Open"); 
       JMenuItem saveItem = new JMenuItem("Save"); 
       JMenuItem saveAsItem = new JMenuItem("Save As"); 
       JMenuItem printItem = new JMenuItem("Print"); 
       JMenuItem exitItem = new JMenuItem("Exit"); 
       fileMenu.add(openItem); 
       fileMenu.add(saveItem); 
       fileMenu.add(saveAsItem); 
       fileMenu.add(printItem); 
       fileMenu.add(exitItem); 
       menuBar.add(fileMenu); 

       JMenu editMenu = new JMenu("Edit"); 
       JMenuItem newItem = new JMenuItem("New"); 
       JMenuItem editItem = new JMenuItem("Edit"); 
       JMenuItem deleteItem = new JMenuItem("Delete"); 
       JMenuItem findItem = new JMenuItem("Find"); 
       JMenuItem firstItem = new JMenuItem("First"); 
       JMenuItem previousItem = new JMenuItem("Previous"); 
       JMenuItem nextItem = new JMenuItem("Next"); 
       JMenuItem lastItem = new JMenuItem("Last"); 
       editMenu.add(newItem); 
       editMenu.add(editItem); 
       editMenu.add(deleteItem); 
       editMenu.add(findItem); 
       editMenu.add(firstItem); 
       editMenu.add(previousItem); 
       editMenu.add(nextItem); 
       editMenu.add(lastItem); 
       menuBar.add(editMenu); 

       JMenu helpMenu = new JMenu("Help"); 
       JMenuItem documentationItem = new JMenuItem("Documentation"); 
       JMenuItem aboutItem = new JMenuItem("About"); 
       helpMenu.add(documentationItem); 
       helpMenu.add(aboutItem); 

       menuBar.add(helpMenu); 

       frame.setVisible(true); 

      } 
     }); 
    } 
} 

class AddressBookFrame extends JFrame 
{ 
    public AddressBookFrame() 
    { 
     setLayout(new BorderLayout()); 
     setTitle("Address Book"); 
     setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); 
     AddressBookToolBar toolBar = new AddressBookToolBar(); 
     add(toolBar, BorderLayout.NORTH); 
     AddressBookStatusBar aStatusBar = new AddressBookStatusBar(); 
     add(aStatusBar, BorderLayout.SOUTH); 
     AddressBookForm form = new AddressBookForm(); 
     add(form, BorderLayout.CENTER); 
    } 
    public static final int DEFAULT_WIDTH = 500; 
    public static final int DEFAULT_HEIGHT = 500; 

} 

/* Create toolbar buttons and add buttons to toolbar */ 
class AddressBookToolBar extends JPanel 
{ 
    public AddressBookToolBar() 
    { 
     setLayout(new FlowLayout(FlowLayout.LEFT)); 
     JToolBar bar = new JToolBar(); 
     JButton newButton = new JButton("New"); 
     JButton editButton = new JButton("Edit"); 
     JButton deleteButton = new JButton("Delete"); 
     JButton findButton = new JButton("Find"); 
     JButton firstButton = new JButton("First"); 
     JButton previousButton = new JButton("Previous"); 
     JButton nextButton = new JButton("Next"); 
     JButton lastButton = new JButton("Last"); 
     bar.add(newButton); 
     bar.add(editButton); 
     bar.add(deleteButton); 
     bar.add(findButton); 
     bar.add(firstButton); 
     bar.add(previousButton); 
     bar.add(nextButton); 
     bar.add(lastButton); 
     add(bar); 
    } 
} 

/* Creates the status bar string */ 
class AddressBookStatusBar extends JPanel 
{ 
    public AddressBookStatusBar() 
    { 
     setLayout(new FlowLayout(FlowLayout.LEFT)); 
     this.statusBarString = new JLabel("Total: " + totalContacts); 
     add(this.statusBarString); 
    } 

    public void updateLabel(int n) 
    { 
     contactsLabel.setText((new Integer(n)).toString()); 
    } 

    private JLabel statusBarString; 
    private JLabel contactsLabel; 
} 

class AddressBookForm extends JPanel 
{ 
    public AddressBookForm() 
    { 
     // create form panel 
     this.setLayout(new GridLayout(2, 1)); 
     JPanel formPanel = new JPanel(); 
     formPanel.setLayout(new GridLayout(4, 2)); 
     firstName = new JTextField(20); 
     lastName = new JTextField(20); 
     telephone = new JTextField(20); 
     email = new JTextField(20); 
     JLabel firstNameLabel = new JLabel("First Name: ", JLabel.LEFT); 
     formPanel.add(firstNameLabel); 
     formPanel.add(firstName); 
     JLabel lastNameLabel = new JLabel("Last Name: ", JLabel.LEFT); 
     formPanel.add(lastNameLabel); 
     formPanel.add(lastName); 
     JLabel telephoneLabel = new JLabel("Telephone: ", JLabel.LEFT); 
     formPanel.add(telephoneLabel); 
     formPanel.add(telephone); 
     JLabel emailLabel = new JLabel("Email: ", JLabel.LEFT); 
     formPanel.add(emailLabel); 
     formPanel.add(email); 
     add(formPanel); 

     // create button panel 
     JPanel buttonPanel = new JPanel(); 
     JButton insertButton = new JButton("Insert"); 
     JButton displayButton = new JButton("Display"); 

     ActionListener insertAction = new AddressBookListener(); 
     ActionListener displayAction = new AddressBookListener(); 
     insertButton.addActionListener(insertAction); 
     displayButton.addActionListener(displayAction); 
     buttonPanel.add(insertButton); 
     buttonPanel.add(displayButton); 
     add(buttonPanel); 
    } 

    public int getTotalContacts() 
    { 
     return addressList.size(); 
    } 

    private JTextField firstName; 
    private JTextField lastName; 
    private JTextField telephone; 
    private JTextField email; 
    private JLabel contacts; 

    private List<Person> addressList = new ArrayList<Person>(); 

    private class AddressBookListener implements ActionListener 
    { 
     public void actionPerformed(ActionEvent e) 
     { 
      String buttonPressed = e.getActionCommand(); 
      System.out.println(buttonPressed); 
      if (buttonPressed == "Insert") { 
       Person aPerson = new Person(firstName.getText(), lastName.getText(), telephone.getText(), email.getText()); 
       addressList.add(aPerson); 
      } 
      else { 
       for (Person p : addressList) { 
        System.out.println(p); 
       } 
      } 
     } 
    } 

} 

回答

1

不要在組件中存儲人員列表,這是緊密耦合。

由服務類或接口,提供人員名單,像這樣

public interface AddressBookService { 

    List<Person> getContacts(); 

    void addContact(Person contact); 

    void deleteContact(Person person); 

} 

現在給你的每一個組件的訪問此服務,或者通過注射,或通過使用它作爲一個單身或任何其他查找方法。

+0

我不認爲我完全得到它。所以這個服務是一個幫手類。我會在哪裏申報我的名單?它是一個類的私有變量嗎?我想我沒有看到兩個單獨的類將如何訪問相同的列表。 – Crystal 2010-06-10 04:19:00

+0

您在助手類中聲明瞭列表,將輔助類注入組件並調用addressBookService.getContacts(),而不是在本地存儲列表。 – 2010-06-10 04:39:18

0

我投seanizer的解決方案,但如果你堅持要用你的代碼,你可以利用在其內部類這種方式訪問​​外部類的AddressList中的變量(AddressBookFrame)的:

class AddressBookFrame extends JFrame { 

private List<Person> addressList = new ArrayList<Person>(); 

public AddressBookFrame() { 
    setLayout(new BorderLayout()); 
    setTitle("Address Book"); 
    setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); 
    AddressBookToolBar toolBar = new AddressBookToolBar(); 
    add(toolBar, BorderLayout.NORTH); 
    AddressBookStatusBar aStatusBar = new AddressBookStatusBar(); 
    add(aStatusBar, BorderLayout.SOUTH); 
    AddressBookForm form = new AddressBookForm(); 
    add(form, BorderLayout.CENTER); 
} 

public static final int DEFAULT_WIDTH = 500; 
public static final int DEFAULT_HEIGHT = 500; 

/* Creates the status bar string */ 
class AddressBookStatusBar extends JPanel { 
    public AddressBookStatusBar() { 
     setLayout(new FlowLayout(FlowLayout.LEFT)); 
     this.statusBarString = new JLabel("Total: " + addressList.size()); 
     add(this.statusBarString); 
    } 

    public void updateLabel(int n) { 
     contactsLabel.setText((new Integer(n)).toString()); 
    } 

    private JLabel statusBarString; 
    private JLabel contactsLabel; 
} 

/* Create toolbar buttons and add buttons to toolbar */ 
class AddressBookToolBar extends JPanel { 
    public AddressBookToolBar() { 
     setLayout(new FlowLayout(FlowLayout.LEFT)); 
     JToolBar bar = new JToolBar(); 
     JButton newButton = new JButton("New"); 
     JButton editButton = new JButton("Edit"); 
     JButton deleteButton = new JButton("Delete"); 
     JButton findButton = new JButton("Find"); 
     JButton firstButton = new JButton("First"); 
     JButton previousButton = new JButton("Previous"); 
     JButton nextButton = new JButton("Next"); 
     JButton lastButton = new JButton("Last"); 
     bar.add(newButton); 
     bar.add(editButton); 
     bar.add(deleteButton); 
     bar.add(findButton); 
     bar.add(firstButton); 
     bar.add(previousButton); 
     bar.add(nextButton); 
     bar.add(lastButton); 
     add(bar); 
    } 
} 

class AddressBookForm extends JPanel { 
    public AddressBookForm() { 
     // create form panel 
     this.setLayout(new GridLayout(2, 1)); 
     JPanel formPanel = new JPanel(); 
     formPanel.setLayout(new GridLayout(4, 2)); 
     firstName = new JTextField(20); 
     lastName = new JTextField(20); 
     telephone = new JTextField(20); 
     email = new JTextField(20); 
     JLabel firstNameLabel = new JLabel("First Name: ", JLabel.LEFT); 
     formPanel.add(firstNameLabel); 
     formPanel.add(firstName); 
     JLabel lastNameLabel = new JLabel("Last Name: ", JLabel.LEFT); 
     formPanel.add(lastNameLabel); 
     formPanel.add(lastName); 
     JLabel telephoneLabel = new JLabel("Telephone: ", JLabel.LEFT); 
     formPanel.add(telephoneLabel); 
     formPanel.add(telephone); 
     JLabel emailLabel = new JLabel("Email: ", JLabel.LEFT); 
     formPanel.add(emailLabel); 
     formPanel.add(email); 
     add(formPanel); 

     // create button panel 
     JPanel buttonPanel = new JPanel(); 
     JButton insertButton = new JButton("Insert"); 
     JButton displayButton = new JButton("Display"); 

     ActionListener insertAction = new AddressBookListener(); 
     ActionListener displayAction = new AddressBookListener(); 
     insertButton.addActionListener(insertAction); 
     displayButton.addActionListener(displayAction); 
     buttonPanel.add(insertButton); 
     buttonPanel.add(displayButton); 
     add(buttonPanel); 
    } 

    public int getTotalContacts() { 
     return addressList.size(); 
    } 

    private JTextField firstName; 
    private JTextField lastName; 
    private JTextField telephone; 
    private JTextField email; 
    private JLabel contacts; 

    private class AddressBookListener implements ActionListener { 
     public void actionPerformed(ActionEvent e) { 
      String buttonPressed = e.getActionCommand(); 
      System.out.println(buttonPressed); 
      if (buttonPressed == "Insert") { 
       Person aPerson = new Person(firstName.getText(), lastName 
         .getText(), telephone.getText(), email.getText()); 
       addressList.add(aPerson); 
      } else { 
       for (Person p : addressList) { 
        System.out.println(p); 
       } 
      } 
     } 
    } 

} 

}