Bar*_*dos 1 java layout swing layout-manager flowlayout
帮我!每当我尝试启动下面的代码时,它只显示底部的按钮和其他地方的密码字段.我希望能够看到一切,但我不能
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);
}
Run Code Online (Sandbox Code Playgroud)
BorderLayout是默认布局JFrame.BorderLayout.CENTER当add()方法中没有参数时,代码中的所有组件都会被添加.所以只有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);
Run Code Online (Sandbox Code Playgroud)
这是它的样子:

编辑:
来自BorderLayout规范:
为方便起见,BorderLayout解释缺少字符串规范与常量CENTER相同:
Run Code Online (Sandbox Code Playgroud)Panel p2 = new Panel(); p2.setLayout(new BorderLayout()); p2.add(new TextArea()); // Same as p.add(new TextArea(), BorderLayout.CENTER);