Java Swing:管理“MousePressed”组件上的事件直到它被释放

发布于 2024-12-08 10:44:52 字数 5014 浏览 0 评论 0原文

在这里,我尝试在自定义面板中,当用户单击自定义呼吸机组件(它只是一个蓝色方块,该组件继承自 JButton)时排斥球。

但问题是,仅当用户单击呼吸机时才管理该事件,而不是一直将鼠标放在呼吸机上。

这是我的不同课程。提前致谢

PhysicsPanel.java

package com.gmail.loloof64.java_swing.physics_panels;

import com.gmail.loloof64.java_swing.physics_panels.gui.MainFrame;

public class PhysicsPanels {

    public static void main(String[] args) {
        new MainFrame().setVisible(true);
    }

}

MainFrame.java

package com.gmail.loloof64.java_swing.physics_panels.gui;

import javax.swing.JFrame;
import javax.swing.JTabbedPane;

import com.gmail.loloof64.java_swing.physics_panels.gui.the_panels.VentilatorPanel;

public class MainFrame extends JFrame {

    public MainFrame(){
        setIntrinsicsProperties();
        addPanels();
    }

    private void setIntrinsicsProperties() {
        setSize(400, 300);
        setTitle("Physics panels application");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }

    private void addPanels() {
        JTabbedPane mainContainer = new JTabbedPane();
        add(mainContainer);
        mainContainer.addTab("Ventilator", new VentilatorPanel());
        /*
         * This tab is not usefull for the question =>
         * mainContainer.addTab("Rotating plate", new RotatingPlatePanel());
         */
    }

    private static final long serialVersionUID = 1L;

}

VentilatorPanel.java

package com.gmail.loloof64.java_swing.physics_panels.gui.the_panels;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;

import javax.swing.JPanel;

import com.gmail.loloof64.java_swing.physics_panels.custom_components.BallComponent;
import com.gmail.loloof64.java_swing.physics_panels.custom_components.VentilatorComponent;

public class VentilatorPanel extends JPanel {

    public VentilatorPanel(){
        setBackground(Color.WHITE);
        setLayout(null);

        ventilatorObject.setSimpleTaskOnMousedPressed(new Runnable() {

            @Override
            public void run() {
                repulseBall();
            }

        });
        add(ventilatorObject);
        add(ballObject);
        ventilatorObject.setLocation(100, 80);
        ballObject.setLocation(160, 90);
    }

    @Override
    public void paint(Graphics graphics) {
        super.paint(graphics);
    }

    private void repulseBall(){
        Point oldBallLocation = ballObject.getLocation();
        ballObject.setLocation(oldBallLocation.x + 5, oldBallLocation.y);
        repaint();
    }

    private VentilatorComponent ventilatorObject = new VentilatorComponent(60,60);
    private BallComponent ballObject = new BallComponent(40);
    private static final long serialVersionUID = 1L;

}

VentilatorComponent.java

package com.gmail.loloof64.java_swing.physics_panels.custom_components;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;

public class VentilatorComponent extends JButton {

    public VentilatorComponent(Dimension size){
        super();
        enableInputMethods(true);
        addMouseListener(new MouseAdapter() {

            @Override
                public void mousePressed(MouseEvent event) {
                    doReactToClick();
                }
        });
        setSize(size);
    }

    public VentilatorComponent(int width, int height){
        this(new Dimension(width, height));
    }

    /**
     * Sets the task that will be executed on mouse pressing.
     * Be carefull !!! Must be a very simple task (no looping inside)
     * @param r - Runnable
     */
    public void setSimpleTaskOnMousePressed(Runnable task){
        taskOnClick = task;
    }

    protected void doReactToClick() {
        new Thread(taskOnClick).run();
    }

    @Override
    protected void paintComponent(Graphics graphics) {
        super.paintComponent(graphics);
        graphics.setColor(Color.BLUE);
        graphics.fillRect(0, 0, getWidth(), getHeight());
    }

    private Runnable taskOnClick;
    private static final long serialVersionUID = 1L;

}

BallComponent.java

package com.gmail.loloof64.java_swing.physics_panels.custom_components;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JComponent;

public class BallComponent extends JComponent {

    public BallComponent(int diameter){
        setSize(new Dimension(diameter, diameter));
    }   

    @Override
    public void paintComponent(Graphics graphics) {
        graphics.setColor(Color.RED);
        graphics.fillOval(0,0, getWidth(), getHeight());
    }

    private static final long serialVersionUID = 1L;

}

here I try, in my custom panel, to repulse a ball while a user click on a custom ventilator component (it is just a blue square, the component is inherited from JButton).

But the problem is that the event is only managed when the user click the ventilator, and not all the time the mouse is down on the ventilator.

Here my different classes. Thank in advance

PhysicsPanel.java

package com.gmail.loloof64.java_swing.physics_panels;

import com.gmail.loloof64.java_swing.physics_panels.gui.MainFrame;

public class PhysicsPanels {

    public static void main(String[] args) {
        new MainFrame().setVisible(true);
    }

}

MainFrame.java

package com.gmail.loloof64.java_swing.physics_panels.gui;

import javax.swing.JFrame;
import javax.swing.JTabbedPane;

import com.gmail.loloof64.java_swing.physics_panels.gui.the_panels.VentilatorPanel;

public class MainFrame extends JFrame {

    public MainFrame(){
        setIntrinsicsProperties();
        addPanels();
    }

    private void setIntrinsicsProperties() {
        setSize(400, 300);
        setTitle("Physics panels application");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }

    private void addPanels() {
        JTabbedPane mainContainer = new JTabbedPane();
        add(mainContainer);
        mainContainer.addTab("Ventilator", new VentilatorPanel());
        /*
         * This tab is not usefull for the question =>
         * mainContainer.addTab("Rotating plate", new RotatingPlatePanel());
         */
    }

    private static final long serialVersionUID = 1L;

}

VentilatorPanel.java

package com.gmail.loloof64.java_swing.physics_panels.gui.the_panels;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;

import javax.swing.JPanel;

import com.gmail.loloof64.java_swing.physics_panels.custom_components.BallComponent;
import com.gmail.loloof64.java_swing.physics_panels.custom_components.VentilatorComponent;

public class VentilatorPanel extends JPanel {

    public VentilatorPanel(){
        setBackground(Color.WHITE);
        setLayout(null);

        ventilatorObject.setSimpleTaskOnMousedPressed(new Runnable() {

            @Override
            public void run() {
                repulseBall();
            }

        });
        add(ventilatorObject);
        add(ballObject);
        ventilatorObject.setLocation(100, 80);
        ballObject.setLocation(160, 90);
    }

    @Override
    public void paint(Graphics graphics) {
        super.paint(graphics);
    }

    private void repulseBall(){
        Point oldBallLocation = ballObject.getLocation();
        ballObject.setLocation(oldBallLocation.x + 5, oldBallLocation.y);
        repaint();
    }

    private VentilatorComponent ventilatorObject = new VentilatorComponent(60,60);
    private BallComponent ballObject = new BallComponent(40);
    private static final long serialVersionUID = 1L;

}

VentilatorComponent.java

package com.gmail.loloof64.java_swing.physics_panels.custom_components;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;

public class VentilatorComponent extends JButton {

    public VentilatorComponent(Dimension size){
        super();
        enableInputMethods(true);
        addMouseListener(new MouseAdapter() {

            @Override
                public void mousePressed(MouseEvent event) {
                    doReactToClick();
                }
        });
        setSize(size);
    }

    public VentilatorComponent(int width, int height){
        this(new Dimension(width, height));
    }

    /**
     * Sets the task that will be executed on mouse pressing.
     * Be carefull !!! Must be a very simple task (no looping inside)
     * @param r - Runnable
     */
    public void setSimpleTaskOnMousePressed(Runnable task){
        taskOnClick = task;
    }

    protected void doReactToClick() {
        new Thread(taskOnClick).run();
    }

    @Override
    protected void paintComponent(Graphics graphics) {
        super.paintComponent(graphics);
        graphics.setColor(Color.BLUE);
        graphics.fillRect(0, 0, getWidth(), getHeight());
    }

    private Runnable taskOnClick;
    private static final long serialVersionUID = 1L;

}

BallComponent.java

package com.gmail.loloof64.java_swing.physics_panels.custom_components;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JComponent;

public class BallComponent extends JComponent {

    public BallComponent(int diameter){
        setSize(new Dimension(diameter, diameter));
    }   

    @Override
    public void paintComponent(Graphics graphics) {
        graphics.setColor(Color.RED);
        graphics.fillOval(0,0, getWidth(), getHeight());
    }

    private static final long serialVersionUID = 1L;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苏别ゝ 2024-12-15 10:44:52

如果不阅读所有代码,问题可能如下:当用户按下按钮时,Mousepressed 仅触发一次。您必须将该状态保存在变量中(例如 boolean Pressed = true),然后对 mousereleased 事件做出反应,从而将变量设置为 false。通过变量的状态,您可以知道鼠标按钮当前是按下还是释放。

Without reading all of your code, the problem might be as follows: Mousepressed is triggered only once, when the user presses down the button. You have to save that state in a variable (e.g. boolean pressed = true), and then react also to mousereleased events, which set your variable to false. By the state of your variable you know then whether the mouse button is currently pressed or released.

浅浅 2024-12-15 10:44:52

要获取鼠标处于按下状态时生成的事件,您需要使用 MouseMotionListener 并处理 mouseDragged() 事件。

To get events that are generated when the mouse is in a pressed state you need to use a MouseMotionListener and handle the mouseDragged() event.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文