[英]Java Swing components not showing
幫我! 每當我嘗試啟動下面的代碼時,它只會在底部顯示按鈕,而在其他任何地方都顯示密碼字段。 我希望能夠看到所有內容,但我看不到
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);
}
BorderLayout
是JFrame
的默認布局。 當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);
這里看起來像:
編輯:
根據BorderLayout規范:
為方便起見,BorderLayout將字符串說明的缺失解釋為與常量CENTER相同:
Panel p2 = new Panel(); p2.setLayout(new BorderLayout()); p2.add(new TextArea()); // Same as p.add(new TextArea(), BorderLayout.CENTER);
這個
frame.getContentPane().add(password,BorderLayout.CENTER);
將替換您添加到屏幕上的所有內容...
這會將按鈕添加到屏幕底部...
frame.getContentPane().add(submit,BorderLayout.SOUTH);
您可以將布局更改為FlowLayout
,它將顯示所有內容...
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);
但是我幾乎不認為那是您真正想要的。
通讀
並查看是否可以找到滿足您要求的一種或多種布局
這是快速修復:
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);
}
但是,您需要進一步了解LayoutManager。 在這里看看: http : //docs.oracle.com/javase/tutorial/uiswing/layout/using.html
還要檢查miglayout.net
您需要將所有項目添加到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);
那應該工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.