Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a JPanel inside a JFrame fill the whole window?

Tags:

java

swing

jpanel

In the below example, how can I get the JPanel to take up all of the JFrame? I set the preferred size to 800x420 but it only actually fills 792x391.

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class BSTest extends JFrame {
    BufferStrategy bs;
    DrawPanel panel = new DrawPanel();

    public BSTest() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());   // edited line
        setVisible(true);
        setSize(800,420);
        setLocationRelativeTo(null);
        setIgnoreRepaint(true);
        createBufferStrategy(2);
        bs = getBufferStrategy();
        panel.setIgnoreRepaint(true);
        panel.setPreferredSize(new Dimension(800,420));
        add(panel, BorderLayout.CENTER);     // edited line
        panel.drawStuff();
    }

    public class DrawPanel extends JPanel {
        public void drawStuff() {
            while(true) {
                try {
                    Graphics2D g = (Graphics2D)bs.getDrawGraphics();
                    g.setColor(Color.BLACK);
                    System.out.println("W:"+getSize().width+", H:"+getSize().height);
                    g.fillRect(0,0,getSize().width,getSize().height);
                    bs.show();
                    g.dispose();
                    Thread.sleep(20);
                } catch (Exception e) { System.exit(0); }
            }
        }
     }

    public static void main(String[] args) {
        BSTest bst = new BSTest();
    }
}
like image 880
flea whale Avatar asked Dec 13 '22 05:12

flea whale


2 Answers

If you are having only one panel in frame and nothing else then try this:

  • Set BorderLayout in frame.
  • Add panel in frame with BorderLayout.CENTER

May be this is happening because of while loop in JPanel.(Not sure why? finding actual reason. Will update when find it.) If you replace it with paintComponent(g) method all works fine:

public BSTest() {
    //--- your code as it is
    add(panel, BorderLayout.CENTER);
    //-- removed panel.drawStuff();
}

public class DrawPanel extends JPanel {
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setColor(Color.BLACK);
        System.out.println("W:" + getSize().width + ", H:" + getSize().height);
        g2d.fillRect(0, 0, getSize().width, getSize().height);
    }
 }

//your code as it is.
like image 128
Harry Joy Avatar answered Feb 01 '23 23:02

Harry Joy


Here's an alternative using pack instead.

import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class PackExample extends JFrame {

    public PackExample(){
        JPanel panel = new JPanel();
        panel.setPreferredSize(new Dimension(800,600));
        panel.setBackground(Color.green);
        add(panel);
        pack();
        setVisible(true);
   }

   public static void main(String[] args){
     new PackExample();
   }

}
like image 32
T.P. Avatar answered Feb 01 '23 23:02

T.P.