I have tried running the following code. The GUI constructor that I call is from a class that extends JPanel
.
As you can see I have already tried using the pack()
method.
The window that appears is smaller than 500x500. How do I fix this?
import java.awt.Dimension;
import javax.swing.JFrame;
public class Main extends JFrame {
public Main(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setPreferredSize(new Dimension(500, 500));
setTitle("Chess");
GUI gui = new GUI();
setContentPane(gui);
pack();
setVisible(true);
}
public static void main(String[] args){
Main main = new Main();
main.repaint();
}
Already tried using pack()
.
CodePudding user response:
Because the frame size includes the frame borders, title bars, and headers which are especially large and take up extra space. Set the size of a JPanel
and add that to the frame and draw and add components to the panel. And don't forget to call frame.pack()
.