英文:
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<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);
专注分享java语言的经验与见解,让所有开发者获益!
评论