简体   繁体   English

如何提高Java Swing中JPanel的边距和填充?

[英]How to improve the margin and padding of JPanel in Java Swing?

When I initially run my java swing application, the padding and margining of the JLabels and JTextFields in JPanel look good, but whenever, I resize the application, the padding and margining for the JLabels and JTextFields increases so much that it does not look as good.当我最初运行我的 java swing 应用程序时,JPanel 中 JLabels 和 JTextFields 的填充和边距看起来不错,但是每当我调整应用程序的大小时,JLabels 和 JTextFields 的填充和边距增加太多以至于看起来不太好. I understand a GridLayout has a set amount of vertical and horizontal spacing, but what can I do to make the spacing look better as the size of the application increases?我知道 GridLayout 有一定数量的垂直和水平间距,但是随着应用程序大小的增加,我该怎么做才能使间距看起来更好? I would also like the text in the text fields to resize as the textfield increases but have been unable to figure out.我还希望文本字段中的文本随着文本字段的增加而调整大小,但一直无法弄清楚。 I have been thinking to use a GridBadLayout but have not had much luck with that either.我一直在考虑使用 GridBadLayout 但也没有太多运气。 There is just way too much spacing as the size increases, which does not look good, and I have been unable to find or learn a good solution.随着尺寸的增加,间距太大,看起来不太好,我一直无法找到或学习一个好的解决方案。 I would appreciate any advice.我将不胜感激任何建议。

Here is the code:这是代码:

package TestPackage;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.GridLayout;

import javax.swing.JTextField;

public class TestGridLayout  {


     
      public static void main(String[] args) {
             JPanel testPanel = new JPanel(); 
             
             GridLayout layoutManager = new GridLayout(5,2); 
              testPanel.setLayout(layoutManager); 
             
             JLabel platformEnumerationNameLabel = new JLabel(); 
            JLabel logicalEnumerationNameFieldLabel = new JLabel(); 
            JLabel valueTypeUnitNameFieldLabel = new JLabel(); 
            JLabel measurementNameFieldLabel = new JLabel(); 
             JLabel measurementAxisNameFieldLabel = new JLabel(); 
            
             JTextField platformEnumerationNameField = new JTextField(); 
             JTextField logicalEnumerationNameField = new JTextField(); 
             JTextField valueTypeUnitNameField = new JTextField(); 
            JTextField measurementNameField = new JTextField(); 
            JTextField measurementAxisNameField = new JTextField(); 
            
            platformEnumerationNameLabel.setText("Label 1");   
            testPanel.add(platformEnumerationNameLabel); 
            
            platformEnumerationNameField.setText("input some data"); 
            testPanel.add(platformEnumerationNameField); 
            
             
            logicalEnumerationNameFieldLabel.setText("Label 2");
            testPanel.add(logicalEnumerationNameFieldLabel); 
            
            logicalEnumerationNameField.setText("input some data"); 
            testPanel.add(logicalEnumerationNameField); 
            
            valueTypeUnitNameFieldLabel.setText("Label 3");
            testPanel.add(valueTypeUnitNameFieldLabel);
            
            valueTypeUnitNameField.setText("input some data");
            testPanel.add(valueTypeUnitNameField); 
            
            measurementNameFieldLabel.setText("Label 4");
            testPanel.add(measurementNameFieldLabel);
            
            measurementNameField.setText("input some data");
            testPanel.add(measurementNameField); 
            
            measurementAxisNameFieldLabel.setText("Label 5");
            testPanel.add(measurementAxisNameFieldLabel);
            
            measurementAxisNameField.setText("input some data");
            testPanel.add(measurementAxisNameField);  
            
            JDialog dialog = new JDialog();
            
            dialog.add(testPanel);
            
            dialog.setSize(700,200); 
            dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            dialog.setVisible(true);
            
           
      }
    }

Use a different layout manager that doesn't resize all the components (unless you want them to)使用不同的布局管理器,它不会调整所有组件的大小(除非您希望它们这样做)

打包 调整大小

See How to Use GridBagLayout for more details有关更多详细信息,请参阅如何使用 GridBagLayout

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.insets = new Insets(8, 8, 8, 8);
            gbc.gridx = 0;
            gbc.gridy = 0;

            JLabel platformEnumerationNameLabel = new JLabel();
            JLabel logicalEnumerationNameFieldLabel = new JLabel();
            JLabel valueTypeUnitNameFieldLabel = new JLabel();
            JLabel measurementNameFieldLabel = new JLabel();
            JLabel measurementAxisNameFieldLabel = new JLabel();

            JTextField platformEnumerationNameField = new JTextField(20);
            JTextField logicalEnumerationNameField = new JTextField(20);
            JTextField valueTypeUnitNameField = new JTextField(20);
            JTextField measurementNameField = new JTextField(20);
            JTextField measurementAxisNameField = new JTextField(20);


            platformEnumerationNameLabel.setText("Label 1");
            add(platformEnumerationNameLabel, gbc);

            gbc.gridx++;
            platformEnumerationNameField.setText("input some data");
            add(platformEnumerationNameField, gbc);

            gbc.gridy++;
            gbc.gridx = 0;
            logicalEnumerationNameFieldLabel.setText("Label 2");
            add(logicalEnumerationNameFieldLabel, gbc);

            gbc.gridx++;
            logicalEnumerationNameField.setText("input some data");
            add(logicalEnumerationNameField, gbc);

            gbc.gridy++;
            gbc.gridx = 0;
            valueTypeUnitNameFieldLabel.setText("Label 3");
            add(valueTypeUnitNameFieldLabel, gbc);

            gbc.gridx++;
            valueTypeUnitNameField.setText("input some data");
            add(valueTypeUnitNameField, gbc);

            gbc.gridy++;
            gbc.gridx = 0;
            measurementNameFieldLabel.setText("Label 4");
            add(measurementNameFieldLabel, gbc);

            gbc.gridx++;
            measurementNameField.setText("input some data");
            add(measurementNameField, gbc);

            gbc.gridy++;
            gbc.gridx = 0;
            measurementAxisNameFieldLabel.setText("Label 5");
            add(measurementAxisNameFieldLabel, gbc);

            gbc.gridx++;
            measurementAxisNameField.setText("input some data");
            add(measurementAxisNameField, gbc);
        }

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM