我在將Netbeans Swing編輯器中的JFrame添加到自定義JPanel時遇到了問題。Netbeans:無法在Swing編輯器中添加自定義JPanel
爲了排除其他問題,我做了一個全新的JFrame,並嘗試拖動我的JPanel(JPanel甚至在同一個包中)。它彈出一個錯誤信息:
警告:無法加載組件類GUI(nameofpanel)從項目 ...(路徑)...類找不到。請注意,該類必須編譯爲 ,並且必須是目標GUI窗體所屬的 項目的源或依賴項的一部分。
我已經成功地將JPanels添加到框架之前使用此方法,但現在它阻止我這樣做。我在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文件,它將列出確切的例外 – MadProgrammer 2013-04-24 21:02:18
好吧,它讓我編譯這個文件,但是當它試圖拖拽時,它給了我同樣的錯誤。我查看了message.log,確切的錯誤是:java.lang.ClassNotFoundException:GUI.CardPanel,它不是完全有幫助。 – 2013-04-24 21:05:10
嘗試並清理並構建整個項目 – MadProgrammer 2013-04-24 21:11:34