Java Applet Tips

Source: Internet
Author: User

Java applets have become more and more important in the application of the network because of their powerful functions, however, Java programming is not so simple, the so-called things at the beginning of the difficult, Java writing is also so, so I introduce some of the most basic Java application techniques for everyone to do a reference, hoping to be inspired by everyone. I'll show you some of the most basic techniques in Java usage, see the applet instance for examples related to these programs.

Display text

Read parameters

Response Mouse

Response keyboard

displaying image files

Play a sound file

Get Program Display space size

Set color

Display text

Display text is the most basic function in Java, and it is very simple to support the display of text, as long as the drawstring () function in class graphics is used. Let's look at the simplest HelloWorld examples:

//HelloWorld.java
import java.awt.*;
import java.applet.*;
public class HelloWorld extends Applet
{
String text="Hello World!";
public void paint(Graphics g)
{g.drawString(text,20,20);} //在坐标20,20处显示text的内容
}

This is the most basic Java applet, which only displays "Hello world!" when run ”。 Java supports Unicode, so Chinese can also be well displayed in Java, we put "Hello world! "Change into" the World Hello! ", You can also show (if it is not properly displayed, it is the browser's bug, I used the IE4.0 of this problem, please use Netscape)." It is noteworthy that each character in Java is represented by 16 bits instead of 8 bits, which is different from the C language.

Read parameters

Java applets can also read parameters from the page where the page is passed <param name= "??" Value= "??" > Pass parameters to the applet, the program should read the parameters in the initialization phase to complete the preparation of the program. Example:

//ShowText.java
import java.awt.*;
import java.applet.*;
public class ShowText extends Applet
{
String text;
public void init()//初始化函数
{
text=getParameter("word");//读入参数word
}
public void paint(Graphics g)
{g.drawString(text,20,20);}
}

So we should set word parameters in the page:

<applet code="ShowText.class" width="100" height="28">
<param name="word" value="Hello World!">
</applet>

The program can then read "Hello world!" from the argument "word" and then display it. Of course in the parameter settings, Word and word are the same, case-insensitive.

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.