hi all

i m making chess in java and have a problem. Plz if u can help me out.

Problem is when i add a JPanel to a JFrame it sets the size of JPanel given by me in the code. but when i add another JPanel then it automatically manages the size. and makes the size of both jpanels equal. and does not set the size given by me.

plz help me out if u can. must b very thankful

public class Chess extends javax.swing.JFrame
{
    private Board board;
    private String curr_player;
    private String []turn;
    JPanel pane,p;
    /** Creates new form Chess */
    public Chess()
    {
        initComponents();
        
        pane.setBackground(Color.red);
        //pane.setVisible(true);
        board = new Board();
        board.setBounds(0,0,600, 600);
        setSize(700,700);
        /////////////////////////////////////////////////////
        p = new JPanel();
        p.setSize(100, 100);
        pane = new JPanel();
        pane.setSize(100, 100);

        add(board);
        add(pane);
        add(p);
        validate();

;

Edited 7 Years Ago by adcodingmaster: n/a

From the code provided I can see that the size for p and pane are the same (100, 100) this is why the size of both panels are equal. Then again you are setting the panel background color before initializing it

pane.setBackground(Color.red); // this line should go after pane = new JPanel();
// ...
pane = new JPanel();

this should generate an error, if you don't get it you probably already initialized the panel in the initComponents() method.

Anyway you should take a look here A Visual Guide to Layout Managers

Write a program that displays the recommended weight given the user’s age and height. The formula for calculating the recommended = (height – 100 + age % 10) * 0.9

Write a program that displays the recommended weight given the user’s age and height. The formula for calculating the recommended weight is
RecommendedWeight = (height – 100 + age % 10) * 0.90

This article has been dead for over six months. Start a new discussion instead.