【问题标题】:Java Swing components not showingJava Swing 组件未显示
【发布时间】:2012-10-24 01:54:32
【问题描述】:

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

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

【问题讨论】:

    标签: java swing layout layout-manager flowlayout


    【解决方案1】:

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

    如下图:

    编辑:

    来自BorderLayout 规范:

    为方便起见,BorderLayout 会解释字符串的缺失 规范与常量 CENTER 相同:

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

    【讨论】:

      【解决方案2】:

      这个

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

      将替换您添加到屏幕上的任何其他内容...

      这会将按钮添加到屏幕底部...

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

      您可以将布局更改为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);
      

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

      仔细阅读

      看看你是否能找到一种或多种符合你要求的布局

      【讨论】:

        【解决方案3】:

        这是快速修复:

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

        但是,您需要进一步了解 LayoutManager。看看这里: http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html

        另请查看 miglayout.net

        【讨论】:

          【解决方案4】:

          您需要将所有项目添加到JPaneltype中,然后将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);


          这应该很简单。

          【讨论】:

          • 您可能想要添加 JPanel 默认使用 FlowLayout。如果 OP 想在 type 面板中使用不同的布局,他们可以简单地调用 type.setLayout() 来更改它。
          猜你喜欢
          • 1970-01-01
          • 2012-04-18
          • 2015-04-24
          • 2013-12-08
          • 2012-10-31
          • 1970-01-01
          • 2022-07-12
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多