2012-10-24 152 views
1

帮帮我!每当我尝试启动下面的代码时,它只显示底部的按钮和其他地方的密码字段。我希望能够看到的一切,但我不能未显示Java Swing组件

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

回答

4

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

将取代其他任何你添加到您的屏幕......

此,将按钮添加到屏幕的底部...

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

你可以布局更改为FlowLayout,将显示一切......

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

但我几乎认为这是你真正想要的。

取读通过

,看看你是否能找到适合您的要求的一个或多个布局

+0

+1相同的图像:) – tenorsax

+0

@Max啊,伟大的思想:) – MadProgrammer

+0

+1暗示的Java教程 –

4

BorderLayoutJFrame默认布局。当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); 

这怎么看起来像:

enter image description here

编辑:

BorderLayout spec:

为方便起见,BorderLayout解释了ab​​sen字符串 规范的CE一样常量CENTER:

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

+1打我..... – MadProgrammer

2

这是快速修复:

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

不过,还需要进一步了解布局管理。看看这里: http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html

还要检查miglayout.net

2

你需要的所有项目添加到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); 
 


这应该只是工作。

+0

您可能要添加的JPanel使用的FlowLayout默认。如果OP想在'type'面板中使用不同的布局,他们可以简单地调用'type.setLayout()'来改变它。 –