简体   繁体   English

使用流布局初始化多个JPanel(作业)

[英]Initialize multiple JPanels using a Flow Layout (Homework)

I am trying to create a JFrame with two JPanels inserted inside using FlowLayout. 我正在尝试使用FlowLayout创建一个内部插入两个JPanel的JFrame。 I have the frame being initialized in a separate file, but here is what I have being called 我将帧初始化在一个单独的文件中,但是这就是所谓的

public class FlowInFlow extends JFrame
{
public FlowInFlow() {

    setLayout(new FlowLayout());

    JPanel panel1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
    panel1.setBackground(Color.RED);

    JPanel panel2 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    panel2.setBackground(Color.BLUE);   

}
}

Edit: When I run this I just get a blank box, when I need the two boxes side to side 编辑:运行此程序时,我只得到一个空白框,当我需要两个框并排放置时

As I've already stated, the default preferred size of a JPanel is 0x0... 正如我已经说过的, JPanel的默认首选大小是0x0 ...

This means that when you add it to a layout like FlowLayout , the uses the preferred size, it will appear...well...it won't 这意味着,当您将其添加到FlowLayout之类的布局中时,使用首选大小,它将显示...好吧...它不会

在此处输入图片说明

public class TestFlowLayout {

    public static void main(String[] args) {
        new TestFlowLayout();
    }

    public TestFlowLayout() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JPanel master = new JPanel(new FlowLayout(FlowLayout.LEFT));
                JPanel left = new JPanel();
                left.setBackground(Color.RED);
                left.add(new JLabel("Lefty"));

                JPanel right = new JPanel();
                right.setBackground(Color.BLUE);
                right.add(new JLabel("Righty"));

                master.add(left);
                master.add(right);

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(master);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

Besides the advice to change the outer layout, those components were never being added to anything (so would never be visible). 除了更改外部布局的建议外,这些组件从未添加到任何组件中(因此永远不会可见)。

网格中的红色/蓝色流

import java.awt.*;
import javax.swing.*;

public class FlowInGrid extends JFrame  {

    public FlowInGrid() {

        setLayout(new GridLayout(1,0));

        JPanel panel1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        panel1.setBackground(Color.RED);
        // ADD Them to something!
        add(panel1);

        JPanel panel2 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        panel2.setBackground(Color.BLUE);   
        // ADD Them to something!
        add(panel2);
    }

    public static void main(String[] args) throws Exception {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                JFrame f = new FlowInGrid();
                f.setSize(300,100);
                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM