setLayout(null)时,JPanel上的组件未显示 [英] Component on JPanel not showing when setLayout(null)

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

问题描述

有人可以说出组合框为什么不显示?我有一个控制器:

Someone can tell why the combobox is not showing ? I have a Controller:

public class TestController extends JPanel {

TestView cgView;

public TestController() 
{

    setLayout(null);

    cgView=new TestView();

    add(cgView);

}
public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
             JFrame fr = new JFrame("testt");
                fr.setSize(1200,1000);
                fr.setResizable(false);

                TestController cgc=new TestController();
                fr.setBackground(Color.white);
                fr.setVisible(true);

                fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                fr.add(cgc);

         }
        });
    }


}

还有一个视图

public class TestView extends JPanel{
    private static final long serialVersionUID = 1L;

    public JComboBox<String> comboBox; 

    public TestView() {

          comboBox= new JComboBox<>(new String[] {"option1", "option2" });
          comboBox.setBounds(100,500, 100, 20);
          add(comboBox);

    }
}

由于TestController中的 setLayout(null),我看不到comboBox.如果我在我的TestContoller()中添加 add(cgView.comboBox),那么它看起来像这样:

Because of setLayout(null) in TestController, I can't see the comboBox. If I add add(cgView.comboBox) to my TestContoller(), so that it looks like this:

public TestController() 
    {

        setLayout(null);

        cgView=new TestView();

        add(cgView);
        add(cgView.comboBox);

    }

比我看得到的多.有人可以告诉为什么吗?

Than I can see it. Can someone tell why?

所以我的解决方案是总是将组件添加到TestController中,或者将TestController作为TestView的一个属性来传递(所以在TestView()中,我会这样添加它们:this.parentPanel.add(comboBox).是否还有其他解决方案?

So my solution is to always add the components in TestController, or to pass TestController as an atribute to TestView (so in TestView() I would add them like this this.parentPanel.add(comboBox). Is there any other solution?

推荐答案

  • 几乎不使用空布局
  • 相反,请使用嵌套在JPanels中的布局的最佳组合来为您的GUI实现令人愉悦的布局.
  • 如果您确实使用空布局,那么您将完全负责设置添加到该容器中的所有组件的大小和位置.
  • 您当前的问题是,您永远不会给TestView一个尺寸或位置,而是将其添加到使用布局的空容器中.
  • 您不应将一个组件(以上是JComboBox)添加到多个容器中.
  • 之后,您要在JFrame上调用setVisible(true).在您添加所有组件并在其上调用pack()之前.
    • Don't use null layout, almost ever
    • Instead use the best combination of layouts nested in JPanels to achieve a pleasing layout for your GUI.
    • If you do use null layout then you are fully responsible for setting the size and location of all components added to that container.
    • Your current problem is that you never give TestView a size or location and have then added it to a null layout-using container.
    • You shouldn't add a component (above, your JComboBox) to more than one container.
    • Don't call setVisible(true) on the JFrame until after you've added all components and called pack() on it.
    • 例如

      import java.awt.*;
      import javax.swing.*;
      
      public class TestController extends JPanel {
         private static final int PREF_W = 1000;
         private static final int PREF_H = 800;
         TestView cgView;
      
         public TestController() {
            setLayout(null);
            cgView = new TestView();
            cgView.setSize(getPreferredSize());
            add(cgView);
         }
      
         @Override
         public Dimension getPreferredSize() {
            return new Dimension(PREF_W, PREF_H);
         }
      
         public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
               public void run() {
                  JFrame fr = new JFrame("testt");
                  // fr.setSize(1200, 1000);
                  fr.setResizable(false);
                  TestController cgc = new TestController();
                  fr.setBackground(Color.white);
                  // fr.setVisible(true);
                  fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                  fr.add(cgc);
                  fr.pack(); //!! added 
                  fr.setVisible(true); // !! moved
               }
            });
         }
      }
      

      但是最好使用布局:

      import java.awt.*;
      import javax.swing.*;
      
      public class TestController extends JPanel {
         private static final int PREF_W = 1000;
         private static final int PREF_H = 800;
         TestView cgView;
      
         public TestController() {
            //!!  setLayout(null);
            cgView = new TestView();
            //!! cgView.setSize(getPreferredSize());
            add(cgView);
         }
      
         @Override
         public Dimension getPreferredSize() {
            return new Dimension(PREF_W, PREF_H);
         }
      
         public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
               public void run() {
                  JFrame fr = new JFrame("testt");
                  // fr.setSize(1200, 1000);
                  fr.setResizable(false);
                  TestController cgc = new TestController();
                  fr.setBackground(Color.white);
                  // fr.setVisible(true);
                  fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                  fr.add(cgc);
                  fr.pack(); //!! added 
                  fr.setVisible(true); // !! moved
               }
            });
         }
      }
      
      class TestView extends JPanel {
         private static final long serialVersionUID = 1L;
         public JComboBox<String> comboBox;
      
         public TestView() {
            comboBox = new JComboBox<String>(new String[] { "option1", "option2" });
            // comboBox.setBounds(100, 500, 100, 20);
            add(comboBox);
         }
      }
      

      修改
      OP在评论中问:

      Edit
      The OP asked in a comment:

      几乎不会"?在哪种情况下,您会使用[null布局]?

      'Almost never'? In which cases you would use it [the null layout]?

      我很少使用它,例如当我想通过动画或使用MouseListener移动组件时,但是即使如此,许多人还是建议您创建自己的布局来处理该布局,例如Rob Camick的

      I use it rarely, such as when I want to move components around via animation or with a MouseListener, but even then, many suggest that you create your own layout to handle that such as Rob Camick's Drag Layout

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

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