How To Set Panel Size In Java
JPanel - Part ii
JPanels are non only used as a layer for the Content Pane, they are a generic container for widgets and tin can do all sorts of things. In the next part of this tutorial, we will show you how to add widgets to the Content Pane and teach you bones manipulation of a JPanel.
Now information technology is fourth dimension to actually add something to the Window that you tin see! "At terminal!" I hear y'all yell. This procedure is very simple. Let's wait at the example previously and add together some coloured panels to the content pane.
1 two 3 4 5 6 7 8 9 ten 11 12 13 14 15 xvi 17 xviii 19 20 21 22 23 24 25 26 27 28 29 xxx 31 32 33 34 35 36 37 38 39 xl 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | import javax.swing.*; import java.awt.Color; public class PanelExample_Extended{ public JPanel createContentPane ( ) { JPanel totalGUI = new JPanel( ); totalGUI.setLayout( naught ); JPanel redPanel = new JPanel( ); redPanel.setBackground(Color.red); redPanel.setLocation( one 0, 1 0 ); redPanel.setSize( v 0,v 0 ); totalGUI.add(redPanel); JPanel bluePanel = new JPanel( ); bluePanel.setBackground(Color.blue); bluePanel.setLocation( 2 2 0, 1 0 ); bluePanel.setSize( 5 0, five 0 ); totalGUI.add(bluePanel); totalGUI.setOpaque( true ); return totalGUI; } private static void createAndShowGUI( ) { JFrame.setDefaultLookAndFeelDecorated( true ); JFrame frame = new JFrame("[ = ] There's 3 JPanels in hither! [ = ]"); PanelExample_Extended demo = new PanelExample_Extended( ); frame.setContentPane(demo.createContentPane( ) ); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize( 2 9 0, one 0 0 ); frame.setVisible( true ); } public static void main(Cord[ ] args) { SwingUtilities.invokeLater( new Runnable( ) { public void run( ) { createAndShowGUI( ); } } ); } } |
When run (and please attempt to run this yourself) the screen should look a scrap similar this...
The but lawmaking nosotros take changed is the code within the method of createContentPane().
To first with, nosotros create a JPanel object. This will exist the lesser JPanel, the base layer on the GUI that we add together things to.
9 | JPanel totalGUI = new JPanel( ); |
JPanels are normally controlled by an interface called the LayoutManager. It helps yous apace place your widgets in a diverseness of ways, and we embrace how to use the LayoutManager later on in the tutorials. For now, I desire to place my widgets manually, so I take turned the LayoutManager on the Content Pane off. This is done by setting the layout to zero.
13 | totalGUI.setLayout( nix ); |
We create a JPanel object and called it redPanel.
17 | JPanel redPanel = new JPanel( ); |
We alter the background colour to red, and requite the JPanel a size using setSize(width, height).
We also give the widget a location using setLocation(ten pixels, y pixels). This sets the top left hand corner of the widget to the specified position.
For reference, location is set assuming the height left paw corner of the Content Pane is position (0,0).
18 19 20 | redPanel.setBackground(Colour.cerise); redPanel.setLocation( ane 0, 1 0 ); redPanel.setSize( 5 0,5 0 ); |
To add together this to the lesser JPanel, we simply apply the syntax add(); If you practice non add the widget to the Content Pane, information technology volition not be displayed.
21 | totalGUI.add(redPanel); |
We repeated this process again, adding a blue JPanel to the other terminate of the Content Pane.
23 24 25 26 27 | JPanel bluePanel = new JPanel( ); bluePanel.setBackground(Color.blue); bluePanel.setLocation( two 2 0, one 0 ); bluePanel.setSize( v 0, five 0 ); totalGUI.add(bluePanel); |
Finally we set the JPanel to opaque earlier returning the bottom JPanel and then that it can be fix as the content pane. This bottom JPanel is re-sized automatically to fit all the widgets positioned on information technology.
30 31 | totalGUI.setOpaque( true ); return totalGUI; |
So now both JPanels have been added to the bottom JPanel, and the bottom JPanel has been made the Content Pane! Like shooting fish in a barrel peasy. Nosotros volition use this process to add together things to JPanels and the Content Pane throughout the residue of the tutorials.
Several things to note hither. JPanels are containers, and you tin can nest them. If you have a ready of widgets that need to exist defined in a certain order and stay like that, place them in a JPanel. That way you tin can move that JPanel around the screen without having to change the order or positioning of the widgets within that JPanel.
Please conduct in mind that JPanels should not actually be used for drawing on a screen. Nosotros colour them hither and so it'south easy for yous to become to grips with the main points of positioning and sizing an object. There is a dissever Graphics2D package for drawing in Swing that will exist covered in a more avant-garde tutorial.
To confirm yous know what to do, and to get you thinking, here are three elementary exercises to carry out.
i) Colour the bottom JPanel (totalGUI) and then you can meet its location and size.
two) Set up the visibility of the bottom JPanel (totalGUI) to false.
3) Attempt adding a green and a yellow JPanel to the JFrame.
Hint for Exercise #1
Hint for Exercise #2
Hint for Exercise #three
Practise #1 Example Lawmaking
Practise #2 Example Lawmaking
Exercise #iii Case Code
Before nosotros movement on, reply these simple questions on the JPanel
Y'all are now wise in the ways of the JPanel. The next tutorial, JLabel, should be simple for y'all.
How To Set Panel Size In Java,
Source: https://www.macs.hw.ac.uk/cs/java-swing-guidebook/?name=JPanel&page=2
Posted by: negrinsaysix.blogspot.com
0 Response to "How To Set Panel Size In Java"
Post a Comment