我一直在做一个项目 . 在项目的一部分,我需要在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]
我试图调试它但不知道问题是什么.... Update 这不是SSCCE但它可能有帮助
JFrame代码....
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")
//
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();
}//
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//
/* 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);
}
//
/* 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();
}
}
JPanel的代码
import btapp.Question;
import btapp.qGens.QGenGeomAbstract;
import btapp.qGens.QGenI;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import static java.awt.image.ImageObserver.HEIGHT;
import static java.awt.image.ImageObserver.WIDTH;
import java.util.ArrayList;
public class UnitTestGeomPanel extends javax.swing.JPanel {
private int type;
/**
* Creates new form UnitTestGeomPanel
*/
public UnitTestGeomPanel() {
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")
//
private void initComponents() {
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.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)
);
}//
// Variables declaration - do not modify
// End of variables declaration
ArrayList dAL = new ArrayList();
private double h, w;
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
System.out.println("repainign based on QO object of type "+type);
if(type == 0){
qg.presentQuestions(qO, (Graphics2D)g);
}
else
if(type == 1)
qg.presentAnswers(qO, (Graphics2D)g);
}
void setQObject(ArrayList dAL) {
this.dAL = dAL;
}
QGenI qg;
Question qO;
void setqGen(QGenI qg, Question qO, int type) {
this.qO = qO;
this.qg = qg;
this.type = type;
}
}
请帮帮我...先谢谢
EDIT: 几天后我的系统出现了问题,但情况并没有那么糟 . 请参阅评论中的图片 .