我创建了这段代码,当我在 JForm 上选择一个单选按钮时应该绘制某些东西,我使用 NetBeans 创建 GUI。当我选择一个单选按钮时,没有任何反应。一段时间以来,我一直试图找出问题所在,但我仍然找不到解决方案,这就是我来到这里的原因。如果有人能发现错误,我将不胜感激。
public class DrawShapesGUI extends javax.swing.JFrame {
private int figureID;
public DrawShapesGUI() {
initComponents();
repaint();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code"></editor-fold>
private void lineButtonActionPerformed(java.awt.event.ActionEvent evt) {
int figureID = 1;
repaint();
}
private void rectButtonActionPerformed(java.awt.event.ActionEvent evt) {
int figureID = 2;
repaint();
}
private void ovalButtonActionPerformed(java.awt.event.ActionEvent evt) {
int figureID = 3;
repaint();
}
private void arcButtonActionPerformed(java.awt.event.ActionEvent evt) {
int figureID = 4;
repaint();
}
private void polygonButtonActionPerformed(java.awt.event.ActionEvent evt) {
int figureID = 5;
repaint();
}
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(DrawShapesGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(DrawShapesGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(DrawShapesGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(DrawShapesGUI.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 DrawShapesGUI().setVisible(true);
}
});
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.red);
if (figureID == 1) {
g.drawLine(50, 50, 100, 100);
} else if (figureID == 2) {
g.fillRect(50, 50, 100, 100);
} else if (figureID == 3) {
g.fillOval(100, 100, 100, 60);
} else if (figureID == 4) {
g.drawArc(50, 50, 200, 200, 90, 30);
} else if (figureID == 5) {
Polygon poly = new Polygon();
poly.addPoint(100, 50);
poly.addPoint(150, 50);
poly.addPoint(200, 100);
poly.addPoint(150, 150);
poly.addPoint(100, 150);
poly.addPoint(50, 100);
g.fillPolygon(poly);
}
}