You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java Swing绘图程序如何通过JButton点击切换画笔颜色

Java绘图程序按钮切换颜色问题修复

核心问题点

  • 颜色变量作用域错误:你定义的PColor是actionPerformed方法内的局部变量,方法执行结束后变量就会被销毁,无法保存选中的颜色状态
  • 界面结构逻辑错误:按钮定义在Controlcircle类中,但你main方法仅实例化了Paint_program窗口,没有实例化显示Controlcircle,运行程序根本看不到颜色按钮
  • 绘图逻辑未关联颜色:MoveableMousePanel的paintComponent方法中没有设置Graphics的绘图颜色,默认一直使用黑色,即使有颜色变量也不会生效
  • 重绘参数错误:mouseDragged中调用repaint(x,y,x,y)时,参数格式为repaint(x,y,width,height),你传入的宽高为0,不会触发正常重绘

修复后可运行完整代码

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Paint_program extends JFrame {
    // 画笔颜色变量,全局保存状态
    public Color currentColor = Color.BLACK;
    
    public Paint_program() {
        MoveableMousePanel p = new MoveableMousePanel();
        setLayout(new BorderLayout());
        
        // 底部按钮面板
        JPanel btnPanel = new JPanel();
        JButton redButton = new JButton("Red");
        JButton greenButton = new JButton("Green");
        JButton blueButton = new JButton("Blue");
        JButton eraserButton = new JButton("Eraser");
        JButton clearButton = new JButton("Clear");
        
        btnPanel.add(redButton);
        btnPanel.add(greenButton);
        btnPanel.add(blueButton);
        btnPanel.add(eraserButton);
        btnPanel.add(clearButton);
        add(btnPanel, BorderLayout.SOUTH);
        
        // 添加绘图面板
        add(p, BorderLayout.CENTER);
        
        // 按钮事件绑定
        ActionListener colorListener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Object source = e.getSource();
                if(source == redButton) {
                    currentColor = Color.RED;
                } else if(source == greenButton) {
                    currentColor = Color.GREEN;
                } else if(source == blueButton) {
                    currentColor = Color.BLUE;
                } else if(source == eraserButton) {
                    // 橡皮擦用背景色
                    currentColor = p.getBackground();
                } else if(source == clearButton) {
                    p.repaint();
                }
            }
        };
        
        redButton.addActionListener(colorListener);
        greenButton.addActionListener(colorListener);
        blueButton.addActionListener(colorListener);
        eraserButton.addActionListener(colorListener);
        clearButton.addActionListener(colorListener);
    }

    class MoveableMousePanel extends JPanel {
        private int preX = -1;
        private int preY = -1;

        public MoveableMousePanel() {
            // 鼠标拖拽绘画
            addMouseMotionListener(new MouseMotionListener() {
                public void mouseDragged(MouseEvent e) {
                    int x = e.getX();
                    int y = e.getY();
                    Graphics g = getGraphics();
                    g.setColor(currentColor);
                    // 从上一个点画到当前点,实现连续绘画
                    if(preX != -1 && preY != -1) {
                        g.drawLine(preX, preY, x, y);
                    }
                    preX = x;
                    preY = y;
                    g.dispose();
                }

                public void mouseMoved(MouseEvent e) {
                    // 鼠标移动时重置上一个点坐标
                    preX = -1;
                    preY = -1;
                }
            });
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
        }
     }

    public static void main(String[] args) {
        Paint_program frame = new Paint_program();
        frame.setTitle("Paint Program");
        frame.setSize(600, 500);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

关键修改说明

  • 把颜色变量currentColor设为主类的成员变量,全局保存用户选中的画笔颜色
  • 调整界面结构,把按钮直接放在主窗口底部,删除冗余的Controlcircle类,避免多窗口逻辑混乱
  • 绘图时先调用g.setColor(currentColor)设置画笔颜色,确保选中的颜色生效
  • 修改绘画逻辑为拖拽时连续画线条,体验更接近常规绘图工具
  • 完善所有按钮的事件逻辑,支持绿、蓝颜色切换、橡皮擦和清屏功能

内容的提问来源于stack exchange,提问作者Worthy251

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 10:15:11