[英]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.