元素与 GridBagConstraints 不正确对齐

Elements incorrectly aligned with GridBagConstraints

我目前使用以下代码创建了一个 JFrame。我希望三个 JLabels 一个在另一个上面,它们相应的 JTextFields 在右边,四个 JButtons 在它们下面(我最终将使用插图将它们 space 分开)。但是,单元格在一行的框架顶部对齐。谁能发现我犯的错误。谢谢

public class UploadFrame extends JFrame {
private GridBagConstraints gbc = new GridBagConstraints();

/**
 * Creates a JFrame for the file uploading panel to sit in
 * @throws HeadlessException
 */
public UploadFrame(){
    this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    setTitle("Download file");
    setSize(700, 300);
    setLocationRelativeTo(null);
    this.setContentPane(new UploadPanel());
    setVisible(true);
}
}

public class UploadPanel extends JPanel implements ActionListener {
private static final Logger LOGGER = Logger.getLogger(UploadPanel.class.getName());

private JButton sendButton;
private JButton menuButton;
private JButton filePathButton;
private JButton youTubeButton;
private JTextField filePathField;
private JTextField jobNameField;
private JTextField youTubeField;
private JLabel jobNameLabel;
private JLabel filePathLabel;
private JLabel youTubeLabel;
private GridBagConstraints gbc = new GridBagConstraints();
private ServiceUpload serviceUpload = new ServiceUpload();
private String filePath = "No file path selected";
private File mp4 = null;

public UploadPanel() {

    gbc.weightx = 1;
    gbc.weighty = 1;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.insets.bottom = 1;
    gbc.insets.top = 1;
    gbc.insets.right = 1;
    gbc.insets.left = 1;
    setJLabels();
    setJTextField();
    setButtons();
    setAction();

}

/**
 * Method sets and add the JLabels to the panel
 */

void setJLabels() {
    jobNameLabel = new JLabel("Job name:");
    gbc.gridx = 0;
    gbc.gridy = 2;
    gbc.gridwidth = 2;
    gbc.gridheight = 1;
    add(jobNameLabel, gbc);

    filePathLabel = new JLabel("File path:");
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.gridwidth = 2;
    gbc.gridheight = 1;
    add(filePathLabel, gbc);

    youTubeLabel = new JLabel("YouTube http:");
    gbc.gridx = 0;
    gbc.gridy = 1;
    gbc.gridwidth = 2;
    gbc.gridheight = 1;
    add(youTubeLabel, gbc);
}

/**
 * Method sets and add the JTextFields to the panel
 */
void setJTextField() {

    filePathField = new JTextField();
    filePathField.setText(filePath);
    gbc.gridx = 2;
    gbc.gridy = 0;
    gbc.gridwidth = 2;
    gbc.gridheight = 1;
    add(filePathField, gbc);

    jobNameField = new JTextField();
    jobNameField.setText("Default");
    gbc.gridx = 2;
    gbc.gridy = 2;
    gbc.gridwidth = 2;
    gbc.gridheight = 1;
    add(jobNameField, gbc);

    youTubeField = new JTextField();
    youTubeField.setText("Default");
    gbc.gridx = 2;
    gbc.gridy = 1;
    gbc.gridwidth = 2;
    gbc.gridheight = 1;
    add(youTubeField, gbc);
}

/**
 * Method sets and add the JButtons to the panel
 */
void setButtons() {
    sendButton = new JButton("Send");
    gbc.gridx = 0;
    gbc.gridy = 3;
    gbc.gridwidth = 1;
    gbc.gridheight = 1;
    add(sendButton, gbc);

    menuButton = new JButton("Menu");
    gbc.gridx = 1;
    gbc.gridy = 3;
    gbc.gridwidth = 1;
    gbc.gridheight = 1;
    add(menuButton, gbc);

    filePathButton = new JButton("Select file");
    gbc.gridx = 2;
    gbc.gridy = 3;
    gbc.gridwidth = 1;
    gbc.gridheight = 1;
    add(filePathButton, gbc);

    youTubeButton = new JButton("YouTube Download");
    gbc.gridx = 3;
    gbc.gridy = 3;
    gbc.gridwidth = 1;
    gbc.gridheight = 1;
    add(youTubeButton, gbc);
}

/**
 * Method creates the actionListeners
 */
void setAction() {
    sendButton.addActionListener(this);
    menuButton.addActionListener(this);
    filePathButton.addActionListener(this);
}

/**
 * Method sets the actions that will take place when buttons are pressed
 *
 * @param actionEvent
 */
@Override
public void actionPerformed(ActionEvent actionEvent) {
    try {
        if (actionEvent.getSource() == sendButton) {

            String jobName = jobNameField.getText();
            serviceUpload.convertToBase64AndSend(jobName, mp4);
        }

        if (actionEvent.getSource() == menuButton) {
            new MenuFrame();
        }

        if (actionEvent.getSource() == filePathButton) {
            filePathField.setText(chooseFile().getAbsolutePath());
        }

        if (actionEvent.getSource() == youTubeButton) {
            filePathField.setText(serviceUpload.httpPath());
        }
    } catch (Exception e) {
        LOGGER.info(e.toString());
    }
}

/**
 * This method creates the JFileChooser that allows the selection of the file being sent to 
AWS
 * @return File
 */
private File chooseFile() {
    JFileChooser chooser = new JFileChooser();
    FileFilter filter = new FileNameExtensionFilter(null,
            "MP4");

    chooser.setFileFilter(filter);
    chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    chooser.showOpenDialog(null);

    return chooser.getSelectedFile();
}
}

它当前生成的JFrame是:

**我意识到我在面板中设置了 GridBagLayout,所以添加了以下行:

uploadPanel.setLayout(new GridBagLayout());

这已经产生了 window:

I've realised I left setting the GridBagLayout in the panel so added the line:

uploadPanel.setLayout(new GridBagLayout());

你在哪里做的?

您发布的代码中没有变量 "uploadPanel"。

实际上不需要定义变量,因为在将组件添加到面板之前需要设置面板​​的布局管理器。

所以布局管理器应该在你的 "UploadPanel" class:

的构造函数中设置
public UploadPanel() 
{
    setLayout( new GridBagLayout() );
    gbc.weightx = 1;

此外,您的代码结构令人困惑。

相关组件要同时添加到面板中。那就是应该将标签和文本字段对一起添加到面板中。

在单独的方法中添加所有标签然后添加所有文本字段没有意义。

此外,您的代码将一个组件添加到第 2 行,然后是 0,然后是 0。让我们合乎逻辑,先执行 0,然后执行 1,然后执行 2,以使代码更易于理解和维护。