I am trying to create for a first time a GUI with Swing.
I want to create it responsive so I am using layouts.
The problem comes with the space between the panel and the top of the frame because there is a big blank space:
Is my first time using Swing and I am trying to find why happens this but I can't find a solution. This is my simple code:
public class GUI2{
public static void main(String[] args){
// Create frame with title Registration Demo
JFrame frame= new JFrame();
//frame.setLocationRelativeTo(null);
frame.setTitle("HUPA Project ML Algorithm Selection");
// Panel to define the layout. We are using GridBagLayout
JPanel mainPanel = new JPanel();
BoxLayout bl = new BoxLayout(mainPanel, BoxLayout.Y_AXIS);
mainPanel.setLayout(bl);
JPanel headingPanel = new JPanel();
JLabel headingLabel = new JLabel("Choose the path of the files, the output and the ML Algorithm");
headingPanel.add(headingLabel);
// Panel to define the layout. We are using GridBagLayout
JPanel panel = new JPanel(new GridBagLayout());
// Constraints for the layout
GridBagConstraints constr = new GridBagConstraints();
//natural height, maximum width
constr.fill = GridBagConstraints.HORIZONTAL;
constr.insets = new Insets(5, 3, 3, 3);
//constr.anchor = GridBagConstraints.WEST;
// Declare the required Labels
JLabel userNameLabel = new JLabel("Enter your name :");
JLabel pwdLabel = new JLabel("Enter your password :");
JLabel emailLabel = new JLabel("Enter email :");
// Declare Text fields
JTextField userNameTxt = new JTextField(20);
JPasswordField pwdTxt = new JPasswordField(20);
JTextField emailTxt = new JTextField(20);
//navegadores de directorio
JButton navCSV = new JButton("CSV File");
navCSV.setPreferredSize(new Dimension(18, 18)); //ancho alto
JButton navArff = new JButton("ARFF");
navArff.setPreferredSize(new Dimension(18, 18));
JButton navOutput = new JButton("Output");
navOutput.setPreferredSize(new Dimension(18, 18));
//Botones de ML
JButton RT_weights = new JButton("Random Tree Weights");
RT_weights.setPreferredSize(new Dimension(125, 100));
JButton SA = new JButton("Random Tree No Weights");
SA.setPreferredSize(new Dimension(125, 100));
JButton NB = new JButton("Naive Bayes");
NB.setPreferredSize(new Dimension(125, 100));
constr.gridx=0; constr.gridy=0;
panel.add(RT_weights, constr);
constr.gridx=1; constr.gridy=0;
panel.add(SA, constr);
constr.gridx=2; constr.gridy=0;
panel.add(NB, constr);
// Set the initial grid values to 0,0
constr.gridx=0; constr.gridy=1;
panel.add(userNameLabel, constr);
constr.gridx=1; constr.gridy=1;
panel.add(userNameTxt, constr);
constr.gridx=2; constr.gridy=1;
panel.add(navCSV, constr);
constr.gridx=0; constr.gridy=2;
panel.add(pwdLabel, constr);
constr.gridx=1; constr.gridy=2;
panel.add(pwdTxt, constr);
constr.gridx=2; constr.gridy=2;
panel.add(navArff, constr);
constr.gridx=0; constr.gridy=3;
panel.add(emailLabel, constr);
constr.gridx=1; constr.gridy=3;
panel.add(emailTxt, constr);
constr.gridx=2; constr.gridy=3;
panel.add(navOutput, constr);
/*constr.gridwidth = 2;
constr.anchor = GridBagConstraints.CENTER;*/
// Button with text "Register"
JButton button = new JButton("Register");
// add a listener to button
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
headingLabel.setText("Thanks for registering. We'll get back to you shortly.");
userNameTxt.setText("");
pwdTxt.setText("");
emailTxt.setText("");
}
});
// Add label and button to panel
constr.gridx=0; constr.gridy=4;
constr.gridwidth = 3; //gridwith cuantas casillas ocupa del layout, aquí 3
panel.add(button, constr);
mainPanel.add(headingPanel);
mainPanel.add(panel);
// Add panel to frame
frame.add(mainPanel);
frame.pack();
frame.setSize(600, 600);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
//panel.setBorder(BorderFactory.createRaisedBevelBorder());
// panel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
}
}
If you need something more to know, just tell me it. Thanks in advance!
question from:
https://stackoverflow.com/questions/65858488/space-between-layout-and-top-of-the-frame-java-swing 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…