Ok so these layout managers are driving me crazy. I am trying to create a Tetris program but my **** keeps on getting resized, and once I fix one thing something else gets moved. I created a simple program that encapsulates my problems:
package BorderTest; import java.awt.*; import javax.swing.*; import javax.swing.border.*; public class BorderTest extends JFrame { public BorderTest() { //setLayout(null); setSize(300, 676); BorderPanel borderPanel = new BorderPanel(); add(borderPanel); setVisible(true); } public static void main(String[] args) { new BorderTest(); } } class BorderPanel extends JPanel { public BorderPanel() { setSize(300, 676); Border b = BorderFactory.createLineBorder(Color.BLACK, 10); this.setBorder(b); setVisible(true); } public void paintComponent(Graphics g) { Graphics draw = g; draw.fill3DRect(10, 150, 40, 40, true); draw.fill3DRect(10, 635, 41, 41, true); } }
When the program is compiled and run, you can see that although the size is set to 300x676 the second rectangle does not show unless the bottom is extended, meaning my JFrame height is somehow getting changed to 640ish. I tried setting the layout to null to see if that helped, but the height is still wrong and now the border is messed up. What is causing these changes, how can I fix it, and why must layouts be so difficult?