简体   繁体   中英

Displaying Class Which Extends JScrollPane

I'm still relatively new to Swing and GUI programming in general, so I'm probably making some very simple mistake here, but:

I have a class called TilePanel which extends JScrollPane . The purpose of this class is to contain another custom class of mine, TileMap , which itself extends JPanel . The problem I have is that the TilePanel never displays.

This seems exceedingly weird to me, because, before creating the TilePanel class, I ran a test where I used a normal JScrollPane in its place, and added the TileMap to it. This displayed normally for me, and I was able to successfully scroll my TileMap . The code in my TilePanel class doesn't really change much about the JScrollPane yet, so I can see no reason why this should not be working.

Here is the code for the TilePanel class:

import javax.swing.JScrollPane;

public class TilePanel extends JScrollPane
{
    TileMap map;

    public TilePanel(int rows, int cols)
    {
        super(VERTICAL_SCROLLBAR_NEVER, HORIZONTAL_SCROLLBAR_NEVER);

        map = new TileMap(rows, cols, 9, 9);
        map.addBorder(Tile.EMPTY, 9);
        this.add(map);
    }
}

TileMap uses a GridLayout for its default layout, if that effects anything.

From my searches both on here, and elsewhere, I have not found anyone having a similar problem, unless I'm just really bad at searching. I found a few people who had problems with adding stuff to the JScrollPane itself, but not anyone who had a problem displaying a class which extended JScrollPane .

If more information is needed, I'll be happy to add it.

Don't extend JScrollPane, you are not adding any new functionality to the scrollpane. Just create the TileMap class and set it to the viewport of the scrollPane.

In the class where you create the TilePanel class you code should look something like:

//TilePanel panel= new TilePanel(); // not needed.
TileMap map = new TileMap(rows, cols, 9, 9);
map.addBorder(Tile.EMPTY, 9);
JScrollPane scrollPane = new JScrollPane( map );
frame.add( scrollPane );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM