IDE 内的 JFrame 与 运行 内的 JFrame 不成比例
Disproportionality between JFrame within the IDE and the same running
我的问题不是很大,但是很烦人。
在我的 IDE (Netbeans 12.6) 中查看我的 JFrame
:
Frame within IDE
(注意文本字段的距离)
现在,看到同框运行:
Frame running
你看出区别了吗?这也太难受了吧
如何使 运行 框架中元素的距离相等?
PS: 框架被设置为不可调整大小(也许这个信息有用)。
我从未使用过 NetBeans IDE,更不用说它 GUI builder, before today. It is actually quite powerful. Even though it allows someone who doesn't know Swing 来创建一个可用的应用程序了,我相信它的真正目的是避免有经验的程序员编写大量 Swing 应用程序需要。换句话说,为了充分利用 NetBeans GUI 构建器,您需要对 Swing 有透彻的了解,而这只能来自编写大量 的经验Swing 代码(并且不让 GUI 构建器为您生成代码)。我会说 NetBeans GUI 构建器是为 Swing 大师而不是 Swing 学徒准备的。
默认情况下,NetBeans GUI 构建器使用 GroupLayout,我假设这是您代码中使用的布局管理器,但这似乎给您带来了问题 – 否则您不会发布您的问题。因此,您需要为每个 JPanel
使用带有适当布局管理器的嵌套 JPanel
——这就是我所做的。
这是 NetBeans IDE 的屏幕截图,显示了嵌套面板的层次结构。
JFrame
[内容窗格]的布局管理器是BorderLayout
。在上图左下角的“树”中可以看到,JFrame
包含三个面板,分别是topPanel
(其布局管理器为BoxLayout
)、resultPanel
和 bottomPanel
(它们都有 FlowLayout
作为它们的布局管理器)。希望您现在能够理解“树”的其余部分。
现在回答关于 GUI 顶部 JTextField
之间的 space 的问题。如您所见,我使用了 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。请注意,我自己从未使用过它。
为了完成这个答案,这里是代码(完全由 NetBeans 生成)。但是,我确实注释掉了设置 look-and-feel 的代码。此外,以下代码仅创建和显示 GUI。当您单击 JButton
时,它们什么也不做。
/*
* 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
}
这就是我 运行 上述代码时的样子。
我的问题不是很大,但是很烦人。
在我的 IDE (Netbeans 12.6) 中查看我的 JFrame
:
Frame within IDE
(注意文本字段的距离)
现在,看到同框运行:
Frame running
你看出区别了吗?这也太难受了吧
如何使 运行 框架中元素的距离相等?
PS: 框架被设置为不可调整大小(也许这个信息有用)。
我从未使用过 NetBeans IDE,更不用说它 GUI builder, before today. It is actually quite powerful. Even though it allows someone who doesn't know Swing 来创建一个可用的应用程序了,我相信它的真正目的是避免有经验的程序员编写大量 Swing 应用程序需要。换句话说,为了充分利用 NetBeans GUI 构建器,您需要对 Swing 有透彻的了解,而这只能来自编写大量 的经验Swing 代码(并且不让 GUI 构建器为您生成代码)。我会说 NetBeans GUI 构建器是为 Swing 大师而不是 Swing 学徒准备的。
默认情况下,NetBeans GUI 构建器使用 GroupLayout,我假设这是您代码中使用的布局管理器,但这似乎给您带来了问题 – 否则您不会发布您的问题。因此,您需要为每个 JPanel
使用带有适当布局管理器的嵌套 JPanel
——这就是我所做的。
这是 NetBeans IDE 的屏幕截图,显示了嵌套面板的层次结构。
JFrame
[内容窗格]的布局管理器是BorderLayout
。在上图左下角的“树”中可以看到,JFrame
包含三个面板,分别是topPanel
(其布局管理器为BoxLayout
)、resultPanel
和 bottomPanel
(它们都有 FlowLayout
作为它们的布局管理器)。希望您现在能够理解“树”的其余部分。
现在回答关于 GUI 顶部 JTextField
之间的 space 的问题。如您所见,我使用了 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。请注意,我自己从未使用过它。
为了完成这个答案,这里是代码(完全由 NetBeans 生成)。但是,我确实注释掉了设置 look-and-feel 的代码。此外,以下代码仅创建和显示 GUI。当您单击 JButton
时,它们什么也不做。
/*
* 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
}
这就是我 运行 上述代码时的样子。