Netbeans:不能在Swing Editor中添加自定义JPanel



我在Netbeans Swing Editor中添加自定义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类中所做的自定义内容,但无济于事。

我从来没有把这个固定下来,但我认为这与错误的文件跟踪或关于我的JPanel类文件损坏的东西有关。

将方法和变量移动到一个新的JPanel类中似乎可以很好地工作,所以这就是解决方法。

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

相关内容

  • 没有找到相关文章

最新更新