`pack()` 和 `setLayout()` 的问题
Issues with `pack()` and `setLayout()`
我有密码
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.FlowLayout;
class GUI extends JFrame {
JPanel mainPanel;
public GUI(String header) {
super(header);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//setLayout(new FlowLayout(FlowLayout.CENTER));
init();
add(mainPanel);
pack();
}
public void init() {
mainPanel = new JPanel(){
@Override
public void paintComponent(Graphics g) {
g.fillOval(0, 0, 50, 50);
}
};
}
}
在我的主要方法中,我有
GUI progam = new GUI("Title");
progam.setLocationRelativeTo(null);
progam.setVisible(true);
如果我运行程序,我得到输出:
如果我取消注释 setLayout
,我会得到输出:
两个问题:
- 为什么
pack()
在第一种情况下没有按预期工作?我不应该看到整个圆而不是一半吗?
- 为什么第二个输出的椭圆变成了三角形?
Why isn't pack() working as expected in the first case? Shouldn't I see the full circle instead of seeing half of it?
是的,您的 mainPanel
没有为布局管理器提供大小调整提示,因此它使用的是组件的默认大小调整提示 (0x0)
添加
@Override
public Dimension getPreferredSize() {
return new Dimension(50, 50);
}
你 mainPanel
和 super.paintComponent(g)
你的 paintComponent
方法,在你做任何自定义绘画之前
Why did the oval turn into a triangle in the second output?
这就是 BorderLayout
和 FlowLayout
之间的区别
当您在 JPanel 中绘图时,您实际上并没有在其中放置组件,因此它与您的 JPanel 一样没有任何尺寸(宽度、高度)。
如此处所述https://docs.oracle.com/javase/tutorial/uiswing/components/frame.html,第 4 项:
The pack method sizes the frame so that all its contents are at or above their preferred sizes. An alternative to pack is to establish a frame size explicitly by calling setSize or setBounds (which also sets the frame location). In general, using pack is preferable to calling setSize, since pack leaves the frame layout manager in charge of the frame size, and layout managers are good at adjusting to platform dependencies and other factors that affect component size.
要解决此问题,请使用 setBounds
或 setSize
或添加具有首选大小的组件。
希望对您有所帮助=)
我有密码
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.FlowLayout;
class GUI extends JFrame {
JPanel mainPanel;
public GUI(String header) {
super(header);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//setLayout(new FlowLayout(FlowLayout.CENTER));
init();
add(mainPanel);
pack();
}
public void init() {
mainPanel = new JPanel(){
@Override
public void paintComponent(Graphics g) {
g.fillOval(0, 0, 50, 50);
}
};
}
}
在我的主要方法中,我有
GUI progam = new GUI("Title");
progam.setLocationRelativeTo(null);
progam.setVisible(true);
如果我运行程序,我得到输出:
如果我取消注释 setLayout
,我会得到输出:
两个问题:
- 为什么
pack()
在第一种情况下没有按预期工作?我不应该看到整个圆而不是一半吗? - 为什么第二个输出的椭圆变成了三角形?
Why isn't pack() working as expected in the first case? Shouldn't I see the full circle instead of seeing half of it?
是的,您的 mainPanel
没有为布局管理器提供大小调整提示,因此它使用的是组件的默认大小调整提示 (0x0)
添加
@Override
public Dimension getPreferredSize() {
return new Dimension(50, 50);
}
你 mainPanel
和 super.paintComponent(g)
你的 paintComponent
方法,在你做任何自定义绘画之前
Why did the oval turn into a triangle in the second output?
这就是 BorderLayout
和 FlowLayout
当您在 JPanel 中绘图时,您实际上并没有在其中放置组件,因此它与您的 JPanel 一样没有任何尺寸(宽度、高度)。
如此处所述https://docs.oracle.com/javase/tutorial/uiswing/components/frame.html,第 4 项:
The pack method sizes the frame so that all its contents are at or above their preferred sizes. An alternative to pack is to establish a frame size explicitly by calling setSize or setBounds (which also sets the frame location). In general, using pack is preferable to calling setSize, since pack leaves the frame layout manager in charge of the frame size, and layout managers are good at adjusting to platform dependencies and other factors that affect component size.
要解决此问题,请使用 setBounds
或 setSize
或添加具有首选大小的组件。
希望对您有所帮助=)