尝试在组件上使用FlowLayout.LEFT时,JPanels水平未对齐

时间:2020-07-26 04:42:41

标签: java swing layout-manager flowlayout

因此,在我的GUI中,我在名为Connection的红色框中有1个JPanel,它在南北都有另外两个面板。我正在使用FlowLayout并尝试将“主机”和“端口”标签设置为在左侧对齐,但是它们有点偏离,似乎无法找出原因。

public class ClientView extends JPanel {

private JComboBox<String> comboBox;
private JButton connect;
private JTextArea display;
private JTextArea text;
private JTextField host;
private JTextField text1;
private JButton send;

// Constructor for all the components of the GUI for the Client application
public ClientView() {
    // Sets the layout for the Client application and the border
    this.setLayout(new BorderLayout());
    this.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
    JPanel connectionPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
    JPanel clientRequestPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
    JPanel subDisplayPanel = new JPanel(new BorderLayout());
    JPanel topPanel = new JPanel(new BorderLayout());
    JScrollPane displayPanel;
    JLabel hostLabel = new JLabel("Host: ");
    JLabel portLabel = new JLabel("Port: ");
    String ports[] = new String[]{"", "8088", "65000", "65535"};
    // Makes a ComboBox and gives its requirments. Makes it the color white, editable and size
    comboBox = new JComboBox<String>(ports);
    comboBox.setBackground(Color.WHITE);
    comboBox.setEditable(true);
    
    comboBox.setMaximumRowCount(4);
    comboBox.setPreferredSize(new Dimension(110, 25));
    // Makes the Connect Buttons and gives it the color red.
    connect = new JButton("Connect");
    connect.setBackground(Color.RED);
    connect.setPreferredSize(new Dimension(110, 25));
    // Sets the host labels required dimensions of 40 and 25. Then the JTextField host with its text being localhost
    // Also makes it White, editable, size and its margin
    hostLabel.setPreferredSize(new Dimension(40, 25));
    host = new JTextField("localhost");
    host.setBackground(Color.WHITE);
    host.setEditable(true);
    host.setPreferredSize(new Dimension(490, 25));
    host.setMargin(new Insets(0, 5, 0, 0));
    // Sets the port labels required dimensions of 40 and 25
    portLabel.setPreferredSize(new Dimension(40, 25));
    // Creates the send button makes it uneditable and its dimension
    send = new JButton("Send");
    send.setEnabled(false);
    send.setPreferredSize(new Dimension(80, 25));
    // Sets the mnemonic for the letter H for the host label
    hostLabel.setDisplayedMnemonic('H');
    // Aligns the hostLabel to the left
    hostLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
    // For when ALT-Key is pressed that it focus is transfered
    hostLabel.setLabelFor(host);
    // Sets the mnemonic for the letter P for the port label
    portLabel.setDisplayedMnemonic('P');
    // Aligns the portLabel to the left
    portLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
    // For when ALT-Key is pressed that it focus is transfered
    portLabel.setLabelFor(comboBox);

enter image description here

0 个答案:

没有答案