Java Study Notes-13. Create a window and program, and take notes-13

Source: Internet
Author: User

Java Study Notes-13. Create a window and program, and take notes-13

1. init () method: This method is called when a program is created for the first time and initialized for the first time.

Start () method: This method is called whenever a program piece enters the web browser and allows the program piece to start its general operations (the Special Program piece is stopped ); also called after init.

The paint () method is a part of the base class Component (the upper and lower levels of the inheritance structure ). Called as part of update () for special profiling of the canvas of the program piece.

Stop () method: each time the program piece leaves the sight of the web browser, the program piece can close costly operations, and also call before calling destroy.

Destroy () method: This method is called when a program is detached from the page.

2. event model: (1) Add the addXXXXXListener () method to the class.

(2) rewrite the method for executing the interface.

package thirteen;import java.awt.*;import java.awt.event.*;import java.applet.*;public class Button2New extends Applet {    Button b1 = new Button("button1"), b2 = new Button("button2");    public void init() {        b1.addActionListener(new B1());        b2.addActionListener(new B2());        add(b1);        add(b2);    }    class B1 implements ActionListener {        public void actionPerformed(ActionEvent e) {            getAppletContext().showStatus("BUTTon1");        }    }    class B2 implements ActionListener {        public void actionPerformed(ActionEvent e) {            getAppletContext().showStatus("Button2");        }    }}

3. Create a window: (1) create a Frame class in the main () method and initialize the struct class of the applet.

(2) inherit the WindowAdapter class and override the windowClosing () method.

(3) execute the setVisible () method of the Frame.

Package thirteen; import java. applet. *; import java. applet. *; import java. awt. borderLayout; import java. awt. button; import java. awt. textField; import java. awt. desktop. action; import java. awt. frame; import java. awt. event. *; import java. time. temporal. temporalQueries; import javax. swing. table. tableRowSorter; import org. omg. CORBA. floatSeqHelper; public class TextNew extends Applet {Button b1 = new Button ("Get Text"), b2 = new Button ("Set Text"); TextField t1 = new TextField (30 ), t2 = new TextField (30), t3 = new TextField (30); String s = new String (); public void init () {b1.addActionListener (new B1 ()); b2.addActionListener (new B2 (); t1.addTextListener (new T1 (); t1.addActionListener (new T1A (); t1.addKeyListener (new T1K (); add (b1 ); add (b2); add (t1); add (t2); add (t3);} class T1 implements TextListener {public void textValueChanged (TextEvent e) {t2.setText (t1.getText () ;}} class T1A implements ActionListener {private int count = 0; @ Override public void actionreceivmed (ActionEvent e) {// method stub automatically generated by TODO t3.setText ("t1 Action Event" + count ++);} class T1K extends KeyAdapter {public void keyTyped (KeyEvent e) {String tString = t1.getText (); if (e. getKeyChar () = KeyEvent. VK_BACK_SPACE) {if (tString. length ()> 0) {tString = tString. substring (0, tString. length ()-1); t1.setText (tString) ;}} else t1.setText (t1.getText () + Character. toUpperCase (e. getKeyChar (); t1.setCaretPosition (t1.getText (). length (); e. consume () ;}} class B1 implements ActionListener {public void actionreceivmed (ActionEvent e) {s = t1.getSelectedText (); if (s. length () = 0) s = t1.getText (); t1.setEditable (true);} class B2 implements ActionListener {public void actionreceivmed (ActionEvent e) {t1.setText ("Insert by Button2: "+ s); t1.setEditable (false); ;}} public static void main (String [] args) {TextNew applet = new TextNew (); frame aFrame = new Frame ("TextNew"); aFrame. addWindowListener (new WindowAdapter () {public void windowClosing (invalid wevent e) {System. exit (0) ;}}); aFrame. add (applet, BorderLayout. CENTER); aFrame. setSize (300,200); applet. init (); applet. start (); aFrame. setVisible (true );}}

4. JavaBean requirements:

(1) All classes must be placed in one package. No package exists in the web.

(2) All classes must be declared as public classes so that they can be accessed externally.

(3) All attributes of the class must be encapsulated and private declarations are used.

(4) If the encapsulated attribute needs to be operated by external entities, the corresponding setter and getter methods must be compiled.

(5) At least one construction method without parameters exists in a JavaBean.

5. An example of Swing borders:

package thirteen;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.border.*;public class Borders extends JPanel {    static JPanel showBorder(Border b) {        JPanel jPanel = new JPanel();        jPanel.setLayout(new BorderLayout());        String nm = b.getClass().toString();        nm = nm.substring(nm.lastIndexOf('.') + 1);        jPanel.add(new JLabel(nm, JLabel.CENTER), BorderLayout.CENTER);        jPanel.setBorder(b);        return jPanel;    }    public Borders() {        setLayout(new GridLayout(2, 4));        add(showBorder(new TitledBorder("Title")));        add(showBorder(new EtchedBorder()));        add(showBorder(new LineBorder(Color.blue)));        add(showBorder(new MatteBorder(5, 5, 30, 30, Color.green)));        add(showBorder(new BevelBorder(BevelBorder.RAISED)));        add(showBorder(new SoftBevelBorder(BevelBorder.LOWERED)));        add(showBorder(new CompoundBorder(new EtchedBorder(), new LineBorder(Color.red))));    }    public static void main(String[] args) {        Show.inFrame(new Borders(), 500, 300);    }     static class Show {        public static void inFrame(JPanel jPanel, int width, int height) {            String title = jPanel.getClass().toString();            if (title.indexOf("class") != -1)                title = title.substring(6);            JFrame frame = new JFrame(title);            frame.addWindowListener(new WindowAdapter() {                public void WindowClosing(WindowEvent e) {                    System.exit(0);                }            });            frame.getContentPane().add(jPanel, BorderLayout.CENTER);            frame.setSize(width, height);            frame.setVisible(true);        }    }}

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.