编程语言
首页 > 编程语言> > java-setPanel(null)时,JPanel上的组件未显示

java-setPanel(null)时,JPanel上的组件未显示

作者:互联网

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

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.如果我将add(cgView.comboBox)添加到TestContoller()中,则它看起来像这样:

public TestController() 
    {

        setLayout(null);

        cgView=new TestView();

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

    }

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

因此,我的解决方案是始终将组件添加到TestController中,或者将TestController作为TestView的一个属性进行传递(因此在TestView()中,我将这样添加它们:this.parentPanel.add(comboBox).还有其他解决方案吗?

解决方法:

>几乎不要使用空布局
>而是使用嵌套在JPanels中的布局的最佳组合来为您的GUI实现令人满意的布局.
>如果您使用空布局,那么您将完全负责设置添加到该容器的所有组件的大小和位置.
>当前的问题是,您永远不会给TestView一个尺寸或位置,而是将其添加到使用空布局的容器中.
>不应将一个组件(以上是JComboBox)添加到多个容器中.
>在添加所有组件并在其上调用pack()之后,才对JFrame调用setVisible(true).

例如.,

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在评论中要求:

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

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

标签:java,swing,jpanel,layout-manager,absolutelayout
来源: https://codeday.me/bug/20191009/1882502.html