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

拈花ヽ惹草 提交于 2020-01-14 10:23:31

问题


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

    }


}

回答1:


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.




回答2:


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

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

Using setViewPosition() should work fine.




回答3:


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.

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



来源:https://stackoverflow.com/questions/7883422/how-do-i-scroll-jscrollpane-viewport-containing-a-jpanel-to-a-specific-location

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!