为什么在运行GUI时会出现Jlabels或Jbuttons?

发布于 2025-01-22 10:00:43 字数 4295 浏览 3 评论 0原文

我一直在玩许多解决方案,但它们似乎都没有用。在构造函数中,有一个默认的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                   
}

I have been playing around with numerous solutions but none of them seems to work. in the constructor, there is a default initComponents() which I can not change. So I created a second method called myComponents(). The class I am using extends JFrame so I do not need to create another frame in any initializer. I created a JPanel with a vertical Boxlayout, 2 labels, 1 text field, and 1 button. I added the panel to the frame then I added all the components to the panel then packed everything.

I am unsure as to why nothing shows up besides the frame when it is run.

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                   
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清音悠歌 2025-01-29 10:00:43

initcomponents()的外观表明,IDE期望程序员使用GUI构建器,而myComponents()是程序员控制GUI构造来编码它的程序员手工。

使用一个或另一个。我总是使用后者,而后者不需要jframe进行扩展。实际上,在jpanel中创建GUI的主要视图,并将该组件添加到jframe中是我们通常建议的方式。

注意:另请参见接受答案 to: NetBeans GUI编辑器生​​成自己的难以理解的代码

The appearance of initComponents() suggests the IDE is expecting the programmer to use the GUI builder, while the myComponents() is the programmer taking control of the GUI construction to code it by hand.

Use one or the other. I'd always use the latter, which does not require the JFrame to be extended. In fact, creating the main view of the GUI in a JPanel and adding that single component to a JFrame is the way we would typically recommend.

Note: See also the accepted answer to: Netbeans GUI editor generating its own incomprehensible code

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文