1

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

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

警告:无法从项目加载组件类 GUI。(nameofpanel)...(路径)...找不到该类。请注意,该类必须经过编译,并且必须是目标 GUI 表单所属项目的源或依赖项的一部分。

在使用此方法之前,我已成功将 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 类中完成的自定义内容,但无济于事。

4

2 回答 2

1

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

将方法和变量移动到新的 JPanel 类似乎工作正常,所以这就是解决方法。

于 2013-04-24T21:17:38.900 回答
0

我前段时间遇到过这个问题,清理和构建我的项目对我有用。

于 2020-07-15T11:29:40.577 回答