2012-04-14 69 views
2

我有一個Swing JFrame,它有一堆菜單項。下面的代碼嘗試但未能在選擇「添加地址」菜單項時設置JFrame的內容。擺動菜單項選擇不設置contentpane中的內容

 JFrame frame = new JFrame(); 

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setResizable(false); 
    frame.setVisible(true); 

    JMenuBar menuBar = new JMenuBar(); 
    JMenu clientMenu = new JMenu("Client"); 
    JMenuItem address = new JMenuItem("Add Address"); 

    clientMenu.add(address); 
    menuBar.add(clientMenu); 

    address.addActionListener(new ActionListener() { 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      setupAddressForm(); 
     } 
    }); 

    private void setupAddressForm() { 
     frame.getContentPane().removeAll(); 

     JPanel infoPanel = new JPanel(); 
     JLabel infoLabel = new JLabel(); 
     infoLabel.setText("<html><B>Please enter the address below</B></html>"); 
     infoPanel.add(infoLabel, SwingConstants.CENTER); 
     frame.getContentPane().add(infoPanel, BorderLayout.NORTH); 

     JPanel buttonPanel = new JPanel(); 
     JButton submitButton = new JButton("Submit Address"); 
     JButton resetButton = new JButton("Reset"); 

     submitButton.setToolTipText("Submit"); 
     resetButton.setToolTipText("Reset"); 

     buttonPanel.add(submitButton); 
     buttonPanel.add(resetButton); 

     frame.getContentPane().add(buttonPanel, BorderLayout.SOUTH); 

     JPanel fieldPanel = new JPanel(); 
     fieldPanel.setLayout(new GridLayout(10, 10)); 

     JLabel houseNumberLabel = new JLabel("House Number", 
       SwingConstants.LEFT); 
     final JTextField houseNumberField = new JTextField(3); 

     JLabel streetNameLabel = new JLabel("Street Name", SwingConstants.LEFT); 
     final JTextField streetNameField = new JTextField(10); 

     JLabel localityLabel = new JLabel("Locality", SwingConstants.LEFT); 
     final JTextField localityField = new JTextField(10); 

     JLabel cityLabel = new JLabel("City", SwingConstants.LEFT); 
     final JTextField cityField = new JTextField(10); 

     JLabel stateLabel = new JLabel("State", SwingConstants.LEFT); 
     final JTextField stateField = new JTextField(10); 

     JLabel zipCodeLabel = new JLabel("Zip Code", SwingConstants.LEFT); 
     final JTextField zipCodeField = new JTextField(10); 

     fieldPanel.add(houseNumberLabel); 
     fieldPanel.add(houseNumberField); 

     fieldPanel.add(streetNameLabel); 
     fieldPanel.add(streetNameField); 

     fieldPanel.add(localityLabel); 
     fieldPanel.add(localityField); 

     fieldPanel.add(cityLabel); 
     fieldPanel.add(cityField); 

     fieldPanel.add(stateLabel); 
     fieldPanel.add(stateField); 

     fieldPanel.add(zipCodeLabel); 
     fieldPanel.add(zipCodeField); 

     frame.getContentPane().add(fieldPanel, BorderLayout.CENTER); 

     submitButton.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 

       String houseNumber = houseNumberField.getText(); 
       String street = streetNameField.getText(); 
       String locality = localityField.getText(); 
       String city = cityField.getText(); 
       String state = stateField.getText(); 
       String zipCode = zipCodeField.getText(); 

       Address address = new Address(); 

       if (!"".equals(houseNumber) && !"".equals(street) 
         && !"".equals(locality) && !"".equals(city) 
         && !"".equals(state) && !"".equals(zipCode)) { 
        address.setHouseNumber(houseNumber); 
        address.setStreetName(street); 
        address.setLocality(locality); 
        address.setState(state); 
        address.setZipCode(zipCode); 
       } 

      } 
     }); 

     frame.getContentPane().doLayout(); 
     frame.getContentPane().update(frame.getGraphics()); 

} 

當我選擇「添加地址」菜單項時,框架變爲空白。不是我所期望的。我使用斷點進行調試,並看到設置地址的方法正在被調用。 我想不通它爲什麼不起作用。難道有人指出我在這裏做錯了什麼嗎?

回答

4

爲什麼要經歷這些體操?相反,只需使用CardLayout以最小的混亂或大驚小怪爲您交換視圖。

另外請注意,您的JFrame上創建在任何一個方法或構造線(A)(你不說):

JFrame frame = new JFrame(); // (A) 

是不一樣的JFrame上線所使用的(B )和(C):

private void setupAddressForm() { 
     frame.getContentPane().removeAll(); // (B) 

     JPanel infoPanel = new JPanel(); 
     JLabel infoLabel = new JLabel(); 
     infoLabel.setText("<html><B>Please enter the address below</B></html>"); 
     infoPanel.add(infoLabel, SwingConstants.CENTER); 
     frame.getContentPane().add(infoPanel, BorderLayout.NORTH); // (C) 

這是因爲你聲明的JFrame上線(A),因此它僅僅是方法或構造函數中可見。

編輯1
此外,這是不對的:

frame.getContentPane().doLayout(); 
    frame.getContentPane().update(frame.getGraphics()); 

因爲這是不是你得到新的組件在搖擺的容器顯示。相反,我會做一些事情,像這樣:

JPanel contentPane = (JPanel) frame.getContentPane(); 
    contentPane.revalidate(); 
    contentPane.repaint(); 

但同樣,我真的沒有,而是會用CardLayout如上所述。

最後,如果其中任何建議沒有意義,請要求澄清。

+0

「這是因爲您在行(A)上聲明瞭JFrame,因此它只在該方法或構造函數中可見。」我沒有發佈上面的完整代碼。但CardLayout解決了我的問題。感謝您的解決方案。 – sachinrahulsourav 2012-04-14 16:10:55

+0

@sachinrahulsourav:歡迎您和CardLayout使用我認爲最好的工具。 – 2012-04-14 16:13:38