你能为JFrame中的JPanel设置一个永久大小吗? [英] Can you set a permanent size for a JPanel inside of a JFrame?

查看:168
本文介绍了你能为JFrame中的JPanel设置一个永久大小吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前的问题是我有一个带有2x2 GridLayout的JFrame。在其中一个正方形内,我有一个显示网格的JPanel。我正在使用java swing库进行实地考察...看看

My current problem is that I have a JFrame with a 2x2 GridLayout. And inside one of the squares, I have a JPanel that is to display a grid. I am having a field day with the java swing library... take a look

图片

Java会自动扩展每个JLabel以适应屏幕。我希望它只是那些蓝色方块(水)和黑色边框而不是那个灰色空间。有没有办法我可以永久设置JPanel的大小,这样我就不必在获得精确尺寸之前将JFrame的大小改变一百万次,以便灰色空间消失?

Java is automatically expanding each JLabel to fit the screen. I want it to just be those blue squares (water) and the black border and not that gray space. Is there a way I can just set the size of that JPanel permanently so that I don't have to go through changing the size of the JFrame a million times before I get the exact dimension so that the gray space disappears?

我还想设置这些按钮的大小,因此它们不是那么大( BorderLayout 用于按钮和TextField)

I also would like to set the size of those buttons so they are not so huge (BorderLayout is being used for the buttons and TextField)

推荐答案

如果您希望两个棋盘保持相同的大小,那么您需要将每个棋盘都包含在他们自己的 JPanel 。将每个父JPanel设置为 GridBagLayout 的布局类型。为每个棋盘组件设置preferedSize,然后将它们添加到各自的容器中。默认情况下,GridBagLayout应该将每个板放在父JPanel的中心。因此,当窗口调整大小时,JPanel父区域将变大或变小,但里面的棋盘组件将保持相同的大小。

If you want the two checkerboards to stay the same size, then you'll need to have them each contained in their own JPanel. Set each of those parent JPanel's to have a layout type of GridBagLayout. Set the preferedSize for each checkerboard component and then add them to their respective containers. GridBagLayout should by default lay each board out in the center of the parent JPanel. So as the window is resized, the JPanel parent area will get larger or smaller, but the checkerboard components inside will remain the same size.

或者,你可以让你的蓝色通过使每个检查板正方形为 JPanel 并使用 BorderLayout 布局管理器调整窗口大小,将正方形缩放到正确的大小并将 JLabel (蓝色背景色)添加到 BorderLayout.CENTER 位置。

Alternatively, you could have your blue squares scale to the right size as the window is resized by having each checkboard square be a JPanel with a BorderLayout layout manager and adding the JLabel (with a blue background color) to its BorderLayout.CENTER location.

至于你的按钮,尝试这样的事情:

As for your buttons, try something like this:

JPanel theButtonPanel = new JPanel(new BorderLayout());
JButton button1 = new JButton("Fire");
JButton button2 = new JButton("Pass");
JButton button3 = new JButton("Forfiet");

JPanel innerButtonContainer = new JPanel(new Grid(1, 3, 8, 8));
innerButtonContainer.add(button1);
innerButtonContainer.add(button2);
innerButtonContainer.add(button3);

theButtonPanel.add(innterButtonContainer);

最后,考虑为Swing用户界面使用设计工具。 Netbeans内置了一个优秀的UI 设计器。下载Netbeans 此处

Lastly, consider using a design tool for your Swing user interface. Netbeans has an excellent UI designer built into it. Download Netbeans here.

这篇关于你能为JFrame中的JPanel设置一个永久大小吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆