Skip to main content

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



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

}

}




Comments

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

    }

    ReplyDelete
  2. 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.

    ReplyDelete
  3. 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)

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.