Issue
My problem is not so big, but it is annoying.
See my JFrame
within my IDE (Netbeans 12.6):
(Pay attention to the distance of the text fields)
Now, see the same frame running:
Do you see the difference? This is too uncomfortable.
What do I do to make the distance of the elements equal in my running frame?
PS: the frame is set as non-resizable (maybe this info is useful).
Solution
I never used NetBeans IDE, let alone its GUI builder, before today. It is actually quite powerful. Even though it allows someone who doesn't know Swing to create a working application, I believe its real intent is to save experienced programmers from writing a lot of boilerplate code that Swing applications require. In other words, in order to get the most out of NetBeans GUI builder, you need to have a thorough knowledge of Swing and that only comes from experience by writing lots of Swing code (and not having the GUI builder generate the code for you). I would say that NetBeans GUI builder is for the Swing master and not for the Swing apprentice.
By default, NetBeans GUI builder uses GroupLayout which I assume is the layout manager used in your code but that seems to be giving you a problem – otherwise you wouldn't have posted your question. Hence you need to use nested JPanel
s with appropriate layout managers for each JPanel
– which is what I did.
Here is a screen capture of NetBeans IDE showing the hierarchy of the nested panels.
The layout manager for [content pane of] JFrame
is BorderLayout
. As you can see in the "tree" in the bottom left corner of the above image, JFrame
contains three panels, namely topPanel
(whose layout manager is BoxLayout
), resultPanel
and bottomPanel
(which both have FlowLayout
as their layout manager). Hopefully you will now be able to understand the rest of the "tree".
Now to your problem regarding the space between the JTextField
s in the top part of the GUI. As you can see, I used GridBagLayout since it will ensure that the locations of the text fields, relative to each other, will remain constant. Note that there are probably other layout managers that will also give you the same result but I will let you investigate other options if you so desire. Maybe look at JGoodies FormLayout. Note that I have never used it myself.
And so to complete this answer, here is the code (which was entirely generated by NetBeans). I did, however, comment out the code that sets the look-and-feel. Also, the below code only creates and displays the GUI. The JButton
s do nothing when you click on them.
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template
*/
package guibuild;
public class NbGuiBld extends javax.swing.JFrame {
/**
* Creates new form NbGuiBld
*/
public NbGuiBld() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;
buttonGroup = new javax.swing.ButtonGroup();
topPanel = new javax.swing.JPanel();
dataPanel = new javax.swing.JPanel();
formPanel = new javax.swing.JPanel();
nameLabel = new javax.swing.JLabel();
nameTextField = new javax.swing.JTextField();
weightLabel = new javax.swing.JLabel();
weightTextField = new javax.swing.JTextField();
heightLabel = new javax.swing.JLabel();
heightTextField = new javax.swing.JTextField();
genderPanel = new javax.swing.JPanel();
maleRadioButton = new javax.swing.JRadioButton();
femaleRadioButton = new javax.swing.JRadioButton();
buttonsPanel = new javax.swing.JPanel();
clearButton = new javax.swing.JButton();
calculateButton = new javax.swing.JButton();
resultPanel = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
bottomPanel = new javax.swing.JPanel();
exitButton = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("BMI Calculator");
topPanel.setPreferredSize(new java.awt.Dimension(538, 220));
topPanel.setLayout(new javax.swing.BoxLayout(topPanel, javax.swing.BoxLayout.PAGE_AXIS));
dataPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Personal Info"));
dataPanel.setLayout(new javax.swing.BoxLayout(dataPanel, javax.swing.BoxLayout.PAGE_AXIS));
formPanel.setPreferredSize(new java.awt.Dimension(526, 130));
formPanel.setLayout(new java.awt.GridBagLayout());
nameLabel.setText("Enter your name:");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 40);
formPanel.add(nameLabel, gridBagConstraints);
nameTextField.setColumns(15);
nameTextField.setMinimumSize(new java.awt.Dimension(171, 22));
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridy = 1;
gridBagConstraints.insets = new java.awt.Insets(0, 0, 10, 40);
formPanel.add(nameTextField, gridBagConstraints);
weightLabel.setText("Enter your weight (kg):");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 0;
gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 40);
formPanel.add(weightLabel, gridBagConstraints);
weightTextField.setColumns(10);
weightTextField.setMinimumSize(new java.awt.Dimension(116, 22));
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 1;
gridBagConstraints.insets = new java.awt.Insets(0, 0, 10, 40);
formPanel.add(weightTextField, gridBagConstraints);
heightLabel.setText("Enter your height (cm):");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 2;
gridBagConstraints.gridy = 0;
gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 0);
formPanel.add(heightLabel, gridBagConstraints);
heightTextField.setColumns(10);
heightTextField.setMinimumSize(new java.awt.Dimension(116, 22));
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 2;
gridBagConstraints.gridy = 1;
gridBagConstraints.insets = new java.awt.Insets(0, 0, 10, 0);
formPanel.add(heightTextField, gridBagConstraints);
dataPanel.add(formPanel);
genderPanel.setLayout(new java.awt.FlowLayout(1, 20, 5));
buttonGroup.add(maleRadioButton);
maleRadioButton.setText("male");
genderPanel.add(maleRadioButton);
buttonGroup.add(femaleRadioButton);
femaleRadioButton.setText("female");
genderPanel.add(femaleRadioButton);
dataPanel.add(genderPanel);
topPanel.add(dataPanel);
buttonsPanel.setPreferredSize(new java.awt.Dimension(824, 70));
buttonsPanel.setLayout(new java.awt.FlowLayout(1, 20, 5));
clearButton.setText("Clear");
buttonsPanel.add(clearButton);
calculateButton.setText("Calculate");
buttonsPanel.add(calculateButton);
topPanel.add(buttonsPanel);
getContentPane().add(topPanel, java.awt.BorderLayout.PAGE_START);
resultPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Result"));
jTextArea1.setColumns(60);
jTextArea1.setRows(15);
jScrollPane1.setViewportView(jTextArea1);
resultPanel.add(jScrollPane1);
getContentPane().add(resultPanel, java.awt.BorderLayout.CENTER);
bottomPanel.setLayout(new java.awt.FlowLayout(4));
exitButton.setText("Exit");
bottomPanel.add(exitButton);
getContentPane().add(bottomPanel, java.awt.BorderLayout.PAGE_END);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NbGuiBld.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NbGuiBld.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NbGuiBld.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NbGuiBld.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
*/
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NbGuiBld().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JPanel bottomPanel;
private javax.swing.ButtonGroup buttonGroup;
private javax.swing.JPanel buttonsPanel;
private javax.swing.JButton calculateButton;
private javax.swing.JButton clearButton;
private javax.swing.JPanel dataPanel;
private javax.swing.JButton exitButton;
private javax.swing.JRadioButton femaleRadioButton;
private javax.swing.JPanel formPanel;
private javax.swing.JPanel genderPanel;
private javax.swing.JLabel heightLabel;
private javax.swing.JTextField heightTextField;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JRadioButton maleRadioButton;
private javax.swing.JLabel nameLabel;
private javax.swing.JTextField nameTextField;
private javax.swing.JPanel resultPanel;
private javax.swing.JPanel topPanel;
private javax.swing.JLabel weightLabel;
private javax.swing.JTextField weightTextField;
// End of variables declaration
}
And this is how it looks when I run the above code.
Answered By - Abra
Answer Checked By - David Marino (JavaFixing Volunteer)