2014-06-13 61 views
0
import java.awt.*; 
import javax.swing.*; 

public class Crisis extends JFrame { 

    public Crisis() { 
     super("Crisis"); 
     setLookAndFeel(); 
     setSize(348, 400); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     panicButton = new JButton("Panic"); 
     dontPanicButton = new JButton("Don't Panic"); 
     blameButton = new JButton("Blame Others"); 
     mediaButton = new JButton("Notify the Media"); 
     saveButton = new JButton("Save Yourself"); 
     JPanel pane = new JPanel(); 
     BorderLayout moo = new BorderLayout(); 
     pane.setLayout(moo); 
     pane.add(panicButton, BorderLayout.NORTH); 
     pane.add(dontPanicButton, BorderLayout.SOUTH); 
     add(pane); 
     FlowLayout flo = new FlowLayout(FlowLayout.CENTER,10,10); 
     JPanel noo = new JPanel(); 
     noo.setLayout(flo); 
     noo.add(blameButton); 
     noo.add(mediaButton); 
     noo.add(saveButton); 
     add(noo); 
     setVisible(true); 
    } 


    public static void main(String[] arguments) { 
     new Crisis(); 
    } 
} 

panicButton和dontPanicButton不GUI顯示所有按鈕不顯示

+2

問題語句是從標題清晰*所有按鈕不顯示*,它是完整的可測試代碼重現問題。 – Braj

+1

@Braj這並沒有改變這個事實,即它是一個代碼轉儲,而不需要在OP一側解決問題。 – Anonymous

+0

正如你所知道的,有時代碼中的一個非常愚蠢的錯誤可能會浪費數小時來找到它。有時它也發生在我身上,並可能與你在一起... – Braj

回答

3

JFrame默認使用BorderLayout,您可以在每個部分北,南,西,東,中心只添加單一成分。

您正在中心添加兩個組件,只有最後一個組件可見。

add(pane); // Added in center 
... 
add(noo); // Added in center and replaced last one  <<-- Here is the problem 

添加它在不同的部分(段)或使用其他佈局,根據您的需要。


更多