【问题标题】:How to define which button is pressed using MouseEvent如何使用 MouseEvent 定义按下哪个按钮
【发布时间】:2021-06-25 00:21:22
【问题描述】:

这个应用程序的想法是在 JPanel 上绘制一个形状。首先,您需要通过单击一个按钮来选择一个形状,然后单击面板上的某个位置以使用一个 MouseListener 来绘制它,但是我不知道如何实现在单击它后激活哪个按钮。我尝试使用 getSource(),但它似乎不起作用。代码如下:

public class Draw extends JFrame  {

    private JPanel contentPane;
    private Point startPoint = null, endPoint = null;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Draw frame = new Draw();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Draw() {
        getContentPane().setLayout(new BorderLayout(0, 0));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 800, 600);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(new BorderLayout(0, 0));
        
        final PnlDrawing pnldrawing = new PnlDrawing();
        contentPane.add(pnldrawing, BorderLayout.CENTER);
        
        JPanel panel = new JPanel();
        contentPane.add(panel, BorderLayout.SOUTH);
        
        JSeparator separator = new JSeparator();
        separator.setOrientation(SwingConstants.VERTICAL);
        panel.add(separator);
        
        JButton btnSelect = new JButton("Select");
        panel.add(btnSelect);
        
        JButton btnModify = new JButton("Modify");
        panel.add(btnModify);
        
        JButton btnRemove = new JButton("Remove");
        panel.add(btnRemove);
        
        JPanel panel_1 = new JPanel();
        contentPane.add(panel_1, BorderLayout.NORTH);
        
        final JButton btnAddPoint = new JButton("Add Point");
        panel_1.add(btnAddPoint);
        
        final JButton btnAddLine = new JButton("Add Line");
        panel_1.add(btnAddLine);
        
        final JButton btnAddCircle = new JButton("Add Circle");
        panel_1.add(btnAddCircle);
        
        final JButton btnAddDonut = new JButton("Add Donut");
        panel_1.add(btnAddDonut);
        
        final JButton btnAddRectangle = new JButton("Add Rectangle");
        panel_1.add(btnAddRectangle);
        
        pnldrawing.addMouseListener(new MouseAdapter() {
               
        @Override 
        public void mousePressed(MouseEvent e) {
            
                if(e.getSource().equals(btnAddPoint)) {
                    pnldrawing.shapes.add(new Point(e.getX(), e.getY()));
                    pnldrawing.repaint();
                }
                if(e.getSource().equals(btnAddLine)) {
                    if (startPoint == null)
                    {
                        startPoint = new Point(e.getX(),e.getY());
                        
                    }
                    else if(endPoint == null)
                    {
                        endPoint = new Point(e.getX(),e.getY());
                        
                    }
                    if (startPoint != null && endPoint != null) 
                    {
                        Line line = new Line(startPoint,endPoint);
                        pnldrawing.shapes.add(line);
                        pnldrawing.repaint();
                        startPoint = null;
                        endPoint = null;
                    }
                }
                if(e.getSource().equals(btnAddCircle)) {
                    DlgCircle drawCircle = new DlgCircle();
                    drawCircle.setVisible(true);
                    Point center = new Point(e.getX(),e.getY());
                    if (drawCircle.isOk)
                    {
                        pnldrawing.shapes.add(new Circle(center,Integer.parseInt(drawCircle.txtCircle.getText())));
                        pnldrawing.repaint();
                    }
                }
                if(e.getSource().equals(btnAddDonut)) {
                    DlgDonut drawDonut = new DlgDonut();
                    drawDonut.setVisible(true);
                    Point center = new Point(e.getX(),e.getY());
                    if (drawDonut.isOk)
                    {
                        pnldrawing.shapes.add(new Donut(center,Integer.parseInt(drawDonut.txtRadius.getText()),Integer.parseInt(drawDonut.txtInner.getText())));
                        pnldrawing.repaint();
                    }
                }
                if(e.getSource().equals(btnAddRectangle)) {
                    DlgRectangleSec drawRect = new DlgRectangleSec();
                    drawRect.setVisible(true);
                    Point upperLeft = new Point(e.getX(),e.getY());
                    if(drawRect.isOk) {
                        pnldrawing.shapes.add(new Rectangle(upperLeft,Integer.parseInt(drawRect.txtWidth.getText()),Integer.parseInt(drawRect.txtHeight.getText())));
                        pnldrawing.repaint();
                }
            }
          }
            
        });
        
    }

}

Here's the GUI

【问题讨论】:

    标签: java swing jpanel mouseevent jbutton


    【解决方案1】:

    首先,您可能想要更改您的绘画逻辑以使其更加用户友好。那就是你可以:

    1. 使用 mousePressed 事件保存鼠标点。

    2. 在 mouseDragged 事件中添加逻辑以在鼠标被拖动时绘制形状。

    3. 最后,在 mouseReleased 事件中,您可以使绘图形状永久化。

    查看Custom Painting Approaches 的工作示例。它只绘制矩形,但它应该给你一个起点。

    但我不知道如何实现激活哪个按钮

    使用JRadioButton 代替 JButton。该按钮将保持选中状态。

    然后你的 MouseListener 代码中的逻辑变成这样:

    if (addLineButton.isSelected())
        // do something
    else if (addRectangleButton.isSelected())
        //  do something
    

    【讨论】:

    【解决方案2】:

    我是否正确,您希望按下鼠标上的按钮?如果是这样,请查看此链接以指定特定单击完成的操作。然后你可以将一个字符串变量设置为点击了什么按钮,比如 String buttonclicked = “left click”。

    Right click mouse event

    【讨论】:

    • 不完全是。如果有帮助,我已经添加了我的 GUI 的图片。基本上我唯一的问题是我无法进入将我的绘图实现保存在 mousePressed 方法中的 if() 函数。我希望能够通过按下显示的按钮(例如添加点、添加线等)进入这些 if() 函数
    猜你喜欢
    • 2011-04-11
    • 1970-01-01
    • 2013-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多