Java 网格布局和 JPanel
我有一个问题。
我有以下程序,但我在做一件事时遇到了麻烦。我创建了一个网格布局,并使用 JPanel 在网格中输入按钮和文本,但如果我的文本很长,所有内容似乎都会改变大小。有人可以帮助我如何防止这种情况发生吗?
这是我的代码:
package JFrameTester;
import java.awt.*;
import javax.swing.*;
public class JFrameTester {
public JPanel createContentPane (){
JPanel panel = new JPanel();
JButton button1,button2,button3,button4;
JPanel mainPanel = new JPanel(new GridLayout(2, 0, 40, 10));
//JPanel red = createSquareJPanel(Color.red, 50);
button1 = new JButton ("button1");
button2 = new JButton ("button2");
button3 = new JButton ("button3");
button4 = new JButton ("button4");
Label one = new Label("Rohihtjthjhtjghjghmgfjgjghjghj");
//add(button1);
mainPanel.add(button1);
mainPanel.add(one);
mainPanel.add(button2);
mainPanel.add(button3);
mainPanel.add(button4);
panel.add(mainPanel);
panel.setOpaque(true);
return panel;
}
public static void main(String[] args) {
JFrame frame = new JFrame("GridLayout");
JFrameTester Display = new JFrameTester();
frame.setContentPane(Display.createContentPane());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这就是布局管理器所做的事情;他们为每个组件分配足够的空间。您可以尝试将 HTML 嵌入到
JLabel
实例中以使其换行。否则,你就只能忍受它。还需要注意的是,
JPanel
实例默认情况下是不透明的。That's what layout managers do; they allocate enough space for every component. You could try embedding HTML into the
JLabel
instance in order for it to wrap. Otherwise, you're just going to have to live with it.It's also important to note that a
JPanel
instance is opaque by default.嗯……首先要做的事情。您正在声明一个 2 行、0 列的 GridLayout。这正是您想要的吗?我想不是。
下一部分是添加内容的地方。网格通常保持动态大小。它将把单元格缩小到尽可能小的值。但是,当某些内容变大(大的
jPanels
文本)时,它会扩展单元格。根据单元格的内容,它们将保持静态或尝试填充所有可用空间。
如果我正确阅读您的问题,您需要做的就是使用
JComponents
请参阅此处的 JavaDoc 链接。您可能需要对每个按钮使用setMaximumSize(new Dimension(width,height));
。有道理吗?
附带说明一下。如果您想更好地控制布局,请考虑使用
GrabBagLayout
。更多的工作,但更多的控制。Well... first things first. You're declaring a
GridLayout
of 2 rows, 0 columns. Is this exactly what you want? I am thinking not.The next part is where you're adding content. The grid is typically held at a dynamic size. It will shrink the cells to the smallest possible value they can. But when some contents are getting bigger (large
jPanels
of text) it will expand the cell.Depending on the contents of the cell they'll either remain static or attempt to fill in all available space.
If I'm reading your question properly, what you'll want to do is set a static size for your JButtons by using the functions for
JComponents
See JavaDoc link here. You'll probably want to usesetMaximumSize(new Dimension(width,height));
for each button.Makes sense?
On a side note. If you want more control over your layout look towards using a
GrabBagLayout
. More work, but more control.这是一篇较旧的帖子,但我在搜索同一问题时偶然发现了这一点。使用frame.pack() 可移动所有内容以适应给定的面板尺寸。因此,如果您增加一件物品的尺寸,其他物品就会缩小并移动以适应面板。
来源:http://www.macs.hw.ac。 uk/guidebook/?name=JFrame&page=2
This is an older post but i just stumbled across this, while searching the same issue. The use of frame.pack() is used to move everything around to fit in to the given panel size. So if you increase the size of one thing, other things shrink and move around to fit in the panel.
source: http://www.macs.hw.ac.uk/guidebook/?name=JFrame&page=2