2011-04-04 67 views
0

我面臨的一個問題,我想補充不是一個按鈕,一個JFrame多,但只需要在最後一個,並將其放入框架,我的代碼示例如下:我可以將更多按鈕添加到java JFrame嗎?

String isName = ""; 

JFrame frame = new JFrame(isName); 
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 

String childAmb = "PDA276"; 

for (int j=0; j<3; j++){ 

    if (childAmb.matches("Phone\\w\\w\\w")) 
     fancyButtonCreator(childAmb, new ImageIcon ("src/phone.gif"), frame); 

    else if (childAmb.matches("PDA\\w\\w\\w")) 
     fancyButtonCreator(childAmb, new ImageIcon ("src/pda.gif"), frame); 

    else if (childAmb.matches("PC\\w\\w\\w")) 
     fancyButtonCreator(childAmb, new ImageIcon ("src/pc.gif"), frame); 
    } 

    frame.setVisible(true); 
    frame.setBounds(100, 200, 200, 200); 

謝謝您。


+3

你能告訴我們'fancyButtonCreator'中發生了什麼嗎? – jmccarthy 2011-04-04 16:30:09

回答

2

如果您沒有佈局管理器,只有一個,最後添加的組件會顯示出來。

frame.setLayout (new FlowLayout()); 
frame.add (new JButton ("foo")); 
frame.add (new JButton ("bar")); 
+0

嗯?當我沒有佈局管理器('setLayout(null);')時,我必須指定座標,並且它們都是可見的。 – 2011-04-04 17:35:17

+0

但是你應該有一個LayoutManager。如果使用AbsoluteLayout,則必須指定座標。問題在哪裏?但是,您應該更喜歡常規的LayoutManagers,它允許用戶調整組件的大小。 – 2011-04-04 17:58:42

相關問題