如何确保仅使用网格、边框和流布局管理器对齐这些字段? JAVA SWING GUI开发

问题描述 投票:0回答:1

我想对齐这些字段,而不必求助于 setPreferredSize。我是一名学生。这是我复制它的目标。这是一份家庭作业,几天来我一直在努力让它保持一致,但我就是做不好。我们只允许使用网格、流或边框布局管理器。

package Main;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;

public class SatelliteManagementSystem {
    public static void main(String[] args) {
        
        JFrame frame = new JFrame("SpaceX Starlink Project");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(550, 400);
        frame.setLayout(new BorderLayout());

        
        ImageIcon imgIcon = new ImageIcon("icon.png");  // Replace with the path to your icon image
        Image img = imgIcon.getImage();
        frame.setIconImage(img);  // Set the icon for the frame

        
        JPanel formPanel = new JPanel(new GridLayout(4, 1, 10, 10)); // 4 rows, 1 column, gaps of 10px

        // Ensure labels have consistent width using EmptyBorder padding to align text fields properly

        
        JPanel satelliteIDPanel = new JPanel(new BorderLayout(10, 10));
        JLabel satelliteIDLabel = new JLabel("Satellite ID:");
        satelliteIDLabel.setPreferredSize(new Dimension(111, 25));  // Set a fixed width for label
        satelliteIDPanel.add(satelliteIDLabel, BorderLayout.WEST);

        
        JPanel satelliteIDFieldPanel = new JPanel(new GridLayout(1, 2, 10, 0));
        JTextField satelliteIDField = new JTextField();  // Text field resizes
        satelliteIDFieldPanel.add(satelliteIDField);

        JButton findSatelliteButton = new JButton("Find Satellite");
        satelliteIDFieldPanel.add(findSatelliteButton);

        satelliteIDPanel.add(satelliteIDFieldPanel, BorderLayout.CENTER);
        formPanel.add(satelliteIDPanel);

        // Row for Satellite Name
        JPanel satelliteNamePanel = new JPanel(new BorderLayout(10, 10));
        JLabel satelliteNameLabel = new JLabel("Satellite Name:");
        satelliteNameLabel.setPreferredSize(new Dimension(111, 25));  // Consistent width for label
        satelliteNamePanel.add(satelliteNameLabel, BorderLayout.WEST);

        JTextField satelliteNameField = new JTextField();
        satelliteNamePanel.add(satelliteNameField, BorderLayout.CENTER);

        formPanel.add(satelliteNamePanel);

        
        JPanel latLongPanel = new JPanel(new GridLayout(1, 2, 10, 0));  // 1 row, 2 columns

        // Longitude
        JPanel longitudePanel = new JPanel(new BorderLayout(10, 0));
        JLabel longitudeLabel = new JLabel("Longitude:");
        longitudeLabel.setPreferredSize(new Dimension(111, 25));  // Consistent width for label
        longitudePanel.add(longitudeLabel, BorderLayout.WEST);
        JTextField longitudeField = new JTextField();
        longitudePanel.add(longitudeField, BorderLayout.CENTER);
        latLongPanel.add(longitudePanel);

        // Latitude
        JPanel latitudePanel = new JPanel(new BorderLayout(10, 0));
        JLabel latitudeLabel = new JLabel("Latitude:");
        latitudeLabel.setPreferredSize(new Dimension(111, 25));  // Consistent width for label
        latitudePanel.add(latitudeLabel, BorderLayout.WEST);
        JTextField latitudeField = new JTextField();
        latitudePanel.add(latitudeField, BorderLayout.CENTER);
        latLongPanel.add(latitudePanel);

        formPanel.add(latLongPanel);

        
        JPanel elevHealthPanel = new JPanel(new GridLayout(1, 2, 10, 0));  // 1 row, 2 columns

        // Elevation
        JPanel elevationPanel = new JPanel(new BorderLayout(10, 0));
        JLabel elevationLabel = new JLabel("Elevation:");
        elevationLabel.setPreferredSize(new Dimension(111, 25));  // Consistent width for label
        elevationPanel.add(elevationLabel, BorderLayout.WEST);
        JTextField elevationField = new JTextField();
        elevationPanel.add(elevationField, BorderLayout.CENTER);
        elevHealthPanel.add(elevationPanel);

        // Health Status
        JPanel healthStatusPanel = new JPanel(new BorderLayout(10, 0));
        JLabel healthStatusLabel = new JLabel("Health Status:");
        healthStatusLabel.setPreferredSize(new Dimension(111, 25));  // Consistent width for label
        healthStatusPanel.add(healthStatusLabel, BorderLayout.WEST);
        JComboBox<String> healthStatusCombo = new JComboBox<>(new String[] { "Healthy", "Unhealthy" });
        healthStatusPanel.add(healthStatusCombo, BorderLayout.CENTER);
        elevHealthPanel.add(healthStatusPanel);

        formPanel.add(elevHealthPanel);

        // Panel for Save, Update, Delete buttons (above the text area)
        JPanel buttonPanel = new JPanel(new GridLayout(1, 3, 15, 0));  // 1 row, 3 columns with 15px gap
        JButton saveButton = new JButton("Save");
        JButton updateButton = new JButton("Update");
        JButton deleteButton = new JButton("Delete");
        buttonPanel.add(saveButton);
        buttonPanel.add(updateButton);
        buttonPanel.add(deleteButton);

        
        JPanel buttonWrapperPanel = new JPanel(new BorderLayout());
        buttonWrapperPanel.setBorder(new EmptyBorder(10, 0, 10, 0));  // Adds 10px margin at the top and bottom
        buttonWrapperPanel.add(buttonPanel, BorderLayout.CENTER);

        
        JPanel textAreaPanel = new JPanel(new BorderLayout());
        JTextArea textArea = new JTextArea(20, 40);
        textAreaPanel.add(textArea, BorderLayout.CENTER);
        textArea.setEditable(false);

        
        JPanel clearPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 15, 10));
        JButton clearAllButton = new JButton("Clear All");
        clearPanel.add(clearAllButton);

        
        JPanel centerPanel = new JPanel(new BorderLayout());
        centerPanel.add(buttonWrapperPanel, BorderLayout.NORTH);  // Buttons on top of the text area
        centerPanel.add(textAreaPanel, BorderLayout.CENTER);  // Text area in the center
        centerPanel.add(clearPanel, BorderLayout.SOUTH);  // Clear All button below the text area

        
        JPanel contentPanel = new JPanel(new BorderLayout());
        contentPanel.setBorder(new EmptyBorder(10, 10, 10, 10)); // Adds a margin of 20px on all sides
        contentPanel.add(formPanel, BorderLayout.NORTH);
        contentPanel.add(centerPanel, BorderLayout.CENTER);

        
        frame.add(contentPanel, BorderLayout.CENTER);  // Add the content panel to the frame

        
        frame.setVisible(true);
    }
}

预期输出: 预期输出 我的输出:我的输出

java user-interface grid alignment border
1个回答
0
投票

要在遵守布局约束的同时对齐表单组件:我建议您使用嵌套布局: 使用 GridLayout 实现结构化、一致的布局,使用 BorderLayout 管理组件的对齐方式。

然后添加一致的填充:EmptyBorder 用于保持组件之间均匀的填充和间距。

并避免使用 setPreferredSize:组件自然对齐,无需设置固定尺寸。

我用更新的代码创建了这个片段:

package Main;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;

public class SatelliteManagementSystem {
    public static void main(String[] args) {
        
        JFrame frame = new JFrame("SpaceX Starlink Project");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(550, 400);
        frame.setLayout(new BorderLayout());

        ImageIcon imgIcon = new ImageIcon("icon.png");  // Replace with the path to your icon image
        Image img = imgIcon.getImage();
        frame.setIconImage(img);  // Set the icon for the frame

        JPanel formPanel = new JPanel(new GridLayout(4, 1, 10, 10)); // 4 rows, 1 column, gaps of 10px

        // Satellite ID Panel
        JPanel satelliteIDPanel = new JPanel(new BorderLayout(10, 10));
        JLabel satelliteIDLabel = new JLabel("Satellite ID:");
        satelliteIDPanel.add(satelliteIDLabel, BorderLayout.WEST);

        JPanel satelliteIDFieldPanel = new JPanel(new GridLayout(1, 2, 10, 0));
        JTextField satelliteIDField = new JTextField();
        satelliteIDFieldPanel.add(satelliteIDField);

        JButton findSatelliteButton = new JButton("Find Satellite");
        satelliteIDFieldPanel.add(findSatelliteButton);

        satelliteIDPanel.add(satelliteIDFieldPanel, BorderLayout.CENTER);
        formPanel.add(satelliteIDPanel);

        // Satellite Name Panel
        JPanel satelliteNamePanel = new JPanel(new BorderLayout(10, 10));
        JLabel satelliteNameLabel = new JLabel("Satellite Name:");
        satelliteNamePanel.add(satelliteNameLabel, BorderLayout.WEST);

        JTextField satelliteNameField = new JTextField();
        satelliteNamePanel.add(satelliteNameField, BorderLayout.CENTER);

        formPanel.add(satelliteNamePanel);

        // Latitude and Longitude Panel
        JPanel latLongPanel = new JPanel(new GridLayout(1, 2, 10, 0));

        JPanel longitudePanel = new JPanel(new BorderLayout(10, 0));
        JLabel longitudeLabel = new JLabel("Longitude:");
        longitudePanel.add(longitudeLabel, BorderLayout.WEST);
        JTextField longitudeField = new JTextField();
        longitudePanel.add(longitudeField, BorderLayout.CENTER);
        latLongPanel.add(longitudePanel);

        JPanel latitudePanel = new JPanel(new BorderLayout(10, 0));
        JLabel latitudeLabel = new JLabel("Latitude:");
        latitudePanel.add(latitudeLabel, BorderLayout.WEST);
        JTextField latitudeField = new JTextField();
        latitudePanel.add(latitudeField, BorderLayout.CENTER);
        latLongPanel.add(latitudePanel);

        formPanel.add(latLongPanel);

        // Elevation and Health Status Panel
        JPanel elevHealthPanel = new JPanel(new GridLayout(1, 2, 10, 0));

        JPanel elevationPanel = new JPanel(new BorderLayout(10, 0));
        JLabel elevationLabel = new JLabel("Elevation:");
        elevationPanel.add(elevationLabel, BorderLayout.WEST);
        JTextField elevationField = new JTextField();
        elevationPanel.add(elevationField, BorderLayout.CENTER);
        elevHealthPanel.add(elevationPanel);

        JPanel healthStatusPanel = new JPanel(new BorderLayout(10, 0));
        JLabel healthStatusLabel = new JLabel("Health Status:");
        healthStatusPanel.add(healthStatusLabel, BorderLayout.WEST);
        JComboBox<String> healthStatusCombo = new JComboBox<>(new String[] { "Healthy", "Unhealthy" });
        healthStatusPanel.add(healthStatusCombo, BorderLayout.CENTER);
        elevHealthPanel.add(healthStatusPanel);

        formPanel.add(elevHealthPanel);

        // Button Panel
        JPanel buttonPanel = new JPanel(new GridLayout(1, 3, 15, 0));
        JButton saveButton = new JButton("Save");
        JButton updateButton = new JButton("Update");
        JButton deleteButton = new JButton("Delete");
        buttonPanel.add(saveButton);
        buttonPanel.add(updateButton);
        buttonPanel.add(deleteButton);

        JPanel buttonWrapperPanel = new JPanel(new BorderLayout());
        buttonWrapperPanel.setBorder(new EmptyBorder(10, 0, 10, 0));
        buttonWrapperPanel.add(buttonPanel, BorderLayout.CENTER);

        // Text Area Panel
        JPanel textAreaPanel = new JPanel(new BorderLayout());
        JTextArea textArea = new JTextArea(20, 40);
        textAreaPanel.add(new JScrollPane(textArea), BorderLayout.CENTER);
        textArea.setEditable(false);

        JPanel clearPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 15, 10));
        JButton clearAllButton = new JButton("Clear All");
        clearPanel.add(clearAllButton);

        JPanel centerPanel = new JPanel(new BorderLayout());
        centerPanel.add(buttonWrapperPanel, BorderLayout.NORTH);
        centerPanel.add(textAreaPanel, BorderLayout.CENTER);
        centerPanel.add(clearPanel, BorderLayout.SOUTH);

        JPanel contentPanel = new JPanel(new BorderLayout());
        contentPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
        contentPanel.add(formPanel, BorderLayout.NORTH);
        contentPanel.add(centerPanel, BorderLayout.CENTER);

        frame.add(contentPanel, BorderLayout.CENTER);
        frame.setVisible(true);
    }
}

© www.soinside.com 2019 - 2024. All rights reserved.