如何将包含JPanel的JScrollPane视口滚动到特定位置 [英] How do I scroll JScrollPane viewport containing a JPanel to a specific location

查看:97
本文介绍了如何将包含JPanel的JScrollPane视口滚动到特定位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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);

    }


}

推荐答案

有一个使用此处此处.您应该能够使其适应您的用法.

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

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

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

这篇关于如何将包含JPanel的JScrollPane视口滚动到特定位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆