Java 您可以为 JFrame 内的 JPanel 设置永久大小吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/316454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can you set a permanent size for a JPanel inside of a JFrame?
提问by Tomek
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
我当前的问题是我有一个带有 2x2 GridLayout 的 JFrame。在其中一个正方形内,我有一个用于显示网格的 JPanel。我正在与 java swing 库一起度过一天……看一看
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?
Java 会自动扩展每个 JLabel 以适应屏幕。我希望它只是那些蓝色方块(水)和黑色边框,而不是那个灰色空间。有没有一种方法可以永久设置该 JPanel 的大小,这样我就不必在获得确切尺寸之前将 JFrame 的大小更改一百万次,从而使灰色空间消失?
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)
我还想设置这些按钮的大小,使它们不会太大(BorderLayout
用于按钮和 TextField)
采纳答案by javamonkey79
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
. 将每个父 JPanel 的布局类型设置为GridBagLayout
. 为每个棋盘组件设置preferedSize,然后将它们添加到各自的容器中。默认情况下,GridBagLayout 应该将每个板放在父 JPanel 的中心。所以随着窗口大小的调整,JPanel的父区域会变大或变小,但里面的棋盘组件会保持不变的大小。
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.
或者,您可以在调整窗口大小时将蓝色方块缩放到正确的大小,方法是让每个棋盘方块都JPanel
带有BorderLayout
布局管理器,并将JLabel
(带有蓝色背景颜色)添加到其BorderLayout.CENTER
位置。
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);
Lastly, consider using a design tool for your Swing user interface. Netbeans has an excellent UIdesigner built into it. Download Netbeans here.
最后,考虑为您的 Swing 用户界面使用设计工具。Netbeans 内置了一个出色的 UI设计器。在此处下载 Netbeans 。
回答by javamonkey79
If you can setResizeable( false )
on the top level frame you can then set your layout manager to null and hard code each location and size via setBounds. This is how I would do it (contingent on resizing of course).
如果可以setResizeable( false )
在顶级框架上,则可以将布局管理器设置为 null 并通过 setBounds 对每个位置和大小进行硬编码。这就是我要做的(当然取决于调整大小)。
回答by javamonkey79
I have had success solving problems like these using TableLayout
which is a third party layout manager. You will need to download it and read the tutorial but the key would be to set the justification to CENTER when adding the JButtons to their positions in the layout.
我使用TableLayout
第三方布局管理器成功解决了此类问题。您需要下载它并阅读教程,但关键是在将 JButton 添加到布局中的位置时将对齐设置为 CENTER。
回答by Jay Sheridan
GridBagLayoutis what you really want to use. The GridLayout will force the same size for each component in the layout no matter what size constraints you put on them. GridBagLayout is a lot more powerful and a lot more complicated. Study up on the API pagefor it. Using GridBagLayout
, the components won't fill the whole grid space if you don't want them to and can even stay the size that you ask it to be. To keep a component's size from changing, I would set all three available size constraints:
GridBagLayout是您真正想要使用的。GridLayout 将强制布局中的每个组件具有相同的大小,无论您对它们施加什么大小限制。GridBagLayout 更强大,也更复杂。研究它的 API 页面。使用GridBagLayout
,如果您不希望组件填充整个网格空间,它们甚至可以保持您要求的大小。为了防止组件的大小发生变化,我将设置所有三个可用的大小限制:
water.setPreferredSize(new Dimension(20, 20));
water.setMinimumSize(new Dimension(20, 20));
water.setMaximumSize(new Dimension(20, 20));
For your buttons, I would definitely use an inner panel as Bryanmentions. You could use either a GridLayout like he suggests or a FlowLayout if you don't want all the buttons to be the same size. Add all your buttons to that inner panel instead of the main one.
对于您的按钮,我肯定会使用Bryan提到的内部面板。如果您不希望所有按钮的大小相同,您可以使用他建议的 GridLayout 或 FlowLayout。将所有按钮添加到该内部面板而不是主面板。