Java learning notes-swing-Basic swing Program

Source: Internet
Author: User

Thinking in Java3rd solves the problem. Although there is a foundation for C/C ++, there are still many things in disorder. The best way to remember is to practice. For me, swing is actually the most attractive and insightful thing. Like bus lines, people who ride bicycles will not be familiar with it. Let's talk a little about my learning experience and so on.

Make the simplest one with only buttons in the text editing boxSwingRight:

 

Import javax. Swing .*;
Import java. AWT .*;
Import java. AWT. event. *; // listener

/***//**
* @ Author sosoayaen
**/
 
/***//**
* Root class named swingex
**/
Public class swingex extends jframe... {// jframe is a top-level container.
Private jbutton B1, B2; // two buttons
Private jpanel P1; // jpanel is also an intermediate container
Private jtextfield T1; // text editing area
/***//**
* Method swingex Constructor
*
* @ Param title the program title
*
**/
Public swingex (String title )...{
Super (title); // call the jframe constructor to pass the program title
B1 = new jbutton ("Click me! "); // The text displayed on the button. AWT can only display text, swing can even display graphs.
B2 = new jbutton ("beat me! ");
T1 = new jtextfield (30); // set it to 30 characters in length
T1.seteditable (true); // set T1 to an editable area.
P1 = new jpanel (); // create a jpanel instance
P1.setlayout (New flowlayout (); // you can specify the jpanel arrangement method.
P1.add (B2); // Add the button B2 to the jpanel.
P1.add (B1); // Add the button B1 to the jpanel.
P1.add (T1); // Add the text area T1 to the jpanel.

This. setsize (100,100); // pay attention to this, which refers to the current swingex, which inherits from jframe and sets the size.
This. setlayout (New flowlayout (); // you can specify the upper-layer container
// This. Add (B1); // you can directly add controls to the top-layer container.
This. Add (P1); // Add the jpanel container to the top-layer container.
}

/***//**
* Method main
*
* @ Param ARGs
*
**/
Public static void main (string [] ARGs )...{
Swingex Se = new swingex ("swing test"); // create an instance
Se. setvisible (true); // set to display. Show (Boolean) is an out-of-date API.
}
}

 

It's a very simple program, but it's also set by yourself. If you want to do something simple, you have to do something complicated. Let's take a look.SwingThe first step is to import the class library,Javax. Swing .*Of course, it must be imported,Java. AWT .*It is also essential,Java. AWT. event .*This class library is mainly used to manage events, such as events triggered when you press a button or change the content of the text area. It will be discussed later, and this is the most important thing, because all your event responses are inseparable from it. Direct inheritanceJframeI personally feel more convenient. At least I don't feel any trouble now. Well, it's a frame program, right? If the main class isJframeIsn't it quite reasonable? There are many containers, and there are top-level containers suchJframe, Intermediate containers suchJpanelAnd so on. You have to set the container size and the layout (layout), and then you just need to add the control or container through the add (Control) method to add the control or container. Note that the middle container cannot be added to the top-level container. I guess that although it has not been tested, it is unreasonable to think about it. The last thing to do is to inheritJframeInstance setvisible (true.
 

 

Related Article

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.