2012-10-24 153 views
1

幫幫我!每當我嘗試啓動下面的代碼時,它只顯示底部的按鈕和其他地方的密碼字段。我希望能夠看到的一切,但我不能未顯示Java Swing組件

public void setup(){ 
    frame = new JFrame("Votinator 3000"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    voteconfirm = new JLabel(""); 
    textarea = new JTextField(1); 
    submit = new JButton("Submit Vote!"); 
    chooser = new JList(items); 
    password = new JPasswordField(1); 
    password.setVisible(true); 
    choices = new JComboBox(); 
    choices.addItem("Choose"); 
    choices.addItem("Submit Own"); 
    type = new JPanel(); 
    type.add(textarea); 
    choices.setEditable(false); 
    choices.setSelectedIndex(0); 
    frame.setBounds(300, 300, 400, 400); 
    frame.getContentPane().add(type); 
    frame.getContentPane().add(choices); 
    frame.getContentPane().add(voteconfirm); 
    frame.getContentPane().add(chooser); 
    frame.getContentPane().add(textarea); 
    frame.getContentPane().add(password,BorderLayout.CENTER); 
    frame.getContentPane().add(submit,BorderLayout.SOUTH); 
    frame.setVisible(true); 
} 

回答

4

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

將取代其他任何你添加到您的屏幕......

此,將按鈕添加到屏幕的底部...

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

你可以佈局更改爲FlowLayout,將顯示一切......

enter image description here

frame.setLayout(new FlowLayout()); 
frame.setBounds(300, 300, 400, 400); 
frame.getContentPane().add(type); 
frame.getContentPane().add(choices); 
frame.getContentPane().add(voteconfirm); 
frame.getContentPane().add(chooser); 
frame.getContentPane().add(textarea); 
frame.getContentPane().add(password); 
frame.getContentPane().add(submit); 

但我幾乎認爲這是你真正想要的。

取讀通過

,看看你是否能找到適合您的要求的一個或多個佈局

+0

+1相同的圖像:) – tenorsax

+0

@Max啊,偉大的思想:) – MadProgrammer

+0

+1暗示的Java教程 –

4

BorderLayoutJFrame默認佈局。當add()方法中沒有參數時,代碼中的所有組件都被添加到BorderLayout.CENTER。所以只有password出現在BorderLayout.CENTER中,因爲它取代了其他組件。嘗試創建一個面板,與對照填充它,這個面板添加到框架,即:

JPanel content = new JPanel(); 
content.add(type); 
content.add(choices); 
content.add(voteconfirm); 
content.add(chooser); 
content.add(textarea); 
content.add(password); 
content.add(submit); 
frame.getContentPane().add(content); 

這怎麼看起來像:

enter image description here

編輯:

BorderLayout spec:

爲方便起見,BorderLayout解釋了ab​​sen字符串 規範的CE一樣常量CENTER:

Panel p2 = new Panel(); 
p2.setLayout(new BorderLayout()); 
p2.add(new TextArea()); // Same as p.add(new TextArea(), BorderLayout.CENTER); 
+0

+1打我..... – MadProgrammer

2

這是快速修復:

public void setup(){ 
frame = new JFrame("Votinator 3000"); 
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
voteconfirm = new JLabel(""); 
textarea = new JTextField(1); 
submit = new JButton("Submit Vote!"); 
chooser = new JList(items); 
password = new JPasswordField(1); 
password.setVisible(true); 
choices = new JComboBox(); 
choices.addItem("Choose"); 
choices.addItem("Submit Own"); 
type = new JPanel(); 
type.add(textarea); 
choices.setEditable(false); 
choices.setSelectedIndex(0); 
frame.setBounds(300, 300, 400, 400); 

JPanel p = new JPanel(); 
p.setLayout(new BoxLayout(p, BoxLayout.PAGE_AXIS)); 
frame.setContentPane(p); 

frame.getContentPane().add(type); 
frame.getContentPane().add(choices); 
frame.getContentPane().add(voteconfirm); 
frame.getContentPane().add(chooser); 
frame.getContentPane().add(textarea); 
frame.getContentPane().add(password); 
frame.getContentPane().add(submit); 
frame.setVisible(true); 
} 

不過,還需要進一步瞭解佈局管理。看看這裏: http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html

還要檢查miglayout.net

2

你需要的所有項目添加到JPanel的,然後將JPanel組件添加到JFrame;這裏有一個例子

 
    
     frame = new JFrame("Votinator 3000"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     voteconfirm = new JLabel(""); 
     textarea = new JTextField(1); 
     submit = new JButton("Submit Vote!"); 
     chooser = new JList(items); 
     password = new JPasswordField(1); 
     password.setVisible(true); 
     choices = new JComboBox(); 
     choices.addItem("Choose"); 
     choices.addItem("Submit Own"); 
     type = new JPanel(); 
     type.add(textarea); 
     choices.setEditable(false); 
     choices.setSelectedIndex(0); 
     frame.setBounds(300, 300, 400, 400); 
     frame.add(type); 
     type.add(choices); 
     type.add(voteconfirm); 
     type.add(chooser); 
     type.add(textarea); 
     type.add(password); 
     type.add(submit); 
     frame.setVisible(true); 
 


這應該只是工作。

+0

您可能要添加的JPanel使用的FlowLayout默認。如果OP想在'type'面板中使用不同的佈局,他們可以簡單地調用'type.setLayout()'來改變它。 –