我一直在玩弄各种各样的解决方案,但似乎都不管用.在构造函数中,有一个默认值initComponents(),我无法更改.所以我创建了第二个方法myComponents().我使用的类扩展了JFrame,所以我不需要在任何初始值设定项中创建另一个帧.我创建了一个JPanel,带有一个垂直Boxlayout、两个标签、一个文本字段和一个按钮.我将面板添加到框架中,然后将所有组件添加到面板中,然后打包所有组件.

我不确定为什么在运行时除了框架之外什么都没有出现.

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 builder,而myComponents()则是程序员控制GUI构造,手动编写代码.

使用其中一个.我总是使用后者,它不需要扩展JFrame.事实上,在JPanel中创建GUI的主视图并将单个组件添加到JFrame是我们通常推荐的方式.

Note:另见accepted answerNetbeans GUI editor generating its own incomprehensible code

Java相关问答推荐

如何审查Java dtos中的自定义注释字段?

使用包私有构造函数强制子类Java类

在Java中测试DAO方法:假实现与内存数据库

多个Java线程和TreeMap.put()的非预期行为

RESTful框架类字段是安全的还是不安全的

调用引发泛型异常的泛型方法时出现编译错误

不推荐使用的Environment.getExternalStorageDirectory().getAbsolutePath()返回的值不同于新的getExternalFilesDir(空)?

如何创建同一类的另一个对象,该对象位于变量中?

Jenv-相同的Java版本,但带有前缀

使用PDFBox从PDF中删除图像

每次FXMLLoader调用ApplationConext.getBean(类)时创建@Component的新实例

错误:未找到扩展元素在JBossEAP 7.2中安装FUSE时出错

我的代码是线程安全的吗?[Java、CAS、转账]

为什么mvn编译生命周期阶段不只是编译已更改的java文件?

为什么项目名称出现在我的GET请求中?

使用@ExceptionHandler的GlobalExceptionHandler还是来自服务器的REST应答的ResponseEntity?

AspectJ编织外部依赖代码,重新打包jar并强制依赖用户使用它

spring 更新多项管理关系

声纳覆盖范围为 0%,未生成 jacoco.xml

如何在Java中调用对象上预定义的接口方法列表?