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

我与影子孤独终老i 提交于 2019-12-30 10:29:49

问题


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

回答1:


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.



回答2:


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

}



回答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)




回答4:


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


来源:https://stackoverflow.com/questions/9441422/how-to-make-a-jpanel-inside-a-jframe-fill-the-whole-window

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!