我目前已使用以下代码创建了一个 JFrame。我希望三个 JLabel 一个在另一个之上,它们相应的 JTextFields 在右侧,四个 JButtons 在它们下面(我最终将使用插图将它们分开)。然而,单元格在框架顶部对齐成一行。谁能发现我犯的错误。谢谢。
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());
现在已经生成了窗口:
最佳答案
I've realised I left setting the GridBagLayout in the panel so added the line:
uploadPanel.setLayout(new GridBagLayout());
你在哪里做的?
您发布的代码中没有变量“uploadPanel”。
事实上,不需要定义变量,因为在将组件添加到面板之前需要设置面板的布局管理器。
因此布局管理器应该在“UploadPanel”类的构造函数中设置:
public UploadPanel()
{
setLayout( new GridBagLayout() );
gbc.weightx = 1;
此外,您的代码结构很困惑。
相关组件应同时添加到面板中。这就是标签和文本字段对应该一起添加到面板中。
在单独的方法中添加所有标签,然后添加所有文本字段是没有意义的。
此外,您的代码还向第 2 行添加了一个组件,然后是 0,然后是 0。让我们合乎逻辑,先执行 0,然后执行 1,然后执行 2,以使代码更易于理解和维护。
关于java - 元素与 GridBagConstraints 不正确对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61424133/