我一直在做一个项目。在项目的一部分中,我需要使用 JPanel。Graphics2D 对象被传递给框架的模型部分,模型将在该实例上绘制它需要绘制的任何内容。问题是 JFrame 未正确呈现。它看起来是半白色阴影并且没有正确绘画。它在我的笔记本电脑(Sony Vaio、Windows7)上运行良好,但在我测试的所有其他系统(2 台戴尔笔记本电脑、windows8;1 Thoshiba、windows7;1 三星、windows8)中无法正确渲染。它在我的队友测试的其他几个系统上也能正确渲染。
我确信模型部分没有错误。问题似乎没有在 JPanel 上呈现。但我可以提供的一个信息是,我们正在 Netbeans 中进行我们的项目。也许由 Netbeans IDE 生成的代码导致了问题?我检查了代码,但无法确定问题所在。很难为此发布 SSCCE,但我将分享一些有关该问题的屏幕截图。
好的,因为我没有足够的声誉来发布图片,请参阅下面的链接....
[Displays Wrongly][1]
[Displays Wrongly][2]
[Displays correctly on my system][3]
[Displays Correctly on my system][4]
我试图调试它,但不知道问题是什么...... 更新 它不是 SSCCE,但它可能会有所帮助
JFrame 的代码......
<pre>
import btapp.Question;
import btapp.qGens.QGenGeomAbstract;
import btapp.qGens.QGenI;
import java.awt.Graphics;
import java.util.ArrayList;
import javax.swing.JPanel;
public class UItestGeomFrame extends javax.swing.JFrame implements UIFrameI, UIGeomFrameAbstract {
/**
* Creates new form UItestGeomFrame
*/
public UItestGeomFrame() {
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() {
jLabel1 = new javax.swing.JLabel();
unitTestGeomPanel1 = new btapp.ui.UnitTestGeomPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Draw Panel JUnitTestCase");
unitTestGeomPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
javax.swing.GroupLayout unitTestGeomPanel1Layout = new javax.swing.GroupLayout(unitTestGeomPanel1);
unitTestGeomPanel1.setLayout(unitTestGeomPanel1Layout);
unitTestGeomPanel1Layout.setHorizontalGroup(
unitTestGeomPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 406, Short.MAX_VALUE)
);
unitTestGeomPanel1Layout.setVerticalGroup(
unitTestGeomPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 260, Short.MAX_VALUE)
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(160, Short.MAX_VALUE)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 263, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(142, 142, 142))
.addGroup(layout.createSequentialGroup()
.addGap(76, 76, 76)
.addComponent(unitTestGeomPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addGap(30, 30, 30)
.addComponent(unitTestGeomPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
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(UItestGeomFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(UItestGeomFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(UItestGeomFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(UItestGeomFrame.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 UItestGeomFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private btapp.ui.UnitTestGeomPanel unitTestGeomPanel1;
// End of variables declaration
@Override
public void presentQuestions(Question qO, int qNo) {
ArrayList qd = qO.getQuestionDraw();
unitTestGeomPanel1.setQObject(qd);
unitTestGeomPanel1.repaint();
}
@Override
public void presentAnswers(Question qO, int qNo) {
ArrayList ad = qO.getAnswerDraw();
unitTestGeomPanel1.setQObject(ad);
unitTestGeomPanel1.repaint();
}
@Override
public void presentQuestions(Question qO, int qNo, QGenI qg) {
unitTestGeomPanel1.setqGen(qg, qO, 0);
unitTestGeomPanel1.repaint();
}
@Override
public void presentAnswers(Question qO, int qNo, QGenI qg) {
unitTestGeomPanel1.setqGen(qg, qO, 1);
unitTestGeomPanel1.repaint();
}
@Override
public int getHeight(){
return unitTestGeomPanel1.getHeight();
}
@Override
public int getWidth(){
return unitTestGeomPanel1.getWidth();
}
}
</pre>
JPanel 的代码
导入 btapp. 问题; 导入 btapp.qGens.QGenGeomAbstract; 导入 btapp.qGens.QGenI; 导入 java.awt.Graphics; 导入 java.awt.Graphics2D; 导入 java.awt.Shape; 导入静态 java.awt.image.ImageObserver.HEIGHT; 导入静态 java.awt.image.ImageObserver.WIDTH; 导入 java.util.ArrayList; 公共类 UnitTestGeomPanel 扩展 javax.swing.JPanel { 私有 int 类型; /** * 创建新表单 UnitTestGeomPanel */ 公共 UnitTestGeomPanel() { 初始化组件(); } /** * 从构造函数中调用此方法以初始化表单。 * 警告:请勿修改此代码。这个方法的内容总是 * 由表单编辑器重新生成。 */ @SuppressWarnings("未选中") // 私人无效initComponents(){ javax.swing.GroupLayout 布局 = 新 javax.swing.GroupLayout(this); this.setLayout(布局); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 400, Short.MAX_VALUE) ); 布局.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 300, Short.MAX_VALUE) ); }// // 变量声明 - 不要修改 // 变量声明结束 ArrayList dAL = new ArrayList(); 私人双 h, w; @覆盖 公共无效paintComponent(图形g){ super.paintComponent(g); System.out.println("根据类型"+type的QO对象重新绘制); 如果(类型 == 0){ qg.presentQuestions(qO, (Graphics2D)g); } 别的 如果(类型 == 1) qg.presentAnswers(qO, (Graphics2D)g); } 无效 setQObject(ArrayList dAL) { 这个.dAL = dAL; } QGenI qg; 问题 qO; void setqGen(QGenI qg, Question qO, int type) { 这.qO = qO; 这个.qg = qg; this.type = 类型; } }
请帮助我...在此先感谢
编辑: 几天后我的系统也出现了问题,但没有那么严重。请参考评论中的图片。