如何让 JPanel 中的组件尽可能地伸展?

How to make components in a JPanel stretch as far as possible?

我正在尝试创建一个简单的摇摆程序,让用户可以玩一个圆圈。

这是我的代码:

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.border.Border;
import javax.swing.BorderFactory;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.FlowLayout;
import java.awt.BorderLayout;

public class SixthProgram
{
    public static void main(String[] args)
    {
        GUI prog=new GUI("SixthProgram");
        prog.setBounds(350,250,500,250);
        prog.setVisible(true);
    }
}

class GUI extends JFrame implements MouseListener, MouseMotionListener
{
    JPanel colorPan, color1, color2, color3 ,color4 ,color5;

    int x=3,y=30; // Position of circle

    public GUI(String header)
    {
        super(header);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        maker();

        addMouseListener(this);
        addMouseMotionListener(this);

        add(colorPan, BorderLayout.SOUTH);
    }

    public void maker()
    {
        colorPan = new JPanel();

        Border raisedbevel = BorderFactory.createRaisedBevelBorder();
        Border loweredbevel = BorderFactory.createLoweredBevelBorder();
        Border compound = BorderFactory.createCompoundBorder(raisedbevel, loweredbevel);
        colorPan.setBorder(compound);

        colorPan.setLayout(new FlowLayout(FlowLayout.CENTER));

        color1 = new JPanel();
        color2 = new JPanel();
        color3 = new JPanel();
        color4 = new JPanel();
        color5 = new JPanel();

        color1.setBackground(Color.WHITE);
        color2.setBackground(Color.GREEN);
        color3.setBackground(Color.RED);
        color4.setBackground(Color.BLUE);
        color5.setBackground(Color.BLACK);

        colorPan.add(color1);
        colorPan.add(color2);
        colorPan.add(color3);
        colorPan.add(color4);
        colorPan.add(color5);

    }

    @Override
    public void paint(Graphics g)
    {
        //g.setColor(Color.WHITE);
        //g.fillRect(0,0,getWidth(),getHeight());
        super.paint(g); //Do the same thing as above(Clear jframe)

        g.setColor(Color.RED);
        g.fillOval(x,y,50,50);
    }

    public void mouseExited(MouseEvent e) //MouseListener overrided methods
    {
        System.out.println("Exit");
    }

    public void mouseEntered(MouseEvent e)
    {
        System.out.println("Enter");
    }

    public void mouseReleased(MouseEvent e)
    {
        System.out.println("Release");
    }

    public void mousePressed(MouseEvent e)
    {
        System.out.println("Press");    
        x=e.getX();
        y=e.getY();
        if(x+50<getWidth() && y+50<getHeight()) // Preventing out of bounds
            repaint();
    }

    public void mouseClicked(MouseEvent e) //Press+Release=Click
    {
        System.out.println("Click");
        System.err.println(((JPanel)e.getSource()).getName());
    }

    public void mouseDragged(MouseEvent e) //MouseMotionListener overrided methods
    {
        System.out.println("Dragged to ("+ e.getX() +","+ e.getY() +")");
        x=e.getX();
        y=e.getY();
        if((x>=3 && y>=30) && (x+50<getWidth() && y+50<getHeight())) // Preventing out of bounds
            repaint();
    }

    public void mouseMoved(MouseEvent e)
    {
        System.out.println("Moved to ("+ e.getX() +","+ e.getY() +")");
    }

}

我正在尝试创建一个具有不同颜色的 JPanel。单击颜色时,圆圈的颜色会发生变化。我还没有实现这部分。

我的问题是,当我 运行 上面的程序时,我得到的输出是:

但我希望输出为

我试过删除

colorPan.setLayout(new FlowLayout(FlowLayout.CENTER));

这导致了相同的输出。

如何拉伸 colorPan 中的组件以获得预期的输出?

尝试:

colorPan.setLayout(new BoxLayout(colorPan,BoxLayout.LINE_AXIS));

更改以下内容:

colorPan.setLayout(new FlowLayout(FlowLayout.CENTER));

到会拉伸内容以适应的布局,例如 GridLayout:例如

colorPan.setLayout(new GridLayout(1,0));

您可以使用 GridLayout 代替 FlowLayout,如下所示:

colorPan.setLayout(new GridLayout(1, 5));

这将元素的宽度分为 1 行和 5 列,每种颜色一个。