[英]JFrame, JPanel, and Gridlayout Issues
As a quick overview, my project involves taking a .txt file and converting that into a 2d array, then drawing it into a JFrame. 快速概述,我的项目涉及获取.txt文件并将其转换为2d数组,然后将其绘制到JFrame中。 As I was testing, I used JButtons instead of a custom class that will be built later.
在测试期间,我使用了JButtons而不是稍后将要构建的自定义类。 I have ran into two bugs that I don't know how to solve.
我遇到了两个我不知道如何解决的错误。 For these pictures, this is all based on a Gridlayout that is 2 Rows and 16 Columes.
对于这些图片,这全部基于2行16列的网格布局。 When I add JButtons to the panel, it looks like this .
当我将JButtons添加到面板时,它看起来像这样 。
When I add them to the JFrame, it looks like this . 当我将它们添加到JFrame时,它看起来像这样 。
Here is my code for creating the JFrame and JPanel and rendering it: 这是用于创建JFrame和JPanel并呈现它的代码:
/**
* This method creates a JFrame, JPanel, and then renders
* all of the level in the JFrame
*/
public void render()
{
JFrame frame = new JFrame("<Insert Title>");
//Make it full screen for any computer monitor
frame.setSize(JFrame.MAXIMIZED_HORIZ, JFrame.MAXIMIZED_HORIZ);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setUndecorated(true);
frame.setVisible(true);
JPanel panel = new JPanel(grid);
frame.setLayout(grid);
frame.add(panel);
panel.getInputMap(IFW).put(KeyStroke.getKeyStroke("ESCAPE"), "quit");
panel.getActionMap().put("quit", quit);
//Draw!
for(int row = 0; row < drawStuff.length; row++)
{
for(int col = 0; col < drawStuff[row].length; col++)
{
//Either panel.add or frame.add here
panel.add(new JButton("Row :"+row+" Col: "+col));
}
}
panel.revalidate();
panel.repaint();
}
Anything that I am doing wrong here? 我在这里做错了什么吗?
Thanks! 谢谢!
Remove frame.setLayout(grid);
删除
frame.setLayout(grid);
- this will allow the panel
to occupy the entire content area of the frame, instead of been one row of one column -这将允许
panel
占据框架的整个内容区域,而不是占据一行的一列
frame.setSize(JFrame.MAXIMIZED_HORIZ, JFrame.MAXIMIZED_HORIZ)
really isn't doing what you think it is, not unless you want a frame which is 2x4 frame.setSize(JFrame.MAXIMIZED_HORIZ, JFrame.MAXIMIZED_HORIZ)
确实没有按照您的想法做,除非您想要一个2x4的框架
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.