作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在这里,我尝试在我的自定义面板中,在用户单击自定义呼吸机组件(它只是一个蓝色方 block ,该组件继承自 JButton)时击退一个球。
但问题是只有当用户点击呼吸机时才会管理该事件,而不是鼠标一直停留在呼吸机上时。
这是我的不同类(class)。提前致谢
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;
}
最佳答案
如果不阅读所有代码,问题可能如下所示:Mousepressed
仅在用户按下按钮时触发一次。您必须将该状态保存在一个变量中(例如 boolean pressed = true
),然后还对 mousereleased
事件作出 react ,这会将您的变量设置为 false。通过变量的状态,您可以知道鼠标按钮当前是按下还是释放。
关于Java Swing : managing the "MousePressed" event on a component until it is released,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7687858/
我是一名优秀的程序员,十分优秀!