【问题标题】:Netbeans: Cannot add custom JPanel in Swing EditorNetbeans:无法在 Swing 编辑器中添加自定义 JPanel
【发布时间】:2013-04-18 14:21:23
【问题描述】:

我在将自定义 JPanel 添加到 Netbeans Swing 编辑器中的 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 类中完成的自定义内容,但无济于事。

【问题讨论】:

  • 确保你已经编译了这个类(以及它的所有依赖项)。如果它仍然不起作用,请检查在 {user.home}/AppData/Roaming/Netbeans/{version}/logs 或 Windows 上类似的位置找到的 message.log 文件,它将列出确切的异常
  • 好的,它让我编译文件,但是在尝试将它拖入时它给了我同样的错误。我查看了 message.log,确切的错误是:java.lang.ClassNotFoundException : GUI.CardPanel,这并不完全有用。
  • 尝试清理并构建整个项目
  • 嘿,我做了一个清理和构建,然后编译了文件,它神奇地工作。现在看看自定义代码是否也有效。
  • 不,重新添加方法,进行清理和构建,然后编译,没有骰子。删除了方法,干净的构建,编译,没有骰子了。

标签: java swing netbeans jpanel


【解决方案1】:

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

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

【讨论】:

  • 这是一个非常古老的问题,但如果其他人看到这个问题,我遇到了同样的问题,问题是我试图将 build 和 dist 目录设置为源外。也就是说,我在项目目录之外设置了“build.dir”和“dist.dir”。该项目仍会编译,但 Netbeans GUI 构建器似乎无法在这些自定义位置找到已编译的代码。从来没有追查过如何做到这两点。当我需要使用 GUI 构建器时,我只是在项目内编译。
  • 后来者的不同解决方案:我必须编辑 .form 以将完整的类路径包含到我的自定义组件中,即将 CardPanel 更改为 GUI.CardPanel
  • 对我来说也有不同的解决方案 - 结果我的自定义组件缺少无参数构造函数,这使得无法在 GUI 构建器中添加空白。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2014-05-05
    • 1970-01-01
    • 1970-01-01
    • 2011-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多