質問

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);
}
役に立ちましたか?

解決

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...

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

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

他のヒント

BorderLayout is a default layout for JFrame. All the components in your code are added to BorderLayout.CENTER when there is no argument in add() method. So only password appears in BorderLayout.CENTER as it replaces other components. 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:

enter image description here

EDIT:

From BorderLayout spec:

As a convenience, BorderLayout interprets the absence of a string specification the same as the constant CENTER:

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

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. Take a look here: http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html

Also check miglayout.net

You need to add all the items to the JPanel type, and then add the JPanel component to the 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帰属
所属していません StackOverflow
scroll top