Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 什么布局在swing中接受百分比而不是值?_Java_Swing_Layout Manager - Fatal编程技术网

Java 什么布局在swing中接受百分比而不是值?

Java 什么布局在swing中接受百分比而不是值?,java,swing,layout-manager,Java,Swing,Layout Manager,我需要根据他们所需的视觉空间百分比创建框架内容。 例如,面板1为20%,面板2为80%。 这种布局管理需要什么布局? 以简化形式GridBagLayout,但成功满足您的要求80%-20% 自定义MigLayout 没有一种JDK布局允许您直接执行此操作。BoxLayout和GridBagLayout允许您这样做 使用GridBagLayout,您可以指定介于0和1之间的权重X/y值,该值告诉布局管理器如何分配额外空间。因此,假设您以80/20的比率创建具有首选大小的组件,它们应该能够以

我需要根据他们所需的视觉空间百分比创建框架内容。 例如,面板1为20%,面板2为80%。 这种布局管理需要什么布局?

  • 以简化形式
    GridBagLayout
    ,但成功满足您的要求80%-20%


  • 自定义
    MigLayout

    • 没有一种JDK布局允许您直接执行此操作。BoxLayout和GridBagLayout允许您这样做

      使用GridBagLayout,您可以指定介于0和1之间的权重X/y值,该值告诉布局管理器如何分配额外空间。因此,假设您以80/20的比率创建具有首选大小的组件,它们应该能够以相同的比率增长

      在这方面,BoxLayout更容易使用,因为您不需要指定特定的约束,它只是按照首选大小的比例调整大小


      对于设计为允许您将相对大小指定为简单约束的简单布局管理器,您可以查看。

      始终认为权重x/weighty是从0到1?将进行向上投票,但随后意识到该示例没有演示如何对相对大小使用GridBagLayout。权重X/y约束仅控制调整帧大小时如何分配额外空间。每个面板的首选尺寸为(10,10),因为它使用FlowLayout。如果将帧宽度增加10像素,则对于(18,10)和(12,10)的新尺寸,8将转到一个面板,2将转到另一个面板,这不是总尺寸的80/20比率。为了使权重X/y正常工作,您需要为每个已经处于所需比例的面板指定一个首选尺寸。
      import java.awt.*;
      import javax.swing.*;
      import javax.swing.border.*;
      
      public class BorderPanels extends JFrame {
      
          private static final long serialVersionUID = 1L;
      
          public BorderPanels() {
              setLayout(new GridBagLayout());// set LayoutManager
              GridBagConstraints gbc = new GridBagConstraints();
              JPanel panel1 = new JPanel();
              Border eBorder = BorderFactory.createEtchedBorder();
      
              panel1.setBorder(BorderFactory.createTitledBorder(eBorder, "80pct"));
              gbc.gridx = gbc.gridy = 0;
              gbc.gridwidth = gbc.gridheight = 1;
              gbc.fill = GridBagConstraints.BOTH;
              gbc.anchor = GridBagConstraints.NORTHWEST;
              gbc.weightx = gbc.weighty = 70;
              add(panel1, gbc); // add component to the ContentPane
      
              JPanel panel2 = new JPanel();
              panel2.setBorder(BorderFactory.createTitledBorder(eBorder, "20pct"));
              gbc.gridy = 1;
              gbc.weightx = gbc.weighty = 20;
              gbc.insets = new Insets(2, 2, 2, 2);
              add(panel2, gbc); // add component to the ContentPane
      
              JPanel panel3 = new JPanel();
              panel3.setBorder(BorderFactory.createTitledBorder(eBorder, "20pct"));
              gbc.gridx = 1;
              gbc.gridy = 0;
              gbc.gridwidth = 1;
              gbc.gridheight = 2;
              gbc.weightx = /*gbc.weighty = */ 20;
              gbc.insets = new Insets(2, 2, 2, 2);
              add(panel3, gbc); // add component to the ContentPane
      
              setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // important
              pack();
              setVisible(true); // important
          }
      
          public static void main(String[] args) {
              javax.swing.SwingUtilities.invokeLater(new Runnable() { // important
      
                  @Override
                  public void run() {
                      BorderPanels borderPanels = new BorderPanels();
                  }
              });
          }
      }