[英]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: 我对Swing和GUI编程一般都比较新,所以我可能在这里犯了一些非常简单的错误,但是:
I have a class called TilePanel
which extends JScrollPane
. 我有一个名为
TilePanel
的类,它扩展了JScrollPane
。 The purpose of this class is to contain another custom class of mine, TileMap
, which itself extends JPanel
. 这个类的目的是包含我的另一个自定义类
TileMap
,它本身扩展了JPanel
。 The problem I have is that the TilePanel
never displays. 我
TilePanel
的问题是TilePanel
永远不会显示。
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. 这对我来说似乎非常奇怪,因为在创建
TilePanel
类之前,我运行了一个测试,我在其中使用了普通的JScrollPane
,并添加了TileMap
。 This displayed normally for me, and I was able to successfully scroll my TileMap
. 这通常对我来说,我能够成功滚动我的
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. 我的
TilePanel
类中的代码对JScrollPane
并没有太大变化,所以我没有理由认为它不应该起作用。
Here is the code for the TilePanel
class: 以下是
TilePanel
类的代码:
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. TileMap
使用GridLayout
作为其默认布局,如果它影响任何东西。
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
. 我发现有些人在向
JScrollPane
本身添加内容时遇到了问题,但是没有人在显示扩展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. 不要扩展JScrollPane,也不要向scrollpane添加任何新功能。 Just create the TileMap class and set it to the viewport of the scrollPane.
只需创建TileMap类并将其设置为scrollPane的视口即可。
In the class where you create the TilePanel class you code should look something like: 在您创建TilePanel类的类中,您的代码应该类似于:
//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 );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.