为什么网格布局不显示java_java – 在GridLayout中不显示组件的FlowLayout?
我正在創建一個應用程序,作為某種類型的中心,用戶可以將快捷方式存儲到他們喜歡的應用程序并輕松啟動它們.不過,我在使用FlowLayout時遇到了一些問題.當我使用GridLayout時,組件顯示完美.當我使用FlowLayout時,根本沒有任何顯示.
網格布局:
FlowLayout中:
我所做的就是LayoutManager.當我調用getComponentCount時,它們都以9響應.
我覺得這個帖子很長,所以我把一個code的片段放在Code Tidy上(來自Pastebin)
預先感謝您的幫助!
解決方法:
1)FlowLayout非常接受來自JComponent的PreferredSize,每個JComponents都可以在屏幕上有不同的Dimension
示例(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)GridLayout為每個JComponents創建比例區域,然后僅接受來自PreferredSize的具有更大Dimnesion的JComponent
3)對于GridLayout我說的是方法pack(),如果沒有JFrame#setSize(),對于FLowLayout沒關系,
標簽:java,swing,layout-manager
來源: https://codeday.me/bug/20190723/1512637.html
總結
以上是生活随笔為你收集整理的为什么网格布局不显示java_java – 在GridLayout中不显示组件的FlowLayout?的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: linux 内存规划,生产场景怎么对li
- 下一篇: python标准输入_Python 处理
