如果我想要执行某个操作,如果按下某个按钮,我可以使用ActionListener。但现在如果我想通过按住鼠标按钮激活更多按钮。 我该如何实现呢?
由于
答案 0 :(得分:3)
向按钮ChangeListener
添加ButtonModel
,监控isPressed
状态的更改。
Timer
,这将在按钮时每秒添加大约40个新组件按下
public class TestPane extends JPanel {
private Timer timer = new Timer(25, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
add(new JButton("..."));
revalidate();
repaint();
}
});
public TestPane() {
JButton btn = new JButton("Help");
btn.getModel().addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
ButtonModel model = (ButtonModel) e.getSource();
if (model.isPressed()) {
timer.start();
} else {
timer.stop();
}
}
});
add(btn);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
谢谢,如果我按下一个按钮,这就有效。但是,如果我按下一个按钮并将鼠标按下鼠标按钮移动到另一个按钮,那么secound按钮不会执行任何操作
我们很清楚,我认为这是一种糟糕的用户体验,但那就是我
public class Test {
public static void main(String[] args) {
new Test();
}
private boolean pressed = false;
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 class TestPane extends JPanel {
public TestPane() {
add(makeButton("1"));
add(makeButton("2"));
}
protected JButton makeButton(String text) {
JButton btn = new JButton(text);
MouseAdapter ma = new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
pressed = true;
}
@Override
public void mouseReleased(MouseEvent e) {
pressed = false;
}
@Override
public void mouseEntered(MouseEvent e) {
if (pressed) {
JButton btn = (JButton) e.getComponent();
System.out.println("Entered " + btn.getText());
btn.doClick();
}
}
};
btn.addMouseListener(ma);
btn.addMouseMotionListener(ma);
return btn;
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
}
答案 1 :(得分:0)
@npinti现在我可以为我用鼠标悬停的每个按钮执行操作。但是,如果我尝试更改实现,以便仅执行操作,如果同时单击鼠标,则仅当按下按钮并再次按下相同按钮时,操作才有效。 `JButton b = new JButton();
如果(A == 1){
b.setBackground(Color.WHITE);
}其他{
b.setBackground(Color.BLACK);
}
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0){
matrix.activate(X,Y);
}
});
b.addMouseListener(new MouseListener() {
int pressed =0;
@Override
public void mouseReleased(MouseEvent e) {
pressed =0;
}
@Override
public void mousePressed(MouseEvent e) {
pressed =1;
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
if (e.getComponent().equals(b) && pressed ==1){
b.setBackground(Color.BLACK);
}
}
public void mouseClicked(MouseEvent e) {
pressed =1;
}
});`
答案 2 :(得分:0)
在您的按钮上捕获mouseEntered
,如果全局布尔值pressed
为真(当您按下鼠标按钮时将其设置为true而在释放时为false),则使用JButton.doClick()
触发操作模拟按钮上的点击事件。