2011-07-01 16 views
1

我有一個程序,我有一個JFrame其中JButton。當用戶單擊JButton時,將刪除JFrame的所有Components,並向其添加帶有紅色背景的JPanel如何在JFrame可見時將組件添加到JFrame中,而無需調整它的大小?

當我點擊JButton時,除非我調整JFrame(我正在使用Windows 7)的大小,否則該紅色JPanel不可見。有沒有辦法實現我想要的而不必手動調整JFrame的大小?

這裏是我使用的代碼的一部分:

public class Demo implements ActionListener{ 
    public static void main(String args[]){ 
      ............... 
     button.addActionListener(this); //'button' is an object of Jbutton class. 
     frame.setVisible(true); //'frame' is an object of JFrame class. 
     ............ 
    } 

    public void actionPerformed(ActionEvent ae){ 
     frame.removeAllComponents(); 
     frame.add(panel1); //panel1 is an object of Jpanel class with red background. 

     /* Here is where my problem lies. 
      panel1 is not visible to me unless I manually resize the JFrame. */ 
    } 
} 
+2

考慮使用'CardLayout'。參見[E.G.](http://stackoverflow.com/questions/5665156/calling-awt-frame-methods-from-subclass/5786005#5786005)。 –

+0

在panel1的構造函數中,是否調用jFrame上的.pack()? –

+0

奇怪的是,我發現在實際情況下這實際上更正確。 – mre

回答

5

用於去除JPanel(然後,例如,添加新JComponents)JComponentstop-level containers你要打電話,只有一次,在結束動作:

revalidate(); 
repaint(); 

如果你只調整或更改JComponents:

validate(); 
repaint(); 
+0

是一個'重繪()'真的必要? – mre

+0

@little bunny foo foo對於LayoutManager的正確工作是的,例如http://stackoverflow.com/questions/6390240/dynamic-generation-of-buttons-in-java/6395105#6395105 – mKorbel

+1

你能看到我的文章嗎?我想徵求你對我的發現的看法。 :) – mre

-1

,你必須強制重繪()的框架,使框架具有重繪自己。

4

對我而言,這有點奇怪。事實證明,調用remove(Component comp),添加新的JPanel,然後調用pack()爲我工作。

public class Demo{ 

    public static void main(String[] args){ 
     SwingUtilities.invokeLater(new Runnable(){ 
      @Override 
      public void run(){ 
       createAndShowGUI(); 
      } 
     }); 
    } 

    private static void createAndShowGUI(){ 
     final JFrame frame = new JFrame(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     final JPanel panel = new JPanel(); 
     final JButton button = new JButton("Press Me"); 
     button.addActionListener(new ActionListener(){ 
      @Override 
      public void actionPerformed(ActionEvent e){ 
       frame.remove(panel); 

       final JPanel redPanel = new JPanel(){ 

        @Override 
        public Dimension getPreferredSize(){ 
         return new Dimension(200, 200); 
        } 

        @Override 
        protected void paintComponent(Graphics g){ 
         Graphics g2 = g.create(); 

         g2.setColor(Color.RED); 
         g2.fillRect(0, 0, getWidth(), getHeight()); 

         g2.dispose(); 
        } 
       }; 

       frame.add(redPanel); 
       frame.pack(); 
      } 
     }); 

     panel.add(button); 
     frame.add(panel); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 
} 

BEFORE按下按鈕

enter image description here

AFTER按下按鈕

enter image description here

古怪

  1. 調用removeAll()實際上導致GUI凍結。看來這個事件發生了before。即使我在刪除所有組件之前嘗試刪除動作偵聽器,也發生這種情況。
  2. 我不需要調用任何驗證方法,甚至不需要重新繪製GUI。
+0

兔子富富,碰到好抓+1 – mKorbel

+0

調用*包()*不止一次不好講編程都,這裏在我的崗位看出:http://stackoverflow.com/questions/34604487/changing-屏幕上的分量換的JFrame-是-笨重和 - 凌亂-java的 呼叫* pack()的*創建其中部件被分配到存儲器中並且被顯示在屏幕上不以線性順序發生的競爭條件就像你想的那樣。如果你想刪除的東西,然後用** ** CardLayout,包括兩個窗格,一個有和一個沒有一定的分量,然後翻轉到窗格時,需要的話。 **你只應該打電話給pack()一次。** –