Java 当试图使用组件上的FlowLayout.LEFT时,JPanels水平不对齐

Java 当试图使用组件上的FlowLayout.LEFT时,JPanels水平不对齐,java,swing,layout-manager,flowlayout,Java,Swing,Layout Manager,Flowlayout,因此,我的GUI在红色框中有一个JPanel,名为Connection,它有两个其他面板(南北)。我正在使用FlowLayout并试图将主机和端口标签设置为在左侧对齐,但它们有点偏离,似乎无法找出原因 public class ClientView extends JPanel { private JComboBox<String> comboBox; private JButton connect; private JTextArea display; private JText

因此,我的GUI在红色框中有一个JPanel,名为Connection,它有两个其他面板(南北)。我正在使用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);
public类ClientView扩展了JPanel{
专用JComboBox组合框;
专用按钮连接;
私人区域显示器;
私有区域文本;
专用JTextField主机;
私有JTextField text1;
私人按钮发送;
//客户端应用程序GUI所有组件的构造函数
公共客户端视图(){
//设置客户端应用程序和边框的布局
此.setLayout(新的BorderLayout());
this.setboorder(BorderFactory.createEmptyByOrder(5,5,5,5));
JPanel面板=新的JPanel(新的FlowLayout(FlowLayout.LEFT));
JPanel connectionPanel=newjpanel(newflowlayout(FlowLayout.LEFT));
JPanel clientRequestPanel=newjpanel(newflowlayout(FlowLayout.LEFT));
JPanel subDisplayPanel=newjpanel(newborderlayout());
JPanel-topPanel=newjpanel(newborderlayout());
JScrollPane显示面板;
JLabel hostLabel=新的JLabel(“主机:”);
JLabel-portLabel=新的JLabel(“端口:”);
字符串端口[]=新字符串[]{”、“8088”、“65000”、“65535”};
//制作一个组合框,并给出它的要求。使其颜色为白色,可编辑和大小
comboBox=新的JComboBox(端口);
组合框。背景(颜色。白色);
comboBox.setEditable(真);
comboBox.setMaximumRowCount(4);
comboBox.setPreferredSize(新维度(110,25));
//创建连接按钮并将其颜色设置为红色。
connect=新的JButton(“connect”);
连接.立根接地(颜色.红色);
connect.setPreferredSize(新尺寸(110,25));
//将主机标签所需的维度设置为40和25。然后选择文本为localhost的JTextField主机
//还使其为白色、可编辑、大小和边距
hostLabel.setPreferredSize(新维度(40,25));
主机=新的JTextField(“localhost”);
寄主.背景(颜色.白色);
host.setEditable(true);
setPreferredSize(新维度(490,25));
setMargin(新的插入(0,5,0,0));
//将端口标签所需的尺寸设置为40和25
portLabel.setPreferredSize(新尺寸(40,25));
//创建“发送”按钮,使其不可编辑并且其维度
发送=新的JButton(“发送”);
send.setEnabled(false);
send.setPreferredSize(新维度(80,25));
//为主机标签的字母H设置助记符
hostLabel.setDisplayedMnemonic('H');
//将主机标签向左对齐
hostLabel.setAlignmentX(组件左对齐);
//当按下ALT键时,焦点会转移
hostLabel.setLabelFor(主机);
//为端口标签的字母P设置助记符
portLabel.setDisplayedMnemonic('P');
//将portLabel与左侧对齐
portLabel.setAlignmentX(组件左对齐);
//当按下ALT键时,焦点会转移
portLabel.setLabelFor(组合框);

1)为了更快地获得更好的帮助,请添加or。2)以最小尺寸提供ASCII艺术或GUI预期布局的简单绘图,如果可以调整大小,则具有更大的宽度和高度-以显示应如何使用额外空间。3)请参阅(是)4)我倾向于仅当我有一行组件时才使用
FlowLayout
。简单、容易、可预测。我可能会为此使用
GridBagLayout
。顺便说一句——“红色框称为连接,它有两个其他面板,分别位于北部和南部。”我们看到的是同一个GUI吗?屏幕截图中的红色面板在北方,中间是黑色。因此默认情况下,BoxLayout使中间的JPanel填充框架的其余部分,我需要显示面板是最大的,所以我所做的是将connectionPanel和requestPanel添加到一个在connectionPanel havi时位于北方的面板中ng一个南北面板。因此,北面板中有4个面板,而框架的南部只是一个显示面板。这很难解释,但我应该让我的变量名称更清楚地识别。我仍然看不到MRE/SSCCE,因此我已投票关闭并正在关闭浏览器中的此选项卡。如果要修复此问题,请通知我。1)For最好尽快添加or。2)以最小尺寸提供ASCII艺术或GUI预期布局的简单绘图,如果可以调整大小,则具有更大的宽度和高度,以显示应如何使用额外空间。3)请参阅(是)4)我倾向于仅当我有一行组件时才使用
FlowLayout
。简单、容易、可预测。我可能会为此使用
GridBagLayout
。顺便说一句——“红色框称为连接,它有两个其他面板,分别位于北部和南部。”我们看到的是同一个GUI吗?屏幕截图中的红色面板在北方,中间是黑色。因此默认情况下,BoxLayout使中间的JPanel填充框架的其余部分,我需要显示面板是最大的,所以我所做的是将connectionPanel和requestPanel添加到一个在connectionPanel havi时位于北方的面板中ng一个南北面板。因此,北面板中有4个面板,而框架的南部只是一个显示面板。这很难解释,但我应该让我的变量名称更清楚地识别。我仍然看不到MRE/SSCCE,因此我已投票关闭并正在关闭浏览器中的此选项卡。如果要修复此问题,请通知我。