Frage

Good time of the day, I'm trying to add scrolls to the GridBagLayout but definitely missing the correct way to do it.

The code:

public GUI(Map map)
{
    //declare image icons and try to read them
    ImageIcon missing = new ImageIcon();
    ImageIcon wall = new ImageIcon();
    ImageIcon floor = new ImageIcon();
    //set texture for missing cases
    try
    {
        Image tempImage = ImageIO.read(this.getClass().getResource("/resources/images/missing.png"));
        missing = new ImageIcon(tempImage.getScaledInstance(16, 16, Image.SCALE_DEFAULT));
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    try
    {
        Image tempImage = ImageIO.read(this.getClass().getResource("/resources/images/wall.png"));
        wall = new ImageIcon(tempImage.getScaledInstance(16, 16, Image.SCALE_DEFAULT));
        tempImage = ImageIO.read(this.getClass().getResource("/resources/images/floor.png"));
        floor = new ImageIcon(tempImage.getScaledInstance(16, 16, Image.SCALE_DEFAULT));
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    Container pane = getContentPane();
    pane.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    for (int i=0;i<map.getMapSize();i++)
    {
        for (int j=0;j<map.getMapSize();j++)
        {
            c.gridy=i;
            c.gridx=j;
            if (i==0 && j==0)
            {
                c.weightx=1;
                c.weighty=1;
            }
            else
            {
                c.weightx=0;
                c.weighty=0;
            }
            c.gridwidth=1;
            c.gridheight=1;
            c.fill = GridBagConstraints.BOTH;
            JLabel tile = new JLabel(missing);
            if (map.getElementAt(i, j)==0)
            {
                tile.setIcon(wall);
            }
            else
            {
                tile.setIcon(floor);
            }
            pane.add(tile, c);
        }
    }
    //c.gridx=map.getMapSize();
    JScrollPane thePane = new JScrollPane();
    pane.add(thePane, c);

    setTitle("Game GUI");
    setSize(640, 480);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
}

As far as I understand adding JScrollPane just after filling the pane is incorrect and of course it fails. The question is: how to add them correctly in this case? The aim overall I'm trying to achieve is to be able to scroll (move) the map in the game if it exceeds the game window size. Thanks in advance:)

War es hilfreich?

Lösung

I guess the issue is that you try to add the GridBagConstraints to the JScollPane instead of adding your JPanel....

JScrollPane thePane = new JScrollPane();
pane.add(thePane, c);

try this:

JPanel myMapPanel = new Jpanel();
myMapPanel.setLayout(new GridBagLayout());
// add all the stuff to myMapPanel here (do the looping stuff here)
JScrollPane thePane = new JScrollPane(myMapPanel);
pane.add(thePane, c);

take care of this:

new JScrollPane(myMapPanel);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top