简体   繁体   English

Java Swing组件未显示

[英]Java Swing components not showing

Help me! 帮我! Whenever I try to start up the code below, it shows only the button on the bottom and the password field everywhere else. 每当我尝试启动下面的代码时,它只会在底部显示按钮,而在其他任何地方都显示密码字段。 I want to be able to see everything, but I can't 我希望能够看到所有内容,但我看不到

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 is a default layout for JFrame . BorderLayoutJFrame的默认布局。 All the components in your code are added to BorderLayout.CENTER when there is no argument in add() method. add()方法中没有参数时,代码中的所有组件都将添加到BorderLayout.CENTER中。 So only password appears in BorderLayout.CENTER as it replaces other components. 因此,只有password出现在BorderLayout.CENTER因为它会替换其他组件。 Try to create a panel, populate it with controls and add this panel to the frame, ie: 尝试创建一个面板,用控件填充它,然后将此面板添加到框架中,即:

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);

Here how it looks like: 这里看起来像:

在此处输入图片说明

EDIT: 编辑:

From BorderLayout spec: 根据BorderLayout规范:

As a convenience, BorderLayout interprets the absence of a string specification the same as the constant CENTER: 为方便起见,BorderLayout将字符串说明的缺失解释为与常量CENTER相同:

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

This 这个

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

Will replace anything else you've added to your screen... 将替换您添加到屏幕上的所有内容...

This, will add the button to the bottom of the screen... 这会将按钮添加到屏幕底部...

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

You could change the layout to a FlowLayout , that will display everything... 您可以将布局更改为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);

But I hardly think that's what you really want. 但是我几乎不认为那是您真正想要的。

Take a read through 通读

And see if you can find one or more layouts that suit your requirements 并查看是否可以找到满足您要求的一种或多种布局

This is quick fix: 这是快速修复:

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);
}

However you need to further learn about LayoutManagers. 但是,您需要进一步了解LayoutManager。 Take a look here: http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html 在这里看看: http : //docs.oracle.com/javase/tutorial/uiswing/layout/using.html

Also check miglayout.net 还要检查miglayout.net

You need to add all the items to the JPanel type , and then add the JPanel component to the JFrame; 您需要将所有项目添加到JPanel 类型 ,然后将JPanel组件添加到JFrame。 here is an example 这是一个例子


        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);
  


That should simply work. 那应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM