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

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 Arduino,利用modbus的RS485协议,进行对外置的温湿度传感器进行数据读取
  • ¥15 vhdl+MODELSIM
  • ¥20 simulink中怎么使用solve函数?
  • ¥30 dspbuilder中使用signalcompiler时报错Error during compilation: Fitter failed,求解决办法
  • ¥15 gwas 分析-数据质控之过滤稀有突变中出现的问题
  • ¥15 没有注册类 (异常来自 HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
  • ¥15 知识蒸馏实战博客问题
  • ¥15 用PLC设计纸袋糊底机送料系统
  • ¥15 simulink仿真中dtc控制永磁同步电机如何控制开关频率
  • ¥15 用C语言输入方程怎么