Home > Back-end >  Error: unable to initialize the main class SimpleGUI reasons: Java. Lang. NoClassDefFoundError: myDr
Error: unable to initialize the main class SimpleGUI reasons: Java. Lang. NoClassDefFoundError: myDr

Time:02-24

Error: unable to initialize the main class SimpleGUI
Reason: the Java. Lang. NoClassDefFoundError: myDrawPanel (wrong name: myDrawPanel)

Code purpose: to display a panel with a random color circle and a button, click on the button, change color,
How do I modify the code?

 import javax.mail. Swing. *; 
import java.awt.*;
Import the Java. The awt. Event. An ActionEvent;
Import the Java. The awt. Event. ActionListener;

The class MyDrawPanel extends JPanel {
Public void paintComponent (Graphics g) {
Graphics2D Graphics2D=g (Graphics2D);
Int red=(int) (Math. The random () * 255);
Int green=(int) (Math. The random () * 255);
Int blue=(int) (Math. The random () * 255);
Color startColor=new Color (red, green, blue);

Red=(int) (Math. The random () * 255);
Green=(int) (Math. The random () * 255);
Blue=(int) (Math. The random () * 255);
Color endColor=new Color (red, green, blue);

GradientPaint GradientPaint=new GradientPaint (70, startColor, 150150, endColor);
Graphics2D. SetPaint (gradientPaint);//the virtual brush painting gradually layer
Graphics2D. FillOval,70,100,100 (70);//use the brush to fill the oval area
}
}

Public class SimpleGUI implements ActionListener {

JFrame frame;

Public static void main (String [] args) {
SimpleGUI GUI=new SimpleGUI ();
GUI. The go ();
}

Public void actionPerformed (an ActionEvent event) {
Frame. The repaint ();
}

Public void the go () {
Frame=new JFrame ();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//set off or stop

JButton button=new JButton (" do not click ");
Button. AddActionListener (this);
MyDrawPanel drawPanel=new MyDrawPanel ();//create our myPanel object

Frame. The add (BorderLayout. CENTER, drawPanel);//add into the frame of the frame
Frame. The add (BorderLayout. SOUTH, the button).

Frame. The setSize (300300);
frame.setVisible(true);//set the framework for visible
}

}


CodePudding user response:

1. Use your code has achieved what you want without abnormal function
2. May be your eclipse or other compilation tools version is too low
  • Related