gpt4 book ai didi

java - 单击时停止 jslider 移动

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:52:19 25 4
gpt4 key购买 nike

是否可以阻止 jslider 在单击时移动或更改值?我希望我的 jslider 仅在拖动时移动。当我点击它时,我根本不希望它做任何事情。有人可以帮我解决这个问题吗?

最佳答案

还有一些其他的例子:

  • slider1:覆盖 TrackListener#shouldScroll(...) 方法以始终返回 false
  • slider2:使用 JLayer 将鼠标左键按下 MouseEvent 更改为右键。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.plaf.*;
import javax.swing.plaf.metal.MetalSliderUI;

public class MainPanel {
public JComponent makeUI() {
JSlider slider0 = new JSlider();

JSlider slider1 = new JSlider() {
@Override public void updateUI() {
super.updateUI();
setUI(new MetalSliderUI() {
@Override protected TrackListener createTrackListener(JSlider slider) {
return new TrackListener() {
@Override public boolean shouldScroll(int direction) {
return false;
}
};
}
});
}
};

//http://stackoverflow.com/questions/9736237/how-to-disable-position-change-of-jslider-on-mouse-right-click
UIManager.put("Slider.onlyLeftMouseButtonDrag", false);
JSlider slider2 = new JSlider();

Box box = Box.createVerticalBox();
box.setBorder(BorderFactory.createEmptyBorder(20, 0, 20, 20));
box.add(makeTitledPanel("Default", slider0));
box.add(Box.createVerticalStrut(20));
box.add(makeTitledPanel(
"Override TrackListener#shouldScroll(...): false", slider1));
box.add(Box.createVerticalStrut(20));
box.add(makeTitledPanel(
"JLayer + Slider.onlyLeftMouseButtonDrag: false",
new JLayer<JSlider>(slider2, new DisableLeftPressedLayerUI())));
box.add(Box.createVerticalGlue());

return box;
}
private static JComponent makeTitledPanel(String title, JComponent c) {
JPanel p = new JPanel(new BorderLayout());
p.setBorder(BorderFactory.createTitledBorder(title));
p.add(c, BorderLayout.NORTH);
return p;
}
public static void main(String... args) {
EventQueue.invokeLater(() -> {
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new MainPanel().makeUI());
f.setSize(320, 240);
f.setLocationRelativeTo(null);
f.setVisible(true);
});
}
}

class DisableLeftPressedLayerUI extends LayerUI<JSlider> {
@Override public void installUI(JComponent c) {
super.installUI(c);
if (c instanceof JLayer) {
((JLayer) c).setLayerEventMask(AWTEvent.MOUSE_EVENT_MASK);
}
}
@Override public void uninstallUI(JComponent c) {
if (c instanceof JLayer) {
((JLayer) c).setLayerEventMask(0);
}
super.uninstallUI(c);
}
@Override protected void processMouseEvent(MouseEvent e, JLayer<? extends JSlider> l) {
if (e.getID() == MouseEvent.MOUSE_PRESSED && SwingUtilities.isLeftMouseButton(e)) {
JSlider slider = (JSlider) e.getComponent();
MouseEvent me = new MouseEvent(
slider,
e.getID(), e.getWhen(),
InputEvent.BUTTON3_DOWN_MASK, //e.getModifiers(),
e.getX(), e.getY(),
e.getXOnScreen(), e.getYOnScreen(),
e.getClickCount(),
e.isPopupTrigger(),
MouseEvent.BUTTON3); //e.getButton());
e.consume();
slider.dispatchEvent(me);
}
}
}

关于java - 单击时停止 jslider 移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39904127/

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