网站无法添加图片,论坛网站建设公司,重庆自助企业建站模板,胡先煦回复秦海璐网页链接我正在创建一个应用程序,作为某种类型的中心,用户可以将快捷方式存储到他们喜欢的应用程序并轻松启动它们.不过,我在使用FlowLayout时遇到了一些问题.当我使用GridLayout时,组件显示完美.当我使用FlowLayout时,根本没有任何显示.网格布局#xff1a;FlowLayout中#xff1a;我…我正在创建一个应用程序,作为某种类型的中心,用户可以将快捷方式存储到他们喜欢的应用程序并轻松启动它们.不过,我在使用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 componentsetMinimumSize(getMinimumSize());setPreferredSize(getPreferredSize());setVisible(true);}public static void main(String[] args) {Runnable r new Runnable() {Overridepublic void run() {CustomComponent main new CustomComponent();main.display();}};javax.swing.SwingUtilities.invokeLater(r);}}class CustomComponents0 extends JLabel {private static final long serialVersionUID 1L;/*Overridepublic Dimension getMinimumSize() {return new Dimension(200, 100);}Overridepublic Dimension getPreferredSize() {return new Dimension(300, 200);}*/Overridepublic 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的JComponent3)对于GridLayout我说的是方法pack(),如果没有JFrame#setSize(),对于FLowLayout没关系,标签java,swing,layout-manager来源 https://codeday.me/bug/20190723/1512637.html