java 如何将包含 JPanel 的 JScrollPane 视口滚动到特定位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7883422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 21:49:21  来源:igfitidea点击:

How do I scroll JScrollPane viewport containing a JPanel to a specific location

javaswingjpaneljscrollpaneviewport

提问by Heheas

I am trying to create a large game board that only part of it will be visible in the viewport and would like to be able to move the viewport around using the arrow keys to see the whole board. Right now I have a JScrollPane that contains a JPanel that will have Images and text and other stuff, but those are irrelevant. Right now I have an ImageIcon at the same size as the JPanel as a background and I would like to have an option to move the viewport around by using keys, and not have any visible scrollbars. I have tried calling getViewPort().setViewPosition() followed by revalidate() on the JPanel and the JScrollPane. But it still does not work. My code is below

我正在尝试创建一个大型游戏板,在视口中只能看到它的一部分,并且希望能够使用箭头键移动视口以查看整个板。现在我有一个 JScrollPane,其中包含一个 JPanel,其中将包含图像、文本和其他内容,但这些无关紧要。现在我有一个与 JPanel 大小相同的 ImageIcon 作为背景,我希望可以选择使用键来移动视口,并且没有任何可见的滚动条。我试过在 JPanel 和 JScrollPane 上调用 getViewPort().setViewPosition() 然后是 revalidate() 。但它仍然不起作用。我的代码在下面

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import java.awt.*;
import java.awt.event.*;


public class Game extends JPanel {

    JPanel game = new JPanel();
    JScrollPane scrollPane = new JScrollPane(game);
    JLabel grass = new JLabel();

    private KeyListener keys = new KeyListener() {
        public void keyTyped(KeyEvent e) {

        }
        public void keyReleased(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_DOWN) {
                System.out.println("Down");
                scrollPane.getViewport()

            } else if (e.getKeyCode() == KeyEvent.VK_UP) {
                System.out.println("up");
            }
            revalidate();
        }
        public void keyPressed(KeyEvent e) {

        }
    };

public void createGame(int turns) {

        int gameWidth = 1800;
        int gameHeight = 1600;

        int viewPortWidth = 800;
        int viewPortHeight = 600;

        //setLayout(null);
        setSize(new Dimension(viewPortWidth, viewPortHeight));

        game.setLayout(null);
        game.setSize(new Dimension(gameWidth, gameHeight));

        //add the background image
        ImageIcon background = new ImageIcon("Grass.png");
        grass.setIcon(background);
        grass.setBounds(0,0, gameWidth, gameHeight);
        game.setBackground(Color.GREEN);
        game.add(grass);
        game.setBounds(0,0, gameWidth, gameHeight);

        //key listener
        game.addKeyListener(keys);

        //add(game);
        scrollPane.setPreferredSize(new Dimension(viewPortWidth, viewPortHeight));

        add(scrollPane);

    }


}

回答by trashgod

There's an example that uses scrollRectToVisible()hereand here. You should be able to adapt it to your usage.

有一个使用herehere的示例。您应该能够使其适应您的使用情况。scrollRectToVisible()

As an aside, consider using key bindings, shown here, hereand here.

顺便说一句,请考虑使用键绑定,显示在此处此处此处

回答by camickr

KeyEvents only go to components that have focus. By default a JPaneldoes not have focus so it does not receive KeyEvents. You can make a panel focusable by invoking the setFocusable()method on the panel.

KeyEvents 只去有焦点的组件。默认情况下 aJPanel没有焦点,因此它不会接收KeyEvents。您可以通过调用setFocusable()面板上的方法使面板成为焦点。

Swing was designed to be used with Key Bindings. They can be set up to work even if the component doesn't have focus.

Swing 旨在与Key Bindings一起使用。即使组件没有焦点,它们也可以设置为工作。

Using setViewPosition()should work fine.

使用setViewPosition()应该可以正常工作。

回答by Heheas

My issue was that I had the layout of the Panel being viewed as null and not as a BorderLayout, unfortunately this means that I have to have the scroll bars.

我的问题是我将 Panel 的布局视为 null 而不是 BorderLayout,不幸的是,这意味着我必须拥有滚动条。

I am currently finding a way to hide the scrollbars and keep the functionality

我目前正在寻找一种隐藏滚动条并保留功能的方法