DYNskr 2021-07-22 20:10 采纳率: 84.6%
浏览 69
已结题

java:按钮操作绘制图形


import java.awt.*;
import javax.swing.*;
public class cycle extends JFrame implements ActionListener{
JButton button;
public static void main(String[] args){
cycle cycle1=new cycle();
cycle1.go();
}
public cycle(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400,400);
setVisible(true);
}
public void go(){
button = new JButton("click me");
button.addActionListener(this);
getContentPane().add(button);
}

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


public void paint(Graphics g){
Image image = new ImageIcon("picture1.jpg").getImage();
g.drawImage(image,3,4,this);

int red = (int)(Math.random()*255);
int green = (int)(Math.random()*255);
int blue = (int)(Math.random()*255);

Color randomColor = new Color(red,green,blue);
g.setColor(randomColor);
g.fillOval(70,70,100,100);

}
public void paintComponent(Graphics g){
int red = (int)(Math.random()*255);
int green = (int)(Math.random()*255);
int blue = (int)(Math.random()*255);

Color randomColor = new Color(red,green,blue);
g.setColor(randomColor);
g.fillOval(70,70,100,100);
}

} 

cycle.java:20: 错误: 找不到符号
public void actionPerformed(ActionEvent event) {
^
符号: 类 ActionEvent
位置: 类 cycle
cycle.java:3: 错误: cycle不是抽象的, 并且未覆盖ActionListener中的抽象方法actionPerformed()
public class cycle extends JFrame implements ActionListener{
^
cycle.java:16: 错误: 不兼容的类型: cycle无法转换为ActionListener
button.addActionListener(this);
^
注: 某些消息已经过简化; 请使用 -Xdiags:verbose 重新编译以获得完整输出
3 个错误

问题出在哪里啊

  • 写回答

3条回答 默认 最新

  • 404警告 2021-07-23 09:34
    关注

    换成如下 应该是导错包或者没导包

    
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.*;
    
    public class Cycle extends JFrame implements ActionListener {
        JButton button;
    
        public static void main(String[] args) {
            Cycle cycle1 = new Cycle();
            cycle1.go();
        }
    
        public Cycle() {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(400, 400);
            setVisible(true);
        }
    
        public void go() {
            button = new JButton("click me");
            button.addActionListener(this);
            getContentPane().add(button);
        }
    
        public void actionPerformed(ActionEvent event) {
            repaint();
        }
    
        public void paint(Graphics g) {
            Image image = new ImageIcon("picture1.jpg").getImage();
            g.drawImage(image, 3, 4, this);
            int red = (int) (Math.random() * 255);
            int green = (int) (Math.random() * 255);
            int blue = (int) (Math.random() * 255);
            Color randomColor = new Color(red, green, blue);
            g.setColor(randomColor);
            g.fillOval(70, 70, 100, 100);
        }
    
        public void paintComponent(Graphics g) {
            int red = (int) (Math.random() * 255);
            int green = (int) (Math.random() * 255);
            int blue = (int) (Math.random() * 255);
            Color randomColor = new Color(red, green, blue);
            g.setColor(randomColor);
            g.fillOval(70, 70, 100, 100);
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 7月31日
  • 已采纳回答 7月23日
  • 创建了问题 7月22日

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵