我遇到了在Netbeans Swing Editor中向JFrame添加自定义JPanel的问题。
为了排除其他问题,我制作了一个全新的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类中完成的自定义内容,但无济于事。
答案 0 :(得分:1)
我从来没有把它固定下来,但我认为这与错误的文件跟踪有关或者我的JPanel类文件损坏了。
将方法和变量移动到新的JPanel类似乎工作正常,因此这是解决方法。
答案 1 :(得分:0)
前一段时间我遇到了这个问题,清理和构建项目对我来说很有效。