Jlabels在Gridlayout中是看不见的
我做了一个jframe
,其中添加了jpanel
gridlayout
。网格由jlabel
组件,其中包含自定义号码。我没有例外。我的问题是在jframe
(mainframe
)上可见面板,但是您看不到添加到面板中的标签的数字。
有人能告诉我为什么我只看到面板,而不是标签?
首先,我创建GUI:
public Gui() {
mainFrame = new JFrame();
mainFrame.setResizable(false);
mainFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
mainFrame.setUndecorated(true);
mainFrame.setLayout(null);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
然后运行此方法:
public void startGame(Map map) {
//Deletes every component that could be on the JFrame
loescheAlles();
//In the test are 100 paths in the String
String[] paths = map.getPaths();
//width has the value 1000
int width = map.getBreite()*100;
//height has the value 1000
int height = map.getHoehe()*100;
gamePanel = new JPanel();
gamePanel.setBounds(0,0,width,height);
gamePanel.setLayout(new GridLayout(width,height));
JLabel[] label = new JLabel[paths.length];
for(int i = 0; i<paths.length; i++) {
//Text and border are not visible???
label[i] = new JLabel("" + i);
label[i].setBorder(new LineBorder(Color.BLACK));
gamePanel.add(label[i]);
}
//Because I can see the Border on the JFrame I know, that the JPanel is visible.
gamePanel.setBorder(new LineBorder(Color.BLACK));
mainFrame.add(gamePanel);
}
I made a JFrame
where I add a JPanel
with a GridLayout
. The grid consists of JLabel
components with a custom number in it. I have no Exception in the program. My problem is that the panel is visible on the JFrame
(mainFrame
), but you can't see the numbers of the labels that are added to the panel.
Can anybody tell me why I only see the panel, but not the labels?
First I create the GUI:
public Gui() {
mainFrame = new JFrame();
mainFrame.setResizable(false);
mainFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
mainFrame.setUndecorated(true);
mainFrame.setLayout(null);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
Then I run this method:
public void startGame(Map map) {
//Deletes every component that could be on the JFrame
loescheAlles();
//In the test are 100 paths in the String
String[] paths = map.getPaths();
//width has the value 1000
int width = map.getBreite()*100;
//height has the value 1000
int height = map.getHoehe()*100;
gamePanel = new JPanel();
gamePanel.setBounds(0,0,width,height);
gamePanel.setLayout(new GridLayout(width,height));
JLabel[] label = new JLabel[paths.length];
for(int i = 0; i<paths.length; i++) {
//Text and border are not visible???
label[i] = new JLabel("" + i);
label[i].setBorder(new LineBorder(Color.BLACK));
gamePanel.add(label[i]);
}
//Because I can see the Border on the JFrame I know, that the JPanel is visible.
gamePanel.setBorder(new LineBorder(Color.BLACK));
mainFrame.add(gamePanel);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论