Java Swing组件未显示 [英] Java Swing components not showing

查看:217
本文介绍了Java Swing组件未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

救救我!每当我尝试启动下面的代码时,它只会在底部显示按钮,而在其他任何地方都显示密码字段.我希望能够看到所有内容,但我看不到

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

推荐答案

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

您可以将布局更改为FlowLayout,它将显示所有内容...

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.

通读

  • A Visual Guide to Layout Managers
  • Using Layout Managers

并查看是否可以找到符合您要求的一种或多种布局

And see if you can find one or more layouts that suit your requirements

这篇关于Java Swing组件未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆