FlowLayout不显示组件,而GridLayout呢?(FlowLayout not displaying components while GridLayout does?)

编程入门 行业动态 更新时间:2024-10-21 18:30:09
FlowLayout不显示组件,而GridLayout呢?(FlowLayout not displaying components while GridLayout does?)

我正在让一个应用程序充当某种类型的中心,用户可以将快捷方式存储到他们最喜欢的应用程序中并轻松启动它们。 不过,我遇到了FlowLayout一些问题。 当我使用GridLayout ,组件显示完美。 当我使用FlowLayout ,根本没有任何显示。

网格布局:

FlowLayout中:

我所有改变的是LayoutManager 。 当我调用getComponentCount ,它们都以9响应。

我认为这篇文章很长,所以我把代码片段放在Code Tidy上(来自Pastebin)

预先感谢您的帮助!

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:

FlowLayout:

All I have changed is the LayoutManager. When I call getComponentCount, they both respond with 9.

I thought this post was pretty long, so I put a snippet of my code on Code Tidy (from Pastebin)

Thank you in advance for your help!

最满意答案

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) GridLayou为每个JComponents创建比例区域,然后只接受具有较大Dimnesion JComponent来自PreferredSize

3)对于GridLayout我正在谈论方法pack() ,而不是如果有JFrame#setSize() ,对于FLowLayout无所谓,

1) FlowLayout pretty accepting PreferredSize that came from JComponent, each of JComponents can have got different Dimension on the screen

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) GridLayout create proportional area for every JComponents, then accepting only JComponent that have got larger Dimnesion came from PreferredSize

3) for GridLayout I'm talking about method pack(), not if is there JFrame#setSize(), for FLowLayout doesn't matter,

更多推荐

本文发布于:2023-08-04 06:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1410153.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组件   FlowLayout   GridLayout   components   displaying

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!