Java Learning Notes (8)-java GUI design and event handling __java

Source: Internet
Author: User
Tags event listener stub
Eighth chapter Java GUI design and event handling 1. Topic 1.1, how to make a graphical interface. What packages it needs to introduce

Creates a window and adds various components to the window, specifying the properties of the component and their position in the window, which makes the graphical interface look good. Define the events of the graphical interface and the responses of various components to the different events, so as to realize the interaction between the graphical interface and the user.
In short, a graphical interface requires the introduction of a Java development package, which then uses the encapsulated functions and classes to write its own classes to inherit them and then construct a series of things in the interface and interface.
Using the interface operation to introduce the AWT package, swing package and so on, select the corresponding content according to the need. 1.2, in Java, what is an event. Describe the mechanism for handling events.

An event is an action that can be recognized by a control. The event object that is bound to the event source listens to the event source, and when the event source has an action, it runs the processing event with which it is bound.
Event refers to an object that describes the state of the event source change. The Java language handles events using the authorization event model. In this model, each component has a corresponding event, such as a button click event, a text field with content change events, and so on. When an event is triggered, the component sends the event to each event listener that the component registers, and the event listener defines the handling of events corresponding to different events, at which point the event listener invokes different event handlers based on different event information to complete the handling of this event. Only event listeners will receive event information when they are triggered. 1.3, what is the layout manager, what role it has.

A layout manager is an instance of a class that controls the position of a component in a container. The effect is to arrange the controls to be arranged according to the programmer's needs.
The layout manager is an instance of a class that implements the Layout manager interface. The role of the layout manager includes setting the best and smallest dimensions of the calculation container and arranging the size and location of the components in the container appropriately. 1.4, please write a window to achieve the BorderLayout layout.

 package xiti8 import java.awt.BorderLayout; import Java.awt.Button; import java.awt.Frame; import Java.awt.Panel;
    Class Borderlayoutframe extends frame{Borderlayoutframe () {super ("BorderLayout Layout window test");
    Panel mypanel = new Panel ();
    Mypanel.setlayout (New BorderLayout ());
    button north = New button ("Northern");
    Button south = New button ("South");
    button east = New button ("Eastern");
    button west = New button ("Western");
    Button Center = new button ("Central");
    Mypanel.add (North,borderlayout.north);
    Mypanel.add (South,borderlayout.south);
    Mypanel.add (East,borderlayout.east);
    Mypanel.add (west,borderlayout.west);
    Mypanel.add (Center,borderlayout.center);
    This.add (Mypanel);
    This.setsize (320,240);
This.setvisible (TRUE); } public class Xiti8_4 {public static void main (string[] args) {//TODO auto-generated the method stub Borderlayo
    Utframe borfrm = new Borderlayoutframe ();
Borfrm.show (); }
}

1.5, please explain the characteristics of the FlowLayout layout mode.

The FlowLayout layout is the default layout for JPanel, components are arranged from left to right in the order in which they are added, and then entered the next line, the default center arrangement, the default horizontal and vertical clearance between components is 5 pixels, and the component size defaults to the optimal size, which guarantees that the text above it is displayed ...
The FlowLayout layout policy arranges the components in the container to be ordered from left to right in the order in which they are added. If one row is full and the next row continues, each row takes a center alignment. 1.6, GridLayout layout characteristics of which.

The GridLayout layout is a grid area that divides the container into rows by several columns, and the components are located in these partitioned compartments, each of which is the size of the grid. 1.7, the preparation of "drawing Square" procedure

Ashamed, this problem wrote to write out of chaos, and then refer to the great God wrote, attached to the Portal: Draw Square
http://blog.csdn.net/Jayler_May/article/details/72825582

Package xiti8;
Import java.awt.*;
    Import java.awt.event.*;

Import javax.swing.*;
    public class Myhuatuframe extends jframe{myhuatuframe (String str) {super (str);
This.setvisible (TRUE);
    This.setdefaultcloseoperation (jframe.exit_on_close);/Set Close window This.setsize (400, 400);
    Mypanel Myhuatupanel = new Mypanel ();//Create Panel//Myhuatupanel.setbackground (Color.yellow);
    This.add (Myhuatupanel,borderlayout.center);
Myhuatupanel.addmouselistener (Myhuatupanel);//Add Mouse monitor this.setdefaultcloseoperation (jframe.dispose_on_close); Class Mypanel extends JPanel implements mouselistener{//static int x,y; int clickednum = 0; @Override public void mo
    useclicked (MouseEvent e) {//TODO auto-generated method stub int X = E.getx ();
    int Y = E.gety ();
        Clickednum = E.getbutton ()///Get the number of mouse clicks Graphics g = this.getgraphics ();//Redraw if (Clickednum = 1) {//If the left button is clicked
            if (e.getclickcount () = = 2) {//If the left arrow g.setcolor (Color.White) is double-clicked; //G.drawoval (X, Y, 30, 30);
        G.fillrect (0,0,1000,1000);
            else if (e.getclickcount () = = 1) {G.setcolor (color.green);
            G.drawoval (X, Y, 30, 30);
        G.filloval (x,y,10,10);
        } else if (Clickednum = 3) {//If the right button is clicked G.setcolor (color.red);
        G.drawrect (x,y,30,30);
    G.fillrect (x,y,10,10);
}//To the mouse of the various events of the rewrite, automatically added, or the error, with Super a truth ... @Override public void mouseentered (MouseEvent arg0) {//TODO auto-generated a stub} @Override public void MoU  seexited (MouseEvent arg0) {//TODO auto-generated method stub} @Override public void mousepressed (MouseEvent arg0) {//Todo auto-generated method stub} @Override public void mousereleased (MouseEvent arg0) {//Todo Auto-gene  Rated method stub}///}//public class Xiti8_7_2 {public static void main (string[] args) throws Exception {// TODO auto-generated method Stub myhuatuframe frm = new Myhuatuframe ("Draw Square");
    Frm.show ();
 }
}
//}

 Read:
 "Simple and Simple MFC", Hou Junjie, Nineth chapter.

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.