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

huangapple 未分类评论45阅读模式
英文:

JPanels horizontally not lining up when trying to use FlowLayout.LEFT on the components

问题

以下是您提供的代码部分的翻译:

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;

    // Client应用程序GUI的所有组件的构造函数
    public ClientView() {
        // 设置Client应用程序的布局和边框
        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"};
        // 创建一个ComboBox并设置其要求。将其颜色设置为白色,可编辑,并设置大小
        comboBox = new JComboBox<String>(ports);
        comboBox.setBackground(Color.WHITE);
        comboBox.setEditable(true);
        
        comboBox.setMaximumRowCount(4);
        comboBox.setPreferredSize(new Dimension(110, 25));
        // 创建Connect按钮并将其颜色设置为红色。
        connect = new JButton("Connect");
        connect.setBackground(Color.RED);
        connect.setPreferredSize(new Dimension(110, 25));
        // 设置host标签的所需尺寸为40和25。然后创建一个带有默认文本"localhost"的JTextField host
        // 同时将其设置为白色,可编辑,并设置大小和边距
        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));
        // 设置port标签的所需尺寸为40和25
        portLabel.setPreferredSize(new Dimension(40, 25));
        // 创建send按钮并将其设置为不可用,并设置其尺寸
        send = new JButton("Send");
        send.setEnabled(false);
        send.setPreferredSize(new Dimension(80, 25));
        // 设置host标签的助记符为字母H
        hostLabel.setDisplayedMnemonic('H');
        // 将hostLabel对齐到左侧
        hostLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
        // 当按下ALT键时,将焦点传递
        hostLabel.setLabelFor(host);
        // 设置port标签的助记符为字母P
        portLabel.setDisplayedMnemonic('P');
        // 将portLabel对齐到左侧
        portLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
        // 当按下ALT键时,将焦点传递
        portLabel.setLabelFor(comboBox);

...

请注意,由于代码部分较长,我只提供了部分翻译,如果您需要继续翻译后面的内容,请随时告诉我。

英文:

So my GUI, I have 1 JPanel in the red box called Connection that has 2 other panels north and south. Im using FlowLayout and trying to set the Host and Port label to line up on the left side but they are off by a bit and cant seem figure out why.

public class ClientView extends JPanel {

private JComboBox&lt;String&gt; 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(&quot;Host: &quot;);
    JLabel portLabel = new JLabel(&quot;Port: &quot;);
    String ports[] = new String[]{&quot;&quot;, &quot;8088&quot;, &quot;65000&quot;, &quot;65535&quot;};
    // Makes a ComboBox and gives its requirments. Makes it the color white, editable and size
    comboBox = new JComboBox&lt;String&gt;(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(&quot;Connect&quot;);
    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(&quot;localhost&quot;);
    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(&quot;Send&quot;);
    send.setEnabled(false);
    send.setPreferredSize(new Dimension(80, 25));
    // Sets the mnemonic for the letter H for the host label
    hostLabel.setDisplayedMnemonic(&#39;H&#39;);
    // 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(&#39;P&#39;);
    // 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);

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

huangapple
  • 本文由 发表于 2020年7月26日 12:42:41
  • 转载请务必保留本文链接:https://java.coder-hub.com/63096157.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定