Java Swing simplest example (2) put a container or component in the JFrame

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.