java - Netbeans:无法在 Swing 编辑器中添加自定义 JPanel

标签 java swing netbeans jpanel

我在向 Netbeans Swing 编辑器中的 JFrame 添加自定义 JPanel 时遇到问题。

为了排除其他问题,我制作了一个全新的JFrame,并尝试将我的JPanel拖进去(JPanel甚至在同一个包中)。弹出错误消息:

Warning: Cannot Load component Class GUI.(nameofpanel) from project ...(path)... The class could not be found. Note the class must be compiled and must be part of the sources or dependencies of the project where the target GUI form belongs to.

在使用此方法之前,我已成功将 JPanel 添加到框架中,但现在它阻止我这样做。我在 Netbeans 7.1 和 7.3 版本中遇到相同的错误。

我认为我可能需要构建 JPanel 类文件,但这样做的选项是灰色的。我什至尝试过清理和构建,但没有效果。

这是我的 JFrame:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

/**
 *
 * @author chris
 */
public class MainFrame extends javax.swing.JFrame {

    /**
     * Creates new form MainFrame
     */
    public MainFrame() {
        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() {

        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                   
}

还有我的 JPanel:

package GUI;


import java.awt.Image;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;

/*
 * Represents one card graphically /**
 *
 * @author Student-HSLH133
 */
public class CardPanel extends javax.swing.JPanel {

    private Image cardImage;

    /**
     * Creates new form CardPanel
     */
    public CardPanel() {
        initComponents();
        //Set to face down card initially
        //Hey look, some Lisp code, jk
        try {
            cardLabel.setIcon(imageToIcon(ImageIO.read(new File("images/gbCard52.gif"))));
        }
        catch (Exception e){
            System.out.println("Failed to load the image.");
            System.exit(-1);
        }
    }

    public void setCard(Image img) {
        cardLabel.setIcon(imageToIcon(img));
        repaint();
    }

// --------------   end of load_picture ---------------------------
    private ImageIcon imageToIcon(Image img) {
        return new ImageIcon(img);
    }

    /**
     * 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() {

        cardLabel = new javax.swing.JLabel();

        setMaximumSize(new java.awt.Dimension(72, 102));
        setMinimumSize(new java.awt.Dimension(72, 102));
        setPreferredSize(new java.awt.Dimension(72, 102));
        setLayout(new java.awt.BorderLayout());

        cardLabel.setMaximumSize(new java.awt.Dimension(72, 102));
        cardLabel.setMinimumSize(new java.awt.Dimension(72, 102));
        cardLabel.setPreferredSize(new java.awt.Dimension(72, 102));
        add(cardLabel, java.awt.BorderLayout.CENTER);
        cardLabel.getAccessibleContext().setAccessibleName("card");
    }// </editor-fold>                        
    // Variables declaration - do not modify                     
    private javax.swing.JLabel cardLabel;
    // End of variables declaration                   
}

我完全迷失了。这是怎么回事?

编辑:我以某种方式得到它来编译 JPanel,但我仍然遇到相同的错误。我认为这可能是 JPanel 本身的问题,所以我制作了一个新的自定义 JPanel,编译了它,并尝试添加它,它成功了。

编辑2:我尝试注释掉我在 JPanel 类中所做的自定义内容,但没有成功。

最佳答案

我从未确定过这一点,但我认为这与错误的文件跟踪或我的 JPanel 类文件的损坏有关。

将方法和变量移至新的 JPanel 类似乎工作正常,因此这就是解决方法。

关于java - Netbeans:无法在 Swing 编辑器中添加自定义 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16202054/

相关文章:

java - hadoop map reduce程序中的InstantiationException

java - 导出到 json 时生成的数据库实体的循环依赖

java - 如何区分用户选择项目和 getSelectedValue()?

java - 使用 Maven 项目在 NetBeans 中运行应用程序,提示没有要编译的源

java - 类型集不带参数

java - 如何检查 URL 是否包含/预览或子域是 CI?

java - 2 的倍数的 boolean 表达式

java - 在Java中,删除操作是正确的,但catch也会被执行

java - Java Swing 中不稳定的框架和窗口?

java - 验证旧 CSV 文件中的信息后打印写入新 CSV 文件并将 DATA INFILE 加载到 MySQL 中