Java Basic Knowledge Learning (ix)

Source: Internet
Author: User

GUI Development

Previously, the GUI program was written in Java using the Abstract Window Toolkit, AWT (abstract Windows Toolkit). Now use swing more. Swing can be seen as an improved version of AWT rather than a substitute for AWT, which is an improvement and extension of AWT. So, when writing GUI programs, both swing and AWT work. They coexist in the Java Foundation Class (Java Foundation CLASS,JFC).

AWT relies on the main platform to draw user interface components, and swing has its own mechanism to draw and manage interfaces in the windows provided by the main platform. The most obvious difference between swing and AWT is the look and feel of the interface components, and AWT runs the same program on different platforms, and there may be some differences in the appearance and style of the interface. However, a swing-based application may have the same look and feel on any platform.

The classes in swing are inherited from AWT, and some swing classes directly extend the corresponding classes in AWT. For example, JApplet, JDialog, JFrame, and JWindow.

Using swing to design a graphical interface, the main introduction is two packages:

    • Javax.swing Package: Contains the basic classes of swing;
    • Java.awt.event Package: Contains interfaces and classes related to handling events.

Import javax.swing.*; Public classExample1_1 { Public Static voidMain (string[] args) {JFrame MW=NewJFrame ("Demo Form"); Mw.setsize ( -, $); JButton Button=NewJButton ("Demo Button");         Mw.getcontentpane (). Add (button); Mw.setvisible (true); }}

Graphical interface development using a variety of space, it is best to find examples on the site, write a demo

http://www.weixueyuan.net/java/rumen_11/

Network programming

Java provides a series of classes that support Java programs to access network resources.

Understand the network protocol first

Object for inetaddress class for IP address and domain name

 Public Static voidMain (string[] args) {Try{inetaddress addr= Inetaddress.getbyname ("www.weixueyuan.net"); String Domain=Addr.gethostname (); String IP=addr.gethostaddress (); System. out. println (domain); System. out. println (IP); } Catch(unknownhostexception e) {e.printstacktrace (); }}

URL Class

The Uniform Resource Locator URL (Uniform Resource Locator) is the name and address used to identify a resource when the WWW client accesses the Internet.

URL url; Try {    new URL ("http://www.weixueyuan.net:80/"catch  (malformedurlexception e) {    e.printstacktrace ();}

To receive and send off the information to use the URLConnection class

New URL ("http://www.weixueyuan.net:80/"= Url.openconnection ();

The following methods are provided to get the stream object and implement the network connection:

    1. Getoutputstream (): Obtains the OutputStream stream object which sends the information to the remote host;
    2. getInputStream (): Gets the InputStream stream object that obtains information from the remote host. With the input and output stream of the network connection, the program can realize the remote communication;
    3. Connect (): Set up a network connection.

Example: Reading a URL resource

Import java.io.*; import java.net.*; import java.awt.*; import java.awt.Event.*; import javax.swing.*; import Javax.swing.jtextarea;import Javax.swing.JTextField; Public classExample1_1 { Public Static voidMain (string[] args) {NewDownnetfile (); }}classDownnetfile extends JFrame implements ActionListener {JTextField infield=NewJTextField ( -); JTextArea Showarea=NewJTextArea (); JButton b=NewJButton ("Download"); JPanel P=NewJPanel (); Downnetfile () {Super ("Read Network text file application"); Container Con= This. Getcontentpane ();        P.add (infield);        P.add (b); JScrollPane JSP=NewJScrollPane (Showarea); B.addactionlistener ( This); Con.add (P," North"); Con.add (JSP,"Center");        Setdefaultcloseoperation (Jframe.exit_on_close); SetSize ( -, -); SetVisible (true); }     Public voidactionperformed (ActionEvent e) {Readbyurl (Infield.gettext ()); }     Public voidReadbyurl (String urlname) {Try{Showarea.settext (""); URL URL=NewURL (urlname); URLConnection TC=url.openconnection ();            Tc.connect (); InputStreamReaderinch=NewInputStreamReader (Tc.getinputstream ()); BufferedReader BR=NewBufferedReader (inch);            String Line;  while(line = Br.readline ())! =NULL) {showarea.append ( line+"\ n");        } br.close (); } Catch(malformedurlexception e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }    }}

Socket

Http://www.weixueyuan.net/view/6080.html

Database

Http://www.weixueyuan.net/view/6081.html

Curd

The Java Foundation temporarily learns these, and then learns some Java Web development, and then makes a thorough study of the problems encountered.

Java Basic Knowledge Learning (ix)

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.