我目前使用以下代码创建了一个JFrame。我正在寻找三个JLabel,一个在另一个之上,其对应的JTextFields在右边,以及四个JButton在它们下面(我最终将使用插入物将它们隔开)。但是,单元格在框架的顶部对齐一行。任何人都可以发现我正在犯的错误。谢谢。
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());
现在已经产生了窗口:
最佳答案
我意识到我已经在面板中设置了GridBagLayout,因此添加了以下行:
uploadPanel.setLayout(new GridBagLayout());
你在哪做的
您发布的代码中没有可变的“ uploadPanel”。
实际上,由于在将组件添加到面板之前需要设置面板的布局管理器,因此无需定义变量。
因此,布局管理器应在“ UploadPanel”类的构造函数中设置:
public UploadPanel()
{
setLayout( new GridBagLayout() );
gbc.weightx = 1;
而且,您的代码结构令人困惑。
相关组件应同时添加到面板中。那就是应该将标签和文本字段对一起添加到面板中。
在单独的方法中添加所有标签,然后添加所有文本字段是没有意义的。
另外,您的代码在第2行中添加了一个组件,然后是0,然后是0。让逻辑化并先执行0,然后是1,然后是2,以使代码更易于理解和维护。