3

ユーザーがお気に入りのアプリケーションへのショートカットを保存して簡単に起動できる、ある種のハブとして機能するアプリケーションを作成しています。しかし、私はいくつかの問題を抱えてFlowLayoutいます。を使用するGridLayoutと、コンポーネントが完全に表示されます。を使用するFlowLayoutと、何も表示されません。

グリッドレイアウト: グリッドレイアウト

フローレイアウト: フローレイアウト

私が変更したのはLayoutManager. を呼び出すgetComponentCountと、両方とも 9 で応答します。

この投稿はかなり長いと思ったので、Code Tidy (Pastebin から) にコードのスニペットを載せました。

よろしくお願いします。

4

2 に答える 2

6

1)から来たものをFlowLayoutかなり受け入れ、それぞれが画面上で異なっている可能性がありますPreferredSizeJComponentJComponentsDimension

例 (コメント解除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のみを受け入れます。JComponentDimnesionPreferredSize

3)GridLayout私は method について話しているpack()ので、 if is thereJFrame#setSize()ではなく、 forFLowLayoutは問題ではありません。

于 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 に答える