什么布局接受百分比而不是摆动中的值? [英] What layout accepts percentages instead of values in swing?

查看:126
本文介绍了什么布局接受百分比而不是摆动中的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to create frame content based on their percentage of visual space they need. For example 20% for panel1,80% for panel2. what layout does this kind of layout management?

推荐答案


  • 缩小形式 GridBagLayout ,但成功满足您的要求80% - 20%

    • in reduced form GridBagLayout, but with success for your requirement 80% - 20%
    • 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();
                  }
              });
          }
      }
      







      • 自定义 MigLayout


        • custom MigLayout
        • 这篇关于什么布局接受百分比而不是摆动中的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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