There are two kinds of things that can be put into jframe, containers and components. You can see these clearly in the graphical interface of Jigloo.
Common containers (containers) have these:
JFrame interface with caption and Close button
JPanel Panel
JScrollPane panel with scroll bar
JSplitPane Panel with sub-columns
JTabbedPane Panel with TAB structure
JDialog dialog box
These are the common components (component)
JButton button
JLabel label (used to display fixed, unchanging text
JTextField text box (for users to write text)
Jradiobutton Radio Box
Jckeckbox selection Box
JComboBox drop-down box
JTable form
The method of adding an ordinary container or component is the same, it is a two step, created, and then added in. We perform these two operations on the basis of the example (1), adding a component to an empty JFrame
Package Basiccompoment;import Javax.swing.jbutton;import Javax.swing.jframe;import javax.swing.SwingUtilities; Import Javax.swing.windowconstants;public class AddComponent extends Jframe{public addcomponent () {Initgui ();} private void Initgui () {setvisible (true); SetSize (300,400); Setlocationrelativeto (null); Setdefaultcloseoperation ( Windowconstants.dispose_on_close);//Create a component JButton JButton1 = new JButton ("JButton1");//Add component (JButton1);} public static void Main (string[] args) {Swingutilities.invokelater (new Runnable () {public void run () {addcomponent F = new AddComponent ();}});}}
There is no layout, just add the component, so it shows a button that covers the entire frame.
In general, you can set an object as a private member, create it when you need it, add it when you need it, and make it easier to manipulate components in different functions. It is important to note that all objects must be initialized before they can be joined, and each object can only be placed in one place. Even two objects that have exactly the same form of expression must be new two, and trying to place a button in two places is not going to work.
How to set a component as a private member
Package Basiccompoment;import Javax.swing.jbutton;import Javax.swing.jframe;import javax.swing.SwingUtilities; Import Javax.swing.windowconstants;public class AddComponent2 extends jframe{//define variable private JButton jbutton1;public AddComponent2 () {Initgui (); Addcomp ();} private void Addcomp () {//Initialize JButton1 = new JButton ("JButton1");//Add component (JButton1);} private void Initgui () {setvisible (true); SetSize (300,400); Setlocationrelativeto (null); Setdefaultcloseoperation ( Windowconstants.dispose_on_close);} public static void Main (string[] args) {Swingutilities.invokelater (new Runnable () {public void run () {AddComponent2 F = ne W AddComponent2 ();}});}}
Java Swing simplest example (2) put a container or component in the JFrame