Sainath S.R
Sainath S.R

Reputation: 3306

JFrame's Border Layout Positioning Not as Expected

I'm trying to create a Java Program that does this,

A Frame with

NORTH ;Description:Two Button Listener

CENTRE:Description: Click Button1 to change solidcolor circle and Button2 to change gradient circle

SOUTH: I want 2 buttons ,Button 1 and Button 2

WEST: A Circle with a gradient colour

EAST:A circle with a solid colour

It's fairly Simple , when I click a button the respective circle's colour should change

The code is as follows ..

import javax.swing.*;//for the frame etc
import java.awt.*;//for  paintComponent(),Grapics Object etc
import java.awt.event.*;//for listeners


class CircleGradientColor extends JPanel
{
    public void paintComponent(Graphics g)
    {
     
    Graphics2D g2d=(Graphics2D)g;//cast 
    int r1=(int)(Math.random()*255);
    int g1=(int)(Math.random()*255);
    int b1=(int)(Math.random()*255);
    
    int r2=(int)(Math.random()*255);
    int g2=(int)(Math.random()*255);
    int b2=(int)(Math.random()*255);
    Color startcolor=new Color(r1,g1,b1);
    Color endcolor=new Color(r2,g2,b2);
    GradientPaint gradient=new GradientPaint(10,10,startcolor,70,70,endcolor);
    g2d.setPaint(gradient);//here it aint set color for a Graphics2D object
    g2d.fillOval(10,10,60,60);//fills with the Current PaintBrushColor
    }

}

class CircleSolidColor extends JPanel//this class will contain code for the circle
{

    public void paintComponent(Graphics g)
    {
    int r=(int)(Math.random()*255);//generate random float between 0 & 255
    int b=(int)(Math.random()*255);//generate random float between 0 & 255
    int gr=(int)(Math.random()*255);//generate random float between 0 & 255
    Color randcolor=new Color(r,gr,b);//name clashed with Graphics g
    g.setColor(randcolor);
    g.fillOval(10,10,60,60);
    }
}

public class TwoButtonGui 
{
JFrame frame;
CircleSolidColor circlesolidcolor;
CircleGradientColor circlegradientcolor;
JButton b1;
JButton b2;
JLabel toplabel;
JLabel centerlabel;

    public static void main(String[] args)
    {
    TwoButtonGui twobuttongui=new TwoButtonGui();
    twobuttongui.go();
    }

public void go()
{
frame=new JFrame();

toplabel=new JLabel("Example of Multiple Action Listeners");
centerlabel=new JLabel("Click The respective Button to change circle Color");
b1=new JButton("Click to change solid");
b2=new JButton("Click to change Gradient");
b1.addActionListener(new CircleSolidColorListener());
b2.addActionListener(new CircleGradientColorListener());

circlesolidcolor=new CircleSolidColor();
circlegradientcolor=new CircleGradientColor();
frame.setSize(1000,1000);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(BorderLayout.NORTH,toplabel);
frame.getContentPane().add(BorderLayout.SOUTH,b1);
frame.getContentPane().add(BorderLayout.SOUTH,b2);

//if i add 2 SOUTH position i though first will go left and second right 
//IS it so
frame.getContentPane().add(BorderLayout.CENTER,centerlabel);
frame.getContentPane().add(BorderLayout.WEST,circlesolidcolor);
frame.getContentPane().add(BorderLayout.EAST,circlegradientcolor);
frame.setVisible(true);

}
//inner class for solidlistener
class CircleSolidColorListener implements ActionListener
{
    public void actionPerformed(ActionEvent e1)
    {
    circlesolidcolor.repaint();
    }
}


class CircleGradientColorListener implements ActionListener
{
    public  void actionPerformed(ActionEvent e2)
    {
    circlegradientcolor.repaint();
    }
}

}//main class ends

But the output I get is rather absurd

Screen Shot of the Swing Program Output

Where am I going Wrong , I know it can be corrected using multiple Panels and LayoutMangaers etc but is there a way to get the results I described without all that? I wrote the program as a variant of an example in Headfirst Java which seems to work just fine with (2 buttons , a label and a Circle ), so why doesn't this work , can I add 2 components in a single position like say BorderLayout.SOUTH as described in my comments, Thanks!

Upvotes: 2

Views: 123

Answers (1)

user1941489
user1941489

Reputation:

North, is correct.

South, adding b2 will overwrite b1, what you want to do is create a new container like JPanel and add b1 to b2 to that container. Then add this container to south. This new container can use GridLayout(1,2)

Center, is correct.

East and west, the components are 'blank' so the default size giving by layout manager is none ( painting in the container background != isNotBlank() ). You can however use circles*.setPrefferredSize(dimension) to force a size on them.

Upvotes: 1

Related Questions