如何尽可能在JPanel中制作组件?

时间:2015-04-29 11:43:10

标签: java swing jpanel components layout-manager

我正在尝试创建一个简单的挥杆程序,让用户玩一个圆圈。

这是我的代码:

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。单击颜色时,圆的颜色将会改变。我还没有实现这个部分。

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

ACTUAL_OUTPUT

但我希望输出为

EXPECTED_OUTPUT

我试过删除

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

这导致了相同的输出。

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

1 个答案:

答案 0 :(得分:5)

而不是FlowLayout,您可以使用GridLayout,如下所示:

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

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