2010-06-07 67 views
1

有兩個問題。第一個問題是我試圖創建一個簡單的表單,當你按下按鈕時,它將一個Person對象添加到ArrayList。然而,由於我不習慣GUI,因此我嘗試創建一個,並且首先嚐試從JTextField獲取用戶輸入,創建適當類型的ActionListener對象,因此一旦這個對象有效,那麼我就可以傳入所有的JTextField輸入來創建我的Person對象。不幸的是,當我輸入名字的JTextField時,我沒有收到任何數據,並想知道是否有人可以查看我的代碼。在Java中傳遞ActionListeners,pack()

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("5"); 
     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(String statusBarString) 
    { 
     setLayout(new FlowLayout(FlowLayout.LEFT)); 
     this.statusBarString = new JLabel("Total number of people: " + statusBarString); 
     add(this.statusBarString); 
    } 
    private JLabel statusBarString; 
    private int totalContacts; 
} 

class AddressBookForm extends JPanel 
{ 
    public AddressBookForm() 
    { 
     this.setLayout(new GridLayout(2, 1)); 
     JPanel formPanel = new JPanel(); 
     formPanel.setLayout(new GridLayout(4, 2)); 
     JTextField firstName = new JTextField(20); 
     JTextField lastName = new JTextField(20); 
     JTextField telephone = new JTextField(20); 
     JTextField 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); 

     JPanel buttonPanel = new JPanel(); 
     JButton insertButton = new JButton("Insert"); 
     JButton displayButton = new JButton("Display"); 

     // create button actions 
     AddressBookManager insertAction = new AddressBookManager(firstName.getText()); 
     insertButton.addActionListener(insertAction); 
     buttonPanel.add(insertButton); 
     buttonPanel.add(displayButton); 
     add(buttonPanel); 
    } 

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

    private class AddressBookManager implements ActionListener 
    { 
     public AddressBookManager(String text) 
     { 
//   addressList.add(
      setName(text); 
      System.out.println("Test" + text); 
     } 

     public void actionPerformed(ActionEvent e) 
     { 
      System.out.println("Hello" + name); 
     } 

     public void setName(String name) 
     { 
      this.name = name; 
     } 

     private String name; 
    } 

} 

第二個問題是,我該如何讓我的表單不佔用整個中心空間。我不喜歡伸展的外觀,並希望JTextFields可能只是一行,而不是一個大框。與按鈕相同的東西。有什麼想法嗎?謝謝。

回答

2

當我輸入firstNameJTextField的東西時,我沒有收到任何數據。

您正在餵食AddressBookManager一個空字符串。作爲一種權宜之計,讓它看到父母感興趣的領域,AddressBookForm。附錄:隨着您的應用程序的發展,請考慮How to Use Actions中描述的組織優勢。

class AddressBookForm extends JPanel { 
    JTextField firstName = new JTextField(20); 
    JTextField lastName = new JTextField(20); 
    JTextField telephone = new JTextField(20); 
    JTextField email = new JTextField(20); 

    public AddressBookForm() { 
    ... 
    } 

    private class Person { ... } 

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

    private class AddressBookManager implements ActionListener { 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      addressList.add(new Person(...)); 
      System.out.println("Hello, " + firstName.getText()); 
     } 

     public void setName(String name) { 
      firstName.setText(name); 
     } 
    } 
} 

如何讓我的形式不會佔據整個中心的空間?

參見Using Layout ManagersGridLayout忽略首選尺寸;我會考慮嵌套BoxLayout