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

Java Swing绘图程序功能互斥问题:如何禁用未选中绘图模式?

问题解决:Swing绘图工具互斥功能实现

你的核心问题是未在切换工具时重置另一个工具的状态,加上字符串比较的错误,导致两个工具的状态会同时为真。以下是具体修复方案:

关键改动点

  • 替换两个独立的状态变量freebtn、rectanglebtn为单个变量currentTool,用不同值标识当前选中的工具(0=无,1=自由绘制,2=矩形),从根本上保证互斥
  • 修复字符串比较错误:用equals()代替==比较按钮的actionCommand(Java中字符串==比较的是对象引用,不是内容)
  • 点击按钮时直接设置currentTool为对应值,自动覆盖之前的状态,实现互斥

修改后的完整代码

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MyFrame extends JFrame implements ActionListener, MouseListener, MouseMotionListener {
    JPanel top;
    JPanel bottom;
    JButton btn1, btn4;
    // 用单个变量管理当前工具:0=无,1=自由绘制,2=矩形
    int currentTool = 0;
    Graphics g;

    public MyFrame() {
        this.setTitle("car");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(700, 700);
        this.setLayout(null);
        this.setResizable(false);

        top = new JPanel();
        bottom = new JPanel();

        top.setBounds(0, 0, 700, 100);
        top.setBackground(Color.blue);

        bottom.setBounds(0, 100, 700, 600);
        bottom.setBackground(Color.white);

        btn1 = new JButton("free");
        btn4 = new JButton("rectangle");

        btn1.addActionListener(this);
        btn4.addActionListener(this);

        top.add(btn1);
        top.add(btn4);

        this.add(top);
        this.add(bottom);
        bottom.addMouseMotionListener(this);
        bottom.addMouseListener(this);
        this.setVisible(true);
        g = bottom.getGraphics();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // 用equals比较字符串内容
        if ("free".equals(e.getActionCommand())) {
            System.out.println("free button selected");
            currentTool = 1;
        } else if ("rectangle".equals(e.getActionCommand())) {
            System.out.println("rectangle button selected");
            currentTool = 2;
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {}

    @Override
    public void mousePressed(MouseEvent e) {
        // 只有当前工具是矩形时才执行绘制
        if (currentTool == 2) {
            g.setColor(Color.BLUE);
            g.drawRect(e.getX(), e.getY(), 100, 50);
        }
    }

    @Override
    public void mouseReleased(MouseEvent e) {}

    @Override
    public void mouseEntered(MouseEvent e) {}

    @Override
    public void mouseExited(MouseEvent e) {}

    @Override
    public void mouseDragged(MouseEvent e) {
        // 只有当前工具是自由绘制时才执行绘制
        if (currentTool == 1) {
            g.setColor(Color.BLUE);
            g.fillOval(e.getX(), e.getY(), 20, 20);
        }
    }

    @Override
    public void mouseMoved(MouseEvent e) {}
}

额外优化建议

  • 不要直接用getGraphics()获取绘图上下文,Swing推荐重写组件的paintComponent()方法来绘制,避免窗口刷新后图形消失的问题
  • 可以给选中的按钮添加视觉反馈(比如设置背景色),让用户更清楚当前选中的工具

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 18:15:48