如何使用触控板在 Java AWT ScrollPane 中进行水平鼠标滚轮滚动

发布于 2024-09-01 21:32:20 字数 1948 浏览 2 评论 0原文

与许多现代鼠标和触控板一样,我的笔记本电脑支持垂直和水平滚动。一旦你习惯了,它就会让人上瘾。我只是希望我的 Java 应用程序支持通过触控板/鼠标滚轮进行水平滚动,但在我搜索的所有地方似乎这在 Java 中是不可能的。

我真的希望有人告诉我我做错了,这个功能已经是请求的行为: https://bugs.java.com/bugdatabase/view_bug?bug_id=6440198

对于我正在开发的应用程序来说,无法完成这个简单的事情实际上是一个大问题。事实上,对于我能想到的任何应用程序!我在 Java 后端投入了一些时间,所以我真的很想为这个看似简单的事情找到一个解决方案。

问题是我可以做什么来实现这种行为?原始操作系统级事件是否通过 java 暴露给我,那么我需要从头开始编写它吗?

import java.awt.*;

public class ScrollExample extends Canvas {

    public void paint(Graphics g) {

        g.setColor(Color.green);
        g.fillOval(0,0,400, 400);

    }

    public static void main(String[] args) {

        ScrollExample b = new ScrollExample();
        Frame f = new Frame ("Scroll Example");

        ScrollPane scroller = new ScrollPane (ScrollPane.SCROLLBARS_ALWAYS);
        scroller.add(b,"Center");
        f.setPreferredSize(new Dimension(500,500));
        f.add ("Center",scroller);
        f.pack();
        f.show();

    }

}

Swing 示例适用于水平和垂直滚动

import java.awt.*;
import javax.swing.*;

public class ScrollExample extends JPanel { 

    public void paint(Graphics g) {

        super.paint(g);

        g.setColor(Color.green);
        g.fillOval(0,0,400, 400);

    }

    public static void main(String[] args) {

        JFrame f = new JFrame ("Scroll Example");
        ScrollExample p = new ScrollExample();
        p.setPreferredSize(new Dimension(1000, 1000));
        
        JScrollPane scroller = new JScrollPane(p,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        scroller.getHorizontalScrollBar().setUnitIncrement(10);
        scroller.getVerticalScrollBar().setUnitIncrement(10);

        f.setPreferredSize(new Dimension(500,500));
        f.add (scroller,BorderLayout.CENTER);
        f.pack();
        f.show();
    }
}

Like many modern mice and trackpads, my laptop supports vertical and horizontal scrolling. It's an addictive feature once you get used to it. I simply want my Java apps to support horizontal scrolling via the trackpad/mousewheel, but everywhere I search it seems that this is not possible in Java.

I really want someone to tell me that I'm somehow doing it wrong, this feature is already requested behaviour: https://bugs.java.com/bugdatabase/view_bug?bug_id=6440198

The inability to do this simple thing is actually a deal breaker for the app I'm working on. In fact, for any app I can envision! I've invested a bit of time in the Java backend, so I'd really like to find a solution for this seemingly simple thing.

Question is what could I do to implement this behaviour? Are raw OS level events even exposed to me by java, would I then need to write this from scratch?

import java.awt.*;

public class ScrollExample extends Canvas {

    public void paint(Graphics g) {

        g.setColor(Color.green);
        g.fillOval(0,0,400, 400);

    }

    public static void main(String[] args) {

        ScrollExample b = new ScrollExample();
        Frame f = new Frame ("Scroll Example");

        ScrollPane scroller = new ScrollPane (ScrollPane.SCROLLBARS_ALWAYS);
        scroller.add(b,"Center");
        f.setPreferredSize(new Dimension(500,500));
        f.add ("Center",scroller);
        f.pack();
        f.show();

    }

}

Swing example works with both horizontal and vertical scrolling

import java.awt.*;
import javax.swing.*;

public class ScrollExample extends JPanel { 

    public void paint(Graphics g) {

        super.paint(g);

        g.setColor(Color.green);
        g.fillOval(0,0,400, 400);

    }

    public static void main(String[] args) {

        JFrame f = new JFrame ("Scroll Example");
        ScrollExample p = new ScrollExample();
        p.setPreferredSize(new Dimension(1000, 1000));
        
        JScrollPane scroller = new JScrollPane(p,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        scroller.getHorizontalScrollBar().setUnitIncrement(10);
        scroller.getVerticalScrollBar().setUnitIncrement(10);

        f.setPreferredSize(new Dimension(500,500));
        f.add (scroller,BorderLayout.CENTER);
        f.pack();
        f.show();
    }
}

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

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

发布评论

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

评论(2

陌若浮生 2024-09-08 21:32:20

Swing 默认支持此功能,但当两个滚动条都可见时,垂直滚动优先于水平滚动。

我希望新的开发能够在 Swing 中完成,而不是在 AWT 中完成。

编辑:

您应该能够使用 鼠标滚轮控制器 自定义滚动速度。我从未尝试过水平滚动,请告诉我它是否有效。

This is supported by default in Swing although vertical scolling has priority over horizontal scrolling when both scrollbars are visible.

I would expect new development to be done in Swing not AWT.

Edit:

You should be able to use the Mouse Wheel Controller to customize the scroll speed. I've never tried it on horizontal scrolling, let me know if it works.

茶花眉 2024-09-08 21:32:20

我的 MacBookPro 触控板上的导航事件是开箱即用的。请注意,JScrollPane 上有一个方法可以启用滚轮事件:setWheelScrollingEnabled

The navigation events on my MacBookPro's trackpad are handled out of the box. Note there is a method on JScrollPane to enable wheel events: setWheelScrollingEnabled

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