2

我想在这张图片上创建滚动窗格: 在此处输入图像描述

组件侧面带有箭头,并且没有可见的滚动条。只需要水平滚动。可以用 JScrollPane 完成吗?

4

1 回答 1

6

您可以通过使用滚动窗格并创建自己的按钮来使用滚动条的操作来创建自己的组件:

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

public class ScrollPaneSSCCE extends JPanel
{
    public ScrollPaneSSCCE()
    {
        setLayout( new BorderLayout() );

        JTextArea textArea = new JTextArea(1, 80);
        textArea.setText("Hopefully this will answer your question");
        JScrollPane scrollPane = new JScrollPane( textArea );
        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        add(scrollPane);

        JScrollBar horizontal = scrollPane.getHorizontalScrollBar();

        BasicArrowButton west = new BasicArrowButton(BasicArrowButton.WEST);
        west.setAction( new ActionMapAction("", horizontal, "negativeUnitIncrement") );
        add(west, BorderLayout.WEST);

        BasicArrowButton east = new BasicArrowButton(BasicArrowButton.EAST);
        east.setAction( new ActionMapAction("", horizontal, "positiveUnitIncrement") );
        add(east, BorderLayout.EAST);
    }

    private static void createAndShowUI()
    {
        JFrame frame = new JFrame("ScrollPaneSSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new ScrollPaneSSCCE(), BorderLayout.NORTH);
        frame.setSize(100, 100);
        frame.setLocationByPlatform( true );
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}

您还需要使用Action Map Action类。

于 2013-04-05T17:08:12.823 回答