Danil Baril
Danil Baril

Reputation: 1

setBounds in java swing not working as intended

I know people are probably gonna tell me to use layouts instead of this but for the project im doing I would really like to able to set the positioning myself.

The problem im having is that I have all the positioning and sizing correct in the panels but when I run the code it simply doesnt have either the dimensions or the positioning I set and I dont understand why.

import javax.swing.*;
import java.awt.*;

class MyFrame extends JFrame {
    MyFrame() {
        SwingUtilities.invokeLater(() -> {
            JPanel combatPanel = new JPanel();
            combatPanel.setLayout(null);
            combatPanel.setBounds(0,0,1380,805);
            combatPanel.setBackground(Color.darkGray);

            JPanel combatImage = new JPanel();
            combatImage.setLayout(null);
            combatImage.setBounds(0,0,1380,575);
            combatImage.setBackground(Color.black);

            JPanel combatActions  = new JPanel();
            combatActions.setLayout(null);
            combatActions.setBounds(0,575,480,230);
            combatActions.setBackground(Color.gray);

            JPanel combatActionInfo = new JPanel();
            combatActionInfo.setLayout(null);
            combatActionInfo.setBounds(480,575,900,115);
            combatActionInfo.setBackground(Color.lightGray);

            JPanel combatPlayerInfo = new JPanel();
            combatPlayerInfo.setLayout(null);
            combatPlayerInfo.setBounds(480,690,900,115);
            combatPlayerInfo.setBackground(Color.red);


            ImageIcon icon = new ImageIcon("image.png");

            this.setTitle("F&H 342"); // sets title of frame
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // exit out of application
            this.setResizable(false); // prevent frame from being resized
            this.setSize(1380, 805); // sets the x-dimension, and y-dimension of frame
            this.setVisible(true); // make frame visible
            this.getContentPane().setBackground(Color.yellow);
            this.setIconImage(icon.getImage());
            this.setLayout(null);

            combatPanel.add(combatImage);
            combatPanel.add(combatActions);
            combatPanel.add(combatActionInfo);
            combatPanel.add(combatPlayerInfo);
            this.add(combatPanel);
        });
    }
}

public class Main {
    public static void main(String[] args) {
        MyFrame myFrame = new MyFrame();
    }
}

what i was trying to do

Tried to make dimensions like in the image.

Upvotes: 0

Views: 26

Answers (1)

Danil Baril
Danil Baril

Reputation: 1

The title bar counts towards the size you set your windows to be, and it also seems to trim the edges for some reason, adding 13 to width and 36 to height seems to work.

Upvotes: 0

Related Questions