3

我正在制作一个应用程序来充当某种中心,用户可以在其中存储他们喜欢的应用程序的快捷方式并轻松启动它们。不过,我遇到了一些问题FlowLayout。当我使用GridLayout时,组件显示完美。当我使用FlowLayout时,什么都没有显示。

网格布局: 网格布局

流布局: 流式布局

我所改变的只是LayoutManager. 当我打电话时getComponentCount,他们都以 9 回应。

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

预先感谢您的帮助!

4

2 回答 2

6

1)FlowLayout相当接受PreferredSize来自JComponent, 每个都可以在屏幕上JComponents得到不同的Dimension

示例(取消注释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,然后只接受JComponent已经变大Dimnesion的来自PreferredSize

3)因为GridLayout我在谈论方法pack(),而不是如果存在JFrame#setSize(),因为FLowLayout没关系,

于 2012-06-17T13:01:47.237 回答
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()在将框架设置为可见之前调用(或在本例中取消注释),您将能够看到框架上的所有组件。

于 2012-06-17T13:44:17.497 回答