Java 使用 JPanels 设置布局
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19697832/
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
Set layout with JPanels
提问by user2307421
I haven't really worked with Swing at all in Java. I'm experimenting with it. I want to make a set layout that the size can't be changed. I've seen alot of things suggesting to use Layout managers
to add multiple JPanels
into a JFrame
.
我根本没有在 Java 中真正使用过 Swing。我正在试验它。我想做一个无法更改大小的设置布局。我已经看到很多建议使用Layout managers
将多个添加JPanels
到JFrame
.
However, all the tutorials I've seen involving layout managers say it allows for the user to resize the screen. The layout I want has a rectangle going along the left hand side, a thin rectangle going along the bottom, and a third rectangle taking up the rest of the space. I attempting it using an Absolute layout but it just doesn't want to work for me.
但是,我见过的所有涉及布局管理器的教程都说它允许用户调整屏幕大小。我想要的布局有一个沿着左侧的矩形,一个沿着底部的细矩形,第三个矩形占据其余空间。我尝试使用绝对布局,但它只是不想为我工作。
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Test extends JFrame {
public Test() {
JPanel rect1 = new JPanel();
rect1.setBounds(101, 650, 900, 50);
rect1.setBackground(Color.RED);
getContentPane().add(rect1);
JPanel rect2 = new JPanel();
rect2.setBounds(0, 650, 100, 1000);
rect2.setBackground(Color.BLUE);
getContentPane().add(rect2);
JPanel rect3 = new JPanel();
rect3.setBounds(101, 700, 900, 950);
rect3.setBackground(Color.GREEN);
getContentPane().add(rect3);
setTitle("TEST");
setSize(1000, 700);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Test ex = new Test();
ex.setVisible(true);
}
});
}
}
Can someone help me properly make three Jpanels in a Jframe in this layout (all with different colors)?
有人可以帮助我在此布局中的 Jframe 中正确制作三个 Jpanel(均具有不同颜色)吗?
回答by Dark Knight
You can use setResizable(). Please refer to below code fragment.
您可以使用 setResizable()。请参考下面的代码片段。
setResizable(false); // this will not allow resizing
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
回答by Sage
AbsoluteLayout
is no go. Don't do that. You are probably looking for BorderLayout
. Check the tutorial: How to Use BorderLayoutfor details. If you don't want your JFrame
to be able to re-size use frame.setResizable(false);
on it.
AbsoluteLayout
不行。不要那样做。您可能正在寻找BorderLayout
. 查看教程:如何使用 BorderLayout了解详细信息。如果你不希望你JFrame
能够重新调整大小使用frame.setResizable(false);
它。
回答by MadProgrammer
You might be able to achieve the same thing using a BorderLayout
as the bases or even a GridBagLayout
.
您可以使用 aBorderLayout
作为基础甚至 a来实现相同的目标GridBagLayout
。
The main piece you are missing is the fact that layout managers use (or can use depending on the layout manager) the component's preferred/minimum/maximum size
您缺少的主要部分是布局管理器使用(或可以根据布局管理器使用)组件的首选/最小/最大尺寸
Basically, what you would do is define a custom component (extending from something like JPanel
) and override it's getPreferredSize
method and return the required value you need. Depending on the layout manager, you may also need to override the getMaximumSize
and getMinimumSize
methods as well.
基本上,你要做的是定义一个自定义组件(从类似的东西扩展JPanel
)并覆盖它的getPreferredSize
方法并返回你需要的所需值。根据布局管理器,您可能还需要覆盖getMaximumSize
和getMinimumSize
方法。
For example...
例如...
Basically, this shows the "default" size and what happens when the screen is resized...
基本上,这显示了“默认”大小以及调整屏幕大小时会发生什么......
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class FixedSizeLayout {
public static void main(String[] args) {
new FixedSizeLayout();
}
public FixedSizeLayout() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
add(new ContentPane(), gbc);
gbc.gridx++;
add(new LeftPane(), gbc);
gbc.gridwidth = 2;
gbc.gridx = 0;
gbc.gridy = 1;
add(new BottomPane(), gbc);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
public class ContentPane extends JPanel {
public ContentPane() {
setBackground(Color.GREEN);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(150, 150);
}
@Override
public Dimension getMinimumSize() {
return getPreferredSize();
}
@Override
public Dimension getMaximumSize() {
return getPreferredSize();
}
}
public class BottomPane extends JPanel {
public BottomPane() {
setBackground(Color.RED);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 50);
}
@Override
public Dimension getMinimumSize() {
return getPreferredSize();
}
@Override
public Dimension getMaximumSize() {
return getPreferredSize();
}
}
public class LeftPane extends JPanel {
public LeftPane() {
setBackground(Color.BLUE);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(50, 150);
}
@Override
public Dimension getMinimumSize() {
return getPreferredSize();
}
@Override
public Dimension getMaximumSize() {
return getPreferredSize();
}
}
}
Now, if you prefer, you can make the screen non-resizable, but I for one won't like you. I prefer to use the power of the layout managers and allow users to make decisions about how they want to view the content ... where I can ... but that's just me (I don't like non-resizable windows except in the case of some dialogs)
现在,如果您愿意,您可以使屏幕不可调整大小,但我不会喜欢您。我更喜欢使用布局管理器的强大功能,并允许用户决定他们想要如何查看内容......我可以......但那只是我(我不喜欢不可调整大小的窗口,除了某些对话框的情况)
回答by MadProgrammer
Just use simple BorderLayout
and it will resize automatically:
只需使用简单BorderLayout
,它就会自动调整大小:
JPanel panel = new JPanel(new BorderLayout());
panel.add(greenPanel, BorderLayout.CENTER);
panel.add(redPanel, BorderLayout.SOUTH);
panel.add(bluePanel, BorderLayout.EAST);
回答by MadProgrammer
You can use MigLayout and replace BorderLayout.CENTER by "dock center":
您可以使用 MigLayout 并将 BorderLayout.CENTER 替换为“停靠中心”:
JPanel panel = new JPanel(new MigLayout(" insets 0"));
panel.add(greenPanel, "dock center");
panel.add(redPanel, "dock south");
panel.add(bluePanel, "dock east");
Read more about MigLayout here: http://www.miglayout.com/QuickStart.pdf
在此处阅读有关 MigLayout 的更多信息:http: //www.miglayout.com/QuickStart.pdf