为什么运行GUI时JLabels或JButtons不显示?



我一直在玩许多解决方案,但似乎没有一个工作。在构造函数中,有一个默认的initComponents(),我不能改变。所以我创建了第二种方法,叫做myComponents()。我使用的类扩展了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中是我们通常推荐的方法。

注意:参见Netbeans GUI编辑器生成自己难以理解的代码

最新更新