java-Layout and Supplemental Components
Basic Layout Method
a card layout-cardlayou
Two border layout-borderlayout the way of the cardinal
Three Mesh bag layout-gridbaglayout
Four split panel-jsplitpanel can only split two components, type unlimited
The
corresponding method can be queried in the API, the previous article provides a download link for the API
Supplemental Components
five tab Components-jtabbedpane
Six desktop Components-jdesktoppane
Seven internal form components-jinternalframe
implement a small program that leverages the card layout
Basic Ideas
Create 3 panels
Create a card
Add a card to one of the panels
Add two additional panels to the card, set the panel to a different color
Set up two buttons on the two panels on the card
button to add an event handling mechanism, click on one of the panel buttons to display the color of another panel
Add cards to the panel
Add a panel on a card
Add two buttons on the panel
Button set event handling mechanism
Click the button for panel conversion
Eg:package com.chengzhi.pkg1;import java.awt.cardlayout;import Java.awt.color;import java.awt.event.ActionEvent; Import Java.awt.event.actionlistener;import Javax.swing.jbutton;import Javax.swing.jframe;import Javax.swing.jpanel;public class Testgui extends JFrame implements actionlistener{JPanel P1, p2, p3; JButton B1, B2; CardLayout cardlayout;public Testgui () {p1 = new JPanel ();p 2 = new JPanel ();p 3 = new JPanel (); b1 = new JButton ("Show red Button"); B2 = new JButton ("Show Green Button"), CardLayout = new CardLayout ();p 1.setLayout (cardlayout);p 2.setBackground (color.red); P3.setbackground (Color.green);p 1.add ("Panel2", p2);p 1.add ("Panel3", p3);p 2.add (B2);p 3.add (B1); This.getcontentpane (). Add (p1); this.setsize (+); this.setvisible (true); This.setdefaultcloseoperation (Jframe.exit_on_close);//cardlayout.last (p1);//cardlayout.first (p1);// Cardlayout.show (P1, "Panel2"); B1.addactionlistener (this); B2.addactionlistener (this);} public static void Main (string[] args) {new Testgui ();} @Overridepublic VOID actionperformed (ActionEvent e) {JButton b = (JButton) e.getsource (); if (b = = B1) {//cardlayout.show (P1, "Panel2"); car Dlayout.next (p1); } if (b = = b2) {//cardlayout.show (P1, "Panel3"); Cardlayout.next (p1);}}}
java-Layout and Supplemental Components