问题
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);
}
回答1:
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...
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
- A Visual Guide to Layout Managers
- Using Layout Managers
And see if you can find one or more layouts that suit your requirements
回答2:
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:
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);
回答3:
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
回答4:
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.
来源:https://stackoverflow.com/questions/13041915/java-swing-components-not-showing