I have code
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.FlowLayout;
class GUI extends JFrame {
JPanel mainPanel;
public GUI(String header) {
super(header);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//setLayout(new FlowLayout(FlowLayout.CENTER));
init();
add(mainPanel);
pack();
}
public void init() {
mainPanel = new JPanel(){
@Override
public void paintComponent(Graphics g) {
g.fillOval(0, 0, 50, 50);
}
};
}
}
and in my main method, I have
GUI progam = new GUI("Title");
progam.setLocationRelativeTo(null);
progam.setVisible(true);
If I run the program, I get output:
and if I uncomment the setLayout
, I get output:
Two questions:
- Why isn't
pack()
working as expected in the first case? Shouldn't I see the full circle instead of seeing half of it? - Why did the oval turn into a triangle in the second output?