/*
Chestnuts Learn about Swing
*/
Import javax.swing.*;
public class Test_swing extends JFrame {
Inherit JFrame top-Level container class (classes that can add other swing components)
JButton jb1 = null;
public static void Main (string[] args) {
Test_swing win = new test_swing ();
}
constructor function
Public test_swing () {
JB1 = new JButton ("button");
This.add (JB1);
This.settitle ("Hello Swing World");
This.setsize (500,500);
This.setlocation (500,150);
Set to ensure that the JVM also exits when the window is closed
This.setdefaultcloseoperation (Jframe.exit_on_close);
This.setvisible (TRUE);
}
}
"The three most common layout managers"
Layout Manager--Introduced
1Concept
Component in Container(Such asJFrame)In the location and?? is determined by the layout manager. All the containers will
Use a layout manager to automate the layout management of components.
2Kinds
JavaProvides a total ofFive layout managers:Streaming layout Manager (FlowLayout) boundary layout manager
(borderlayout) layout manager (GridLayout) card layout manager (cardlayout) , grid package cloth
Board manager (gridbaglayout) Boundary layout borderlayout-- introduction
Boundary layout ( BorderLayout) The container is simply divided into the cardinal and 5 jframe form, jdialog dialog box component default Layout method
boundary layout borderlayout-- caveats
1, not five parts must be added;
2 3jframe, jdialog The default layout manager is BORDERLAYOUT 
Import javax.swing.*;
Import java.awt.*;
/*
Chestnut Border Layout BorderLayout presentation
* 1, Inherit JFrame
* 2. Define the individual components you need
* 3. Creating components (components in constructors)
* 4. Add components
* 5, set the form
* 6. Display Form
*/
public class Test_swing extends JFrame {
JButton jb1,jb2,jb3,jb4,jb5;
Public test_swing () {
JB1 = new JButton ("Middle");
JB2 = new JButton ("North");
JB3 = new JButton ("East");
JB4 = new JButton ("South");
JB5 = new JButton ("West");
Adding individual components
This.add (JB1, Borderlayout.center);
This.add (Jb2,borderlayout.north);
This.add (Jb3,borderlayout.east);
This.add (Jb4,borderlayout.south);
This.add (jb5,borderlayout.west);
Set form Properties
This.settitle ("Border layout demo");
This.setsize (300,200);
This.setlocation (200,200);
Close the JFrame at the same time after exiting the form
This.setdefaultcloseoperation (Jframe.exit_on_close);
Show form
This.setvisible (TRUE);
}
public static void Main (string[] args) {
Test_swing win = new test_swing ();
}
}
three, often? Layout manager -- streaming layout
Flow-style layout flowlayout-- Introduction
FlowLayout Layout, placing the button component (or, of course, other components) in the order in which the components are added from left to right
In the container. When the boundary of the container is reached, the component is placed to the next?? In FlowLayout can be left right,
Alignment components in right-aligned pairs?
Import javax.swing.*;
Import java.awt.*;
/*
The Chestnut flow Layout FlowLayout presentation
* 1, Inherit JFrame
* 2. Define the individual components you need
* 3. Creating components (components in constructors)
* 4. Add components
* 5, set the form
* 6. Display Form
*/
public class Test_swing extends JFrame {
Defining components
JButton Jb1,jb2,jb3,jb4,jb5,jb6;
Public test_swing () {
JB1 = new JButton ("Guan Yu");
JB2 = new JButton ("Zhang Fei");
JB3 = new JButton ("Ma Chao");
JB4 = new JButton ("Jack Huang");
JB5 = new JButton ("Zhao Yun");
Jb6 = new JButton ("Wei Yan");
This.add (JB1);
This.add (JB2);
This.add (JB3);
This.add (JB4);
This.add (JB5);
To set the layout manager, the streaming layout defaults to in-play alignment
This.setlayout (New FlowLayout (Flowlayout.left));
Set form Properties
This.settitle ("Flow layout demo");
This.setsize (300,200);
This.setlocation (200,200);
This.setdefaultcloseoperation (Jframe.exit_on_close);
Prevent users from changing form size
This.setresizable (FALSE);
Show form
This.setvisible (TRUE);
}
public static void Main (string[] args) {
Test_swing win = new test_swing ();
}
}
"Java" "Graphics"