How to make components in a stretched JPanel as large as possible?
I am trying to create a simple swing program that allows the user to play with a circle.
Here is my code:
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() +")");
}
}
I am trying to create a JPanel with different colors. When you click on a color, the color of the circle changes. I haven't implemented this part yet.
My problem is that when I run the above program, I get output as:
But I want the result to be
I tried to remove
colorPan.setLayout(new FlowLayout(FlowLayout.CENTER));
and this led to the same conclusion.
How do I stretch the components in colorPan
to get the expected result?
+3
source to share