我正在使应用程序充当某种类型的中心,用户可以将快捷方式存储到自己喜欢的应用程序中并轻松启动它们。不过,我在FlowLayout
遇到了一些问题。当我使用GridLayout
时,组件显示完美。当我使用FlowLayout
时,根本不会显示任何内容。
GridLayout的:
的FlowLayout:
我改变的只是LayoutManager
。当我致电getComponentCount
时,他们都回答9。
我认为这篇文章很长,所以我在Code Tidy(来自Pastebin)上放了我的code片段
提前感谢您的帮助!
答案 0 :(得分:6)
1)FlowLayout
非常接受来自PreferredSize
的{{1}},JComponent
中的每一个都可以在屏幕上显示不同的JComponents
示例(不明显的Dimension
& getMinimumSize
)
getMinimumSize
2)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);
}
}
为每个GridLayou
创建比例区域,然后只接受JComponents
来自JComponent
的{{1}}来自Dimnesion
3)对于PreferredSize
我正在谈论方法GridLayout
,而不是pack()
,因为JFrame#setSize()
并不重要,
答案 1 :(得分:3)
嗯,我知道这已经得到了解答,但只是补充一下,如果你看下面的代码并运行它,它将创建9个标签和4个按钮,并使用流程布局添加它们,但是如下面的示例所示帧大小使用setSize(int width,int height)
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class FlowLayoutTest extends JFrame {
private JPanel NorthPanel, SouthPanel;
private JLabel[] labels;
private JButton[] buttons;
public FlowLayoutTest() {
createAndShowUI();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
FlowLayoutTest flowLayoutTest = new FlowLayoutTest();
}
});
}
private void createAndShowUI() {
setTitle("Flow layout");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
initializeComponents();
addComponents(this.getContentPane());
//pack();
setVisible(true);
}
private void initializeComponents() {
labels = new JLabel[9];
for (int i = 0; i < labels.length; i++) {
labels[i] = new JLabel("Label " + (i+1));
}
buttons = new JButton[4];
for (int i = 0; i < buttons.length; i++) {
buttons[i] = new JButton("Button " + (i+1));
}
NorthPanel = new JPanel(new FlowLayout(5));
SouthPanel = new JPanel(new FlowLayout(5));
}
private void addComponents(Container contentPane) {
for (int i = 0; i < buttons.length; i++) {
SouthPanel.add(buttons[i]);
}
for (int i = 0; i < labels.length; i++) {
NorthPanel.add(labels[i]);
}
contentPane.add(NorthPanel, BorderLayout.NORTH);
contentPane.add(SouthPanel, BorderLayout.SOUTH);
}
}
然而,当你运行应用程序时,你会注意到一个标签(编号9)已经关闭,这是因为setSize()
被使用了,但是如果我们调用(或者在这种情况下取消注释)pack()
在将帧设置为可见之前,您还可以看到帧上的所有组件。