为什么 JLabel 或 JButton 在 运行 GUI 时不显示?
Why won't the JLabels or JButtons show up when running the GUI?
我一直在尝试多种解决方案,但其中 none 似乎有效。在构造函数中,有一个默认值 initComponents()
我无法更改。所以我创建了第二个方法 myComponents()
。 class 我正在使用扩展 JFrame
所以我不需要在任何初始化程序中创建另一个框架。我创建了一个带有垂直 Boxlayout
、2 个标签、1 个文本字段和 1 个按钮的 JPanel
。我将面板添加到框架,然后将所有组件添加到面板,然后打包所有内容。
我不确定为什么在 运行 时除了框架什么都没有显示。
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
*
* @author gpbli
*/
public class MAINFRAME extends javax.swing.JFrame {
/**
* Creates new form MAINFRAME
*/
//MAINFRAME frame = new MAINFRAME();
JPanel homescreen = new JPanel();
JLabel numOfIngredLabel = new JLabel();
JTextField numOfIngred = new JTextField();
JButton okButton = new JButton();
JLabel logo = new JLabel();
ImageIcon img = new ImageIcon("logo_resized.png");
public MAINFRAME() {
initComponents();
myComponents();
}
public void myComponents(){
BoxLayout box = new BoxLayout(homescreen,BoxLayout.Y_AXIS);
homescreen.setLayout(box);
numOfIngredLabel.setText("How many Ingredients");
numOfIngred.setText("");
okButton.setText("OK");
logo.setText(" ");
logo.setIcon(img);
add(homescreen);
homescreen.add(logo);
homescreen.add(numOfIngredLabel);
homescreen.add(numOfIngred);
homescreen.add(okButton);
numOfIngredLabel.setVisible(true);
numOfIngred.setVisible(true);
okButton.setVisible(true);
logo.setVisible(true);
pack();
revalidate();
}
/**
* 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() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
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(MAINFRAME.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(MAINFRAME.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(MAINFRAME.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(MAINFRAME.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 MAINFRAME().setVisible(true);
}
});
}
// Variables declaration - do not modify
// End of variables declaration
}
initComponents()
的出现表明 IDE 期望程序员使用 GUI 生成器,而 myComponents()
是程序员控制 GUI 构造并通过手.
使用一个或另一个。我总是使用后者,它不需要扩展 JFrame
。事实上,在 JPanel
中创建 GUI 的主视图并将该单个组件添加到 JFrame
是我们通常推荐的方式。
注:另见accepted answer to: Netbeans GUI editor generating its own incomprehensible code
我一直在尝试多种解决方案,但其中 none 似乎有效。在构造函数中,有一个默认值 initComponents()
我无法更改。所以我创建了第二个方法 myComponents()
。 class 我正在使用扩展 JFrame
所以我不需要在任何初始化程序中创建另一个框架。我创建了一个带有垂直 Boxlayout
、2 个标签、1 个文本字段和 1 个按钮的 JPanel
。我将面板添加到框架,然后将所有组件添加到面板,然后打包所有内容。
我不确定为什么在 运行 时除了框架什么都没有显示。
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
*
* @author gpbli
*/
public class MAINFRAME extends javax.swing.JFrame {
/**
* Creates new form MAINFRAME
*/
//MAINFRAME frame = new MAINFRAME();
JPanel homescreen = new JPanel();
JLabel numOfIngredLabel = new JLabel();
JTextField numOfIngred = new JTextField();
JButton okButton = new JButton();
JLabel logo = new JLabel();
ImageIcon img = new ImageIcon("logo_resized.png");
public MAINFRAME() {
initComponents();
myComponents();
}
public void myComponents(){
BoxLayout box = new BoxLayout(homescreen,BoxLayout.Y_AXIS);
homescreen.setLayout(box);
numOfIngredLabel.setText("How many Ingredients");
numOfIngred.setText("");
okButton.setText("OK");
logo.setText(" ");
logo.setIcon(img);
add(homescreen);
homescreen.add(logo);
homescreen.add(numOfIngredLabel);
homescreen.add(numOfIngred);
homescreen.add(okButton);
numOfIngredLabel.setVisible(true);
numOfIngred.setVisible(true);
okButton.setVisible(true);
logo.setVisible(true);
pack();
revalidate();
}
/**
* 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() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
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(MAINFRAME.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(MAINFRAME.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(MAINFRAME.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(MAINFRAME.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 MAINFRAME().setVisible(true);
}
});
}
// Variables declaration - do not modify
// End of variables declaration
}
initComponents()
的出现表明 IDE 期望程序员使用 GUI 生成器,而 myComponents()
是程序员控制 GUI 构造并通过手.
使用一个或另一个。我总是使用后者,它不需要扩展 JFrame
。事实上,在 JPanel
中创建 GUI 的主视图并将该单个组件添加到 JFrame
是我们通常推荐的方式。
注:另见accepted answer to: Netbeans GUI editor generating its own incomprehensible code