gpt4 book ai didi

java - 当鼠标已经在按钮上时,鼠标监听器如何在一定时间后激活?

转载 作者:行者123 更新时间:2023-11-30 07:51:25 25 4
gpt4 key购买 nike

我试图在计时器结束后在 react 按钮上显示一个边框。除非我将鼠标从触发按钮上移开然后再移回到它上面,否则我似乎不会发生这种情况。有没有一种方法可以激活鼠标监听器,而无需将鼠标移开然后在计时器后返回触发按钮?请不要说:计时器结束时设置边框,因为那不是我要找的。另外,请随时指出我的代码的其他错误或坏习惯。我是 Java 编码的新手。

import java.util.Timer;
import java.util.TimerTask;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class ButtonTester{

public static final Border PANEL_BORDER = new LineBorder(Color.red, 12);
public static JPanel panel;
public static JButton trigger;
public static JButton react;
public static JLabel msg;

public static void main(String [] args){
JFrame frame = new JFrame();
frame.setSize(new Dimension(500,200));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

panel = new JPanel();
panel.setBorder(PANEL_BORDER);
frame.getContentPane().add(panel);

JButton trigger = new JButton("Trigger");
JButton react = new JButton("React");
JLabel msg = new JLabel();
react.setPreferredSize(new Dimension(200, 60));
trigger.setPreferredSize(new Dimension(200, 60));

panel.add(trigger);
panel.add(react);
panel.add(msg);
panel.setVisible(true);
frame.setVisible(true);

MouseListener mL = new MouseAdapter(){
@Override public void mouseEntered(MouseEvent evt) {
react.setBorder(PANEL_BORDER);
}
@Override public void mouseExited(MouseEvent evt) {
react.setBorder(javax.swing.BorderFactory.createEmptyBorder());
}
};
countDown(msg, trigger, mL);
}

public static void countDown(JLabel msg, JButton trigger, MouseListener mL){
Timer timer = new Timer();
TimerTask task = new TimerTask(){
short seconds = 4;
public void run(){
if(seconds == 0){
timer.cancel();
trigger.addMouseListener(mL);
return;
}
seconds--;
msg.setText("Attempting to add listener in : "+seconds);
}
};
timer.scheduleAtFixedRate(task, 1000, 1000);
}

}

最佳答案

好的,这个例子设置了两个状态变量,一个判断鼠标是否进入或退出按钮,一个判断定时器是否完成。

如果这两个条件都为真,则设置边框。

这意味着当计时器用完时,如果鼠标不在 trigger 按钮上,react 按钮的边框将不会改变,但是如果用户移回按钮,它将被更改。它也会在鼠标悬停在 trigger 按钮上并且计时器用完

时发生变化
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;

public class Test {

public static void main(String[] args) {
new Test();
}

public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public static class TestPane extends JPanel {

public static final Border PANEL_BORDER = new LineBorder(Color.red, 12);

private boolean mouseInTheHouse = false;
private boolean timedOut = false;

private JButton react;
private JButton trigger;

public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.ipadx = 200;
gbc.ipady = 60;
gbc.gridwidth = GridBagConstraints.REMAINDER;

react = new JButton("React");
trigger = new JButton("Trigger");

add(react, gbc);
add(trigger, gbc);

trigger.addMouseListener(new MouseAdapter() {

@Override
public void mouseEntered(MouseEvent e) {
mouseInTheHouse = true;
stateChanged();
}

@Override
public void mouseExited(MouseEvent e) {
mouseInTheHouse = false;
}

});
Timer timer = new Timer(4000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
timedOut = true;
System.out.println("!!");
stateChanged();
}
});
timer.start();
}

protected void stateChanged() {
if (mouseInTheHouse && timedOut) {
react.setBorder(PANEL_BORDER);
}
}

}

}

请注意,我没有为鼠标离开 trigger 按钮时发生的情况设置条件,但我假设您会重置边框。

I see. I have an additional question. What if I had 10 trigger buttons (top of the panel) and 10 react button (bottom of the panel)? The condition is: If I have my mouse over one of the trigger button, then the corresponding react button of the same position plus the react button to the right side of that react button will have borders. How do I detect that without looping through my button List and detecting mouseInHouse?

基本上,将想法提炼到最常见的水平。您有两个按钮、一个 TimerMouseListener 和两个状态变量。将它们包装到一个通用类中,然后您可以重复使用。

public class ButtonStateManager {

private boolean mouseInTheHouse = false;
private boolean timedOut = false;

private JButton trigger;
private JButton react;

private Timer timer;

public ButtonStateManager(JButton trigger, JButton react, int timeOut) {
this.trigger = trigger;
this.react = react;

trigger.addMouseListener(new MouseAdapter() {

@Override
public void mouseEntered(MouseEvent e) {
mouseInTheHouse = true;
stateChanged();
}

@Override
public void mouseExited(MouseEvent e) {
mouseInTheHouse = false;
}

});

Timer timer = new Timer(timeOut, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
timedOut = true;
stateChanged();
}
});
}

protected void stateChanged() {
if (mouseInTheHouse && timedOut) {
react.setBorder(TestPane.PANEL_BORDER);
}
}

}

现在,这假设两个按钮之间存在关系。

关于java - 当鼠标已经在按钮上时,鼠标监听器如何在一定时间后激活?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47120264/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com