我正在尝试使用Netbeans GUI Builder在按键时旋转一行。代码已经简化,但我会回答任何问题。
我已经让实际的旋转单独工作但是将它与按钮组合在一起会给我带来以下错误:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at exam2.Exam2.jButton3MouseClicked(Exam2.java:372)
at exam2.Exam2.access$200(Exam2.java:18)
at exam2.Exam2$3.mouseClicked(Exam2.java:222)
at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270
myMouseClicked事件,它读取jtextfield中的新值,并将其保存在newhour中。
//function of JFrame
private void jButton3MouseClicked(java.awt.event.MouseEvent evt) {
setHour();
Integer newHour = (Integer) currentHour +1;
String newHourStr = newHour.toString();
jTextField1.setText(newHourStr);
myCanvas.updateHour(newHour); //this is where the error occurs
}
//function of Canvas which extends JPanel
public void updateHour (int currentHour) {
currentHour = getHour();
System.out.println("the hour at hand" + currentHour); //does not print this
int currentAngle = currentHour*30;
Stroke stroke = new BasicStroke(3f);
g2d.setStroke(stroke);
double lengthHour = Math.pow(Math.pow(centerX-this.getWidth()/2,2)+Math.pow(centerY-(this.getHeight()/2)-radius,2),0.5);
int xHourChange = (int) (lengthHour * Math.cos(Math.toRadians(currentAngle)));
int yHourChange = (int) (lengthHour * Math.sin(Math.toRadians(currentAngle)));
g2d.drawLine(centerX, centerY, centerX + xHourChange, centerY - yHourChange);
}
//function of Canvas which extends JPanel
public void paintComponent(Graphics g){
g.setColor(Color.WHITE);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(Color.BLACK);
drawCircle(g);
drawFace(g);
drawHour(g); //this works perfectly
//drawMinute(g);
//drawSecond(g);
}