So I'm using a GridBagLayout and i'm trying to create a JButton in the center of the JPanel, and then have a JLabel at the very top of the JPanel. When I try to do this the button and label are not aligned.
Code:
package view;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class StartPanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
//Declare our global variables
JButton btnUploadProject;
JLabel heading;
GridBagConstraints gbc;
GridBagConstraints gbc2;
/**
* Create the panel.
*/
public StartPanel() {
//Set up Panel
this.setVisible(true);
setLayout(new GridBagLayout());
//Create the components
btnUploadProject = new JButton("Upload A Project");
heading = new JLabel("Heading test");
gbc = new GridBagConstraints();
gbc2 = new GridBagConstraints();
//Modify components
btnUploadProject.setPreferredSize(new Dimension(400,100));
btnUploadProject.setFont(new Font("Arial", Font.PLAIN, 40));
heading.setFont(new Font("Arial", Font.PLAIN, 40));
gbc.anchor = GridBagConstraints.CENTER;
gbc2.anchor = GridBagConstraints.NORTH;
gbc2.weighty = 1.0;
//Add the buttons
this.add(btnUploadProject, gbc);
this.add(heading, gbc2);
}
}
