I just learn Swing and create a sample GUI for myself as a practice. Very simple, it has three panels on the western, central, and eastern sides of the JFrame . On the last panel there is another panel and a button. The panel is an extension of JPanel , DrawPanel . He dials a random Rectangle every time the button below it is pressed. This is my code:
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Tester { public static DrawPanel panelEastDrawPanelCenter; public static void main() { Tester gui = new Tester(); gui.go(); } public void go() { JFrame frame = new JFrame("This is the title"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
The problem I am facing is that although I explicitly setSize() for the DrawPanel object ( panelEastDrawPanelCenter ) is 50 and 50 times when I run the program, it ( DrawPanel ) still represents a small panel next to the button and the East panel (DrawPanel container and button) still have the same width (and will never expand). I understand what I can say
frame.pack(); frame.setSize(frame.getWidth(), 500);
if I set the east panel to use either the BorderLayout or BoxLayout layout, and that will make the DrawPanel display bigger.
- But I donβt understand why setting the size of the
DrawPanel object does not actually change its size and why it remains small no matter what I do setSize(50,50); - How do I make the middle panel stop resizing so huge that the East panel can change on a larger scale? Or how to resize the east panel?
source share