错误: 无法初始化主类 SimpleGUI 原因: java.lang.NoClassDefFoundError: myDrawPanel (wrong nam

Foools 2021-02-23 11:53:29
错误: 无法初始化主类 SimpleGUI
原因: java.lang.NoClassDefFoundError: myDrawPanel (wrong name: MyDrawPanel)

代码目的:显示一个面板,带有一个随机颜色的圆和一个按钮,点击按钮,圆变颜色。
请问如何修改代码?

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

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

GradientPaint gradientPaint = new GradientPaint(70,70,startColor,150,150,endColor);
graphics2D.setPaint(gradientPaint); //将虚拟的笔刷画成渐层
graphics2D.fillOval(70,70,100,100); //用目前的笔刷来填满椭圆形区域
}
}

public class SimpleGUI implements ActionListener {

JFrame frame;

public static void main(String[] args){
SimpleGUI gui = new SimpleGUI();
gui.go();
}

public void actionPerformed(ActionEvent event){
frame.repaint();
}

public void go(){
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置关闭即停止

JButton button = new JButton("do not click") ;
button.addActionListener(this);
MyDrawPanel drawPanel = new MyDrawPanel(); //创建我们的myPanel对象

frame.add(BorderLayout.CENTER,drawPanel); //add进frame框架中
frame.add(BorderLayout.SOUTH,button);

frame.setSize(300,300);
frame.setVisible(true); //设置框架为可见
}

}


...全文
184 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
顾染尘 2021-02-23
  • 打赏
  • 举报
回复
1.使用你的代码 没有出现异常 已经实现了你想要的功能 2.可能是你的eclipse或者其他编译工具版本太低导致的

50,526

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧