5

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();
    }
}
flea whale
  • 1,783
  • 3
  • 25
  • 38
  • 1
    The Swing engine has its own paint loop, you don't need while(true). Just override `JComponent.paint(Graphics g)` in your panel class – Joe Coder Feb 25 '12 at 05:19
  • Hi @Joe, I'm trying to use `BufferStrategy` here for which you dont use `paint()`, thats why the `while()` loop and what the `setIgnoreRepaint(true);` are for. Any idea about how I can fill the whole window by any chance? – flea whale Feb 25 '12 at 05:23
  • Yes, try passing your JPanel to `JFrame.setContentPane()` – Joe Coder Feb 29 '12 at 00:43

4 Answers4

6

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.
Harry Joy
  • 58,650
  • 30
  • 162
  • 207
  • Hi @Harry, I've just tried your suggestion (see new *edited lines* in code above) but it displays exactly the same! – flea whale Feb 25 '12 at 05:24
  • thanks but I'm trying to make a game using `BufferStrategy` here for which you don't use `paintComponent`, you first draw the stuff and then you flip the buffer over using `bs.show()` – flea whale Feb 25 '12 at 05:39
3

This took me forever to figure out but its actually the simplest code ever. Just create a parent panel and pass GridLayout then add your child panel like this.

JPanel parentPanel= new JPanel(new GridLyout());
JPanel childPanel= new JPanel();
parentPanel.add(childPanel);
kupaff
  • 329
  • 4
  • 5
  • Seems that the FlowLayout used by default tries to squish everything as small as it can. Even a BorderLayout solved the issue. – user515655 Nov 30 '21 at 13:59
3

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();
   }

}
T.P.
  • 2,975
  • 1
  • 21
  • 20
  • +1 for `pack()`; -1 for using `setPreferredSize()` [incorrectly](http://stackoverflow.com/q/7229226/230513). – trashgod Feb 25 '12 at 06:51
  • @trashgod DYM 1) Use an advanced layout (AKA the 'kleopatra method') 2) Override `getPreferredSize()` 3) Something else? Since 1) requires 3rd party layouts, it cannot be done in core J2SE (which may or may not be a problem) 2) Makes the preferred size of the component inflexible. I would prefer setting the size just as jtp did. +1 – Andrew Thompson Feb 26 '12 at 04:29
  • 1
    @AndrewThompson: Thank you for elaborating. I meant 2) override [`getPreferredSize()`](http://docs.oracle.com/javase/7/docs/api/javax/swing/JComponent.html#getPreferredSize%28%29) in the sense shown [here](http://stackoverflow.com/a/9447425/230513) and mentioned in bullet one [here](http://stackoverflow.com/a/7229662/230513). I see an unexplained `setPreferredSize()` as potentially misleading in this context. It depends on what's _in_ the panel: more components → `setPreferredSize()` bad; just painting → `setPreferredSize()` OK. – trashgod Feb 26 '12 at 04:52
  • 1
    @trashgod Thanks for clarifying. After glancing at the OP's code (& seeing the `BufferStrategy`) I decided there was almost 0 chance of anything but 'just painting'. However now I think about it, for 'just painting', a `JComponent` is usually sufficient. Also the word 'unexplained' suggests the answer might be edited with a code comment to make the context clear. – Andrew Thompson Feb 26 '12 at 08:48
1

If you want to fill the JFrame with the whole of JPanel you need to setUndecorated to true i.e. frame.setUndecorated(true);. But now you have to worry about your MAXIMIZE< MINIMIZE, and CLOSE Buttons, towards the top right side(Windows Operating System)

nIcE cOw
  • 24,468
  • 7
  • 50
  • 143