I have this little bug I haven´t been able to handle. I set the size of the JPanel to be (150,90), but the code in the paintComponent() show its (160,100). Whats wrong? Thanks for your time and help.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test2 extends JPanel{
public static void main(String[] args) {
JFrame window = new JFrame("Test");
Test2 content = new Test2();
window.setContentPane(content);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLocation(120,70);
window.pack();
window.setResizable(false);
window.setVisible(true);
}
Test2(){
setBackground( new Color(0,120,0) );
setPreferredSize( new Dimension(150, 90));
repaint();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor( Color.YELLOW);
g.drawRect(0, 0, 150, 90);
System.out.println(this.getWidth());
System.out.println(this.getHeight());
}
}
The Console output indicates that the size is (160,90). The yellow rectangle shows the same.