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

后端 未结 4 421
深忆病人
深忆病人 2021-01-14 01:43

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 j         


        
4条回答
  •  说谎
    说谎 (楼主)
    2021-01-14 02:12

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

提交回复
热议问题