问题描述
我正在创建一个应用程序来充当某些类型的中心,用户可以将快捷方式存储到他们喜欢的应用程序中并轻松启动它们。不过,我在使用 FlowLayout
时遇到了一些问题。当我使用 GridLayout
时,组件显示完美。当我使用 FlowLayout
时,根本不会显示任何内容。
I'm making an application to act as a hub of some sorts, where the user can store shortcuts to their favorite applications and easily launch them. I'm having some problems with FlowLayout
, though. When I use GridLayout
, the components display perfectly. When I use FlowLayout
, nothing displays at all.
GridLayout:
GridLayout:
FlowLayout:
FlowLayout:
我所有改变的是 LayoutManager
。当我打电话给 getComponentCount
时,他们都回答9。
All I have changed is the LayoutManager
. When I call getComponentCount
, they both respond with 9.
我认为这个帖子很长,所以我把Code Tidy上的的片段(来自Pastebin)
I thought this post was pretty long, so I put a snippet of my code on Code Tidy (from Pastebin)
提前感谢您的帮助!
推荐答案
1) FlowLayout
非常接受来自 JComponent
的 PreferredSize
,每个 JComponents
屏幕上可以有不同的 Dimension
1) FlowLayout
pretty accepting PreferredSize
that came from JComponent
, each of JComponents
can have got different Dimension
on the screen
示例(不明显的 getMinimumSize
& getMinimumSize
)
example (uncomnent getMinimumSize
& getMinimumSize
)
import java.awt.*;
import javax.swing.*;
public class CustomComponent extends JFrame {
private static final long serialVersionUID = 1L;
public CustomComponent() {
setTitle("Custom Component Test / BorderLayout");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
}
public void display() {
add(new CustomComponents0(), BorderLayout.NORTH);
add(new CustomComponents0(), BorderLayout.CENTER);
add(new CustomComponents0(), BorderLayout.SOUTH);
add(new CustomComponents0(), BorderLayout.EAST);
pack();
// enforces the minimum size of both frame and component
setMinimumSize(getMinimumSize());
setPreferredSize(getPreferredSize());
setVisible(true);
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
CustomComponent main = new CustomComponent();
main.display();
}
};
javax.swing.SwingUtilities.invokeLater(r);
}
}
class CustomComponents0 extends JLabel {
private static final long serialVersionUID = 1L;
/*@Override
public Dimension getMinimumSize() {
return new Dimension(200, 100);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(300, 200);
}*/
@Override
public void paintComponent(Graphics g) {
int margin = 10;
Dimension dim = getSize();
super.paintComponent(g);
g.setColor(Color.red);
g.fillRect(margin, margin, dim.width - margin * 2, dim.height - margin * 2);
}
}
2) GridLayou
t为每个 JComponents
创建比例区域,然后仅接受 JComponent
,其中<$ c c更大$ c> Dimnesion 来自 PreferredSize
2) GridLayou
t create proportional area for every JComponents
, then accepting only JComponent
that have got larger Dimnesion
came from PreferredSize
3) GridLayout
我说的是方法 pack()
,而不是 JFrame#setSize()
,对于 FLowLayout
无关紧要,
3) for GridLayout
I'm talking about method pack()
, not if is there JFrame#setSize()
, for FLowLayout
doesn't matter,
这篇关于在GridLayout中,FlowLayout不显示组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!