2011-08-14 177 views
5

我有以下代碼:按鈕更改面板尺寸點擊

package in.res.num.tapb.ui; 

import java.awt.BorderLayout; 
import java.awt.CardLayout; 
import java.awt.Dimension; 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.BorderFactory; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 

class MainClass extends JPanel { 
    public MainClass() { 
     Registration registration = new Registration(); 
     ButtonPanel buttonPanel = new ButtonPanel(); 
     buttonPanel.setRegistration(registration); 

     buttonPanel.setBorder(BorderFactory.createTitledBorder("Button Panel")); 
     registration.setBorder(BorderFactory.createTitledBorder("Registration Panel")); 

     setLayout(new BorderLayout()); 
     add(registration, BorderLayout.CENTER); 
     add(buttonPanel, BorderLayout.SOUTH); 
    } 

    private static void createAndShowUI() { 
     JFrame frame = new JFrame("Registration"); 
     frame.getContentPane().add(new MainClass()); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setResizable(false); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     java.awt.EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       createAndShowUI(); 
      } 
     }); 
    } 

    @SuppressWarnings("serial") 
    private class ButtonPanel extends JPanel { 
     private Registration registration; 

     public ButtonPanel() { 
      setLayout(new GridLayout(1, 0, 10, 0));  
      for (final String keyText : Registration.KEY_TEXTS) { 
       JButton btn = new JButton(keyText); 
       btn.addActionListener(new ActionListener() { 
        public void actionPerformed(ActionEvent e) { 
         if (registration != null) { 
          registration.swapView(keyText); 
         } 
        } 
       }); 
       add(btn); 
      } 
     } 

     public void setRegistration(Registration registration) { 
      this.registration = registration; 
     } 
    } 

    private static class Registration extends JPanel { 
     private static final Dimension PREF_SIZE = new Dimension(450, 300); 
     public static final String USER_AGREEMENT = "User Agreement"; 
     public static final String USER_INFO = "User Information"; 
     public static final String ENROLLMENT = "Enrollment"; 
     public static final String[] KEY_TEXTS = { USER_AGREEMENT, USER_INFO, ENROLLMENT }; 
     private CardLayout cardlayout = new CardLayout(); 
     private JPanel cards = new JPanel(cardlayout); 

     public Registration() { 
      cards.add(createUserAgreePanel(), USER_AGREEMENT); 
      cards.add(createUserInfoPanel(), USER_INFO); 
      cards.add(createEnrollmentPanel(), ENROLLMENT); 
      setLayout(new BorderLayout()); 
      add(cards, BorderLayout.CENTER); 
     } 



     private JPanel createEnrollmentPanel() { 
      JPanel enrol = new JPanel(); 
      enrol.setSize(new Dimension(400, 200)); 
      enrol.add(new JLabel("Enrollment")); 
      return enrol; 
     } 

     private JPanel createUserAgreePanel() { 
      JPanel userAgree = new JPanel(); 
      userAgree.setSize(new Dimension(200, 300)); 
      userAgree.add(new JLabel("User Agreement")); 
      return userAgree; 
     } 

     private JPanel createUserInfoPanel() { 
      JPanel userInfo = new JPanel(); 
      userInfo.setSize(new Dimension(300, 400)); 
      userInfo.add(new JLabel("User Information")); 
      return userInfo; 
     } 

     public void swapView(String key) { 
      cardlayout.show(cards, key); 
     } 

    } 

} 

正如你可以看到我想改變按一下按鈕的大小。可能嗎?上面的代碼不起作用,我的意思是大小沒有變化。我怎樣才能改變大小?

感謝和問候。 編輯:

交換關於選擇JList中的一個行的面板。

getChoicesList().addListSelectionListener(new ListSelectionListener() { 

     @Override 
     public void valueChanged(ListSelectionEvent listSelectionEvent) { 
      getViewPanel().changeView(getChoicesList().getSelectedIndex()); 
      getChoicePanel().changeView(Constants.PanelInfo.valueOf(getEngine().getChoiceList().get(getChoicesList().getSelectedIndex()).getEnumName()).getDimensionForScrollPaneOfChoicePanel()); 
      ((MainFrame) getTopLevelAncestor()).pack(); 
     } 
    }); 

viewPanel中#changeView(),此交換面板:

public void changeView(int index) { 
    removeAll(); 
    getPanels().get(index).setPreferredSize(Constants.PanelInfo.valueOf(getEngine().getChoiceList().get(index).getEnumName()).getDimensionForViewPanel()); 
    add(getPanels().get(index)); 
} 
+2

可能是這可以幫助http://stackoverflow.com/questions/3889498/how-can-i-increase-decrease-size-of-window-on-click-event – Shadow

+0

+1謝謝... –

+0

+1 [SSCCE(http://sscce.org/)。 – trashgod

回答

3

的setSize()使用佈局管理器時,你不應該使用。確定尺寸是佈局經理的工作。您可以通過設置peferred或最小或最大尺寸向佈局管理器提供提示。不過,建議您不要這樣做,因爲組件和麪板應該按照他們的首選尺寸顯示,這將由您使用的佈局管理器決定。如果你沒有覆蓋的大小,則代碼應該是:

// enrol.setSize(new Dimension(400, 200)); 
enrol.setPreferredSize(new Dimension(400, 200)); 

然而,這仍然無法工作,你所希望的方式,因爲CardLayout的任務是確定添加到面板中的所有面板的最大尺寸使用CardLayout。所以當你從面板切換到面板時,你不會獲得每個面板的大小。這對用戶來說是更好的體驗,因爲用戶不希望每次按下按鈕時都會看到幀大小不斷變化。

如果你確實想你點擊一個按鈕,每一次有框架改變大小,則基本代碼將是:

mainPanel.remove(oldPanel); 
mainPanel.add(newPanel); 
frame.pack(); 

然後在主面板的佈局管理器將觀察的首選大小newlay增加了面板。

4

調整JFrame中,使用後: yourframe.validate();

+1

調整幀大小將導致幀被驗證。 – camickr

3

作爲camickr的answer的一個具體示例,下面的程序顯示瞭如何通過pack()調整框架的大小來依賴組件的首選尺寸。該人造內容是一系列的標籤,但任何JComponent會做。由於內容是動態重新創建的,因此可以根據程序中的其他條件進行更改。

enter image description here

import java.awt.BorderLayout; 
import java.awt.EventQueue; 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.BorderFactory; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 

/** @see https://stackoverflow.com/questions/7059278 */ 
class MainPanel extends JPanel { 

    private static final String title = "Registration Panel"; 
    private JFrame frame = new JFrame(title); 
    private JPanel registration = new JPanel(); 

    public MainPanel() { 
     this.setLayout(new BorderLayout()); 
     registration.setBorder(BorderFactory.createTitledBorder(title)); 
     registration.add(PanelType.USER_AGREEMENT.panel); 
     ButtonPanel buttonPanel = new ButtonPanel(); 
     buttonPanel.setBorder(BorderFactory.createTitledBorder("Button Panel")); 
     add(registration, BorderLayout.CENTER); 
     add(buttonPanel, BorderLayout.SOUTH); 
    } 

    private void display() { 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.add(new MainPanel()); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       new MainPanel().display(); 
      } 
     }); 
    } 

    private class ButtonPanel extends JPanel { 

     public ButtonPanel() { 
      for (final PanelType panel : PanelType.values()) { 
       final JButton button = panel.button; 
       this.add(button); 
       button.addActionListener(new ActionListener() { 

        @Override 
        public void actionPerformed(ActionEvent e) { 
         registration.removeAll(); 
         registration.add(panel.create()); 
         frame.pack(); 
         frame.setLocationRelativeTo(null); 
        } 
       }); 
      } 
     } 
    } 

    private enum PanelType { 

     USER_AGREEMENT("User Agreement", 2), 
     USER_INFO("User Information", 4), 
     ENROLLMENT("Enrollment Form", 6); 
     private String name; 
     private int count; 
     private JButton button; 
     private JPanel panel; 

     private PanelType(String name, int count) { 
      this.name = name; 
      this.count= count; 
      this.button = new JButton(name); 
      this.panel = create(); 
     } 

     private JPanel create() { 
      this.panel = new JPanel(new GridLayout(0, 1)); 
      this.panel.add(new JLabel(name)); 
      this.panel.add(new JLabel(" ")); 
      for (int i = 0; i < count; i++) { 
       this.panel.add(new JLabel("Label " + String.valueOf(i + 1))); 
          } 
      return panel; 
     } 
    } 
}