如何在 Java 面板中使用流布局?

问题描述 投票:0回答:0

这里是有问题的代码:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;

public class xtemp {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(1067, 600);
        frame.getContentPane().setBackground(new Color(0x000000));
        frame.setLayout(null);

        JPanel panel_red = new JPanel();
        panel_red.setBackground(Color.red);
        panel_red.setBounds(10, 10, 300, 300);

        JPanel panel_blue = new JPanel();
        panel_blue.setBackground(Color.blue);
        panel_blue.setBounds(10, 310, 300, 300);

        JPanel panel_green = new JPanel();
        panel_green.setBackground(Color.green);
        panel_green.setPreferredSize(new Dimension(250, 250));
        panel_green.setBounds(310, 10, 800, 600);
        panel_green.setLayout(new FlowLayout(FlowLayout.TRAILING, 9, 16));

        for (int i = 0; i <= 99; i++)
            panel_green.add(new JButton(String.format("%02d", i)));

        frame.add(panel_red);
        frame.add(panel_green);
        frame.add(panel_blue);
        frame.setVisible(true);
    }
}

这是代码的输出:

我想要类似这样的输出:

我想使用多个面板,并为框架内的不同面板提供不同的布局。我基本上希望

panel_green
内的任何按钮都必须根据可用空间自动调整,而不管它是一个面板,而不是第二个 gif/图像中显示的框架。

java swing user-interface panel flowlayout
© www.soinside.com 2019 - 2024. All rights reserved.