Common GUI methods and Examples
Frame:
Frame: Create a box
1 Frame f = new Frame ("this is the title of the box"); // create a box. The title is the title of the box.
// By default, the painting (Graphics g) method needs to be overwritten.
2. You can also write a class to inherit this Frame class;
Public class GUI extends Frame {
Public static void main (String [] args ){
GUI a = new GUI (); // create a box
// When instantiated, the first row of the parameter-free structure in the GUI is called, and the parameter-free structure in the parameter-free structure contains a super () by default. The parameter-free structure of the parent class is called, A Frame is created.
}
}
SetLocation:
SetLocation (): // set the frame position to the upper-left corner of the screen (that is, the screen origin ).
F. setLocation (100,100); // set the frame position to the standard (x, y) in the upper left corner of the screen)
SetSize:
SetSize (): // set the size of the box
F. setSize (200,200); // width and height (w, h)
SetBackground:
SetBackground (): // sets the background color of the box.
1 f. setBackground (Color. blue); // set the background Color of the box to blue.
2 f. setBackground (new Color (102,); // RGB: Red, Green, Blue-> three primary colors
SetResizable:
SetResizable (): // sets whether the size of the box can be changed or whether it can be extended or not.
F. setResizable (false); // boolean true/false
SetVisible:
SetVisible (): // whether the setting box is visible
F. setVisible (true); // if this parameter is left blank, the default value is false. Generally, you must set the value to either create a box or
SetBounds:
SetBounds (): // set the position and width and height x, y, w, h at the same time.
F. setBounds (444,444,222,222); // you can specify both the position and width and height.
Panel:
The default layout manager of Panel is FlowLayout floating layout.
1 Panel p = new Panel (); // create a widget
2 Panel p = new Panel (null); // you can directly set the layout in the widget.
3 Panel p = new Panel (new FlowLayout (FlowLayout. LEFT/RIGHT/CENTER ));
// Float LEFT to LEFT
// RIGHT float RIGHT
// Floating in the CENTER
P. setBounds (50, 50, 100,100); // set the x, y, w, and h of the widget! This setting is based on the upper left corner (origin) of the box, and f is the standard.
P. setBackground (new Color (200,200,255); // you can specify the background Color of the widget.
F. add (p); // add the small space to the Frame Box.
SetLayout:
SetLayout (): // sets the layout manager.
There are three layout managers:
1 FlowLayout (); // default value of the Panel
2 BorderLayout (); // The default Frame
3 GridLayout (); // applicable
FlowLayout:
1 The default layout manager of the Panel, but you can also use BorderLayout
Panel p = new Panel (new FlowLayout (FlowLayout. LEFT/RIGHT/CENTER ));
Panel p = new Panl (new BorderLayout (BorderLayout. EAST/WEST/NORTH/SOUTH/CENTER ));
// Float LEFT to LEFT
// RIGHT float RIGHT
// Floating in the CENTER
BorderLayout:
2 The default layout manager of the Frame, but you can also use FlowLayout
1 Frame f = new Frame (BorderLayout. EAST/WEST/NORTH/SOUTH/CENTER );
2 FlowLayout fl = new FlowLayout (FlowLayout. CENTER, 20, 40); // you can specify the layout mode, horizontal spacing, and Vertical spacing.
F. setLayout (fl );
// East-west and north-south Scaling
// North-South East-West Scaling
// Zoomed around
// EAST China
// WEST
// NORTH
// SOUTH of SOUTH China
// CENTER
GridLayout:
3 layout manager for both Frame and Panel
GridLayout (); // grid layout manager
F. setLayout (new GridLayout (); //
P. set (new GridLayout (2, 2); //
Button:
Button b1 = new Button ("Button"); // The Name Of The Button is enclosed in brackets.
Add the buttons to the Frame, and specify their positions through the constant (public static final XXX (uppercase:
1 f. add (b1, BorderLayout. EAST/WEST/NORTH/SOUTH/CENTER); // EAST (right), WEST (left), NORTH (upper), SOUTH (lower), middle
2 f. add (b1, "East/West/North/South/Center"); // East (right), West (left), North (upper), South (lower), middle
Pack:
Pack (): // whether to package, package all elements in the frame, and then open the frame according to the package size. f. setSize () will become invalid.
F. pack (); // use the elements in the frame to open the frame.
ActionListener:
// Create a listener:
1 The Listener class must implement the ActionListener Interface
2 The actionreceivmed (ActionEvent e) method in the ActionListener interface must be implemented.
AddActionListener:
// Bind the listener:
AddActionListener (); // bind the listener to the specified button to trigger the listener. By default, the method of actionreceivmed (ActionEvent e) is called.
B. addActionListener (m); // bind listener m to button B. In the following example,
SetActionCommand:
SetActionCommand (): // listener binding description
B. setActionCommand ("I am the first button ");
For example: // many simple attribute settings are not set.
Class Monitor implements ActionListener {// create a listener class to implement the ActionListener Interface
// Implement the actionreceivmed (ActionEvent e) method in the ActionListener Interface
Public void actionreceivmed (ActionEvent e ){
System. out. println ("I pressed this button ");
System. out. println (e. getActionCommand (); // obtain the listener's description.
}
}
Public class GUI {
Public static void main (String [] args ){
Frame f = new Frame ("name"); // create a box
Button B = new Button ("Button"); // create Button
Monitor m = new Monitor (); // instantiate the listener class
B. addActionListener (m); // bind the listener and upload the object
F. add (B); // add the button to the box
F. setVisible (true); // The box is visible.
}
}
TextField:
TextField (): The bound listener, triggered by the carriage return.
TextField t = new TextField (); // create a text box
TextField t = new TextField (10); // create a text box and enter up to 10
F. add (t); // add the text box to the Frame Box.
T. addActionListener (new TFActionListener (); // The Listener is triggered by the carriage return.
T. setEchoChar ('*'); // The password mechanism masks input text *
F. pack (); // The Frame is framed when the package contains content.
SetVisible (true); // sets the visibility
Class TFActionListener implements ActionListener {// create a listener
// Receives the event object, /// a class to access the local variables in the methods in another class, which is impossible
Public void actionreceivmed (ActionEvent e ){
// Get the source object of the event object: in one class, you want to access the event source object in another class, getSource ()
TextField tf = (TextField) e. getSource ();
// Pass the event source over and then perform a downward Transformation
// Force type conversion to TextField object and toString () Output
System. out. println (tf. getText (); // output the content of the text box.
// Set the content to null
Tf. setText ("");
}
}
GetSource:
GetSource (): Get the object of the event Source
TextField tf = (TextField) e. getSource (); // obtain the object of the event source from the code above, and then perform a downward transformation to access the unique attribute.
GetText:
T. getText (); // get the content of the text box
SetText:
T. setText (""); // assign a new value to the text box. "" is set to null.
SetEchoChar:
T. setEchoChar ('*'); // Replace the text entered in the text box with the "*" sign (only masking, what is the output, or what is it, * just not allowing direct display)
Label:
Label l = new Label ("Note:"); // create a Label
F. add (l); // add to Frame
Paint:
Paint (Graphics g); // this method is called by default when the Frame is created.
// Graphics: view the image as a paint brush and Frame as a canvas.
GetColor:
Color c = g. setColor (); // obtain the original Color of the paint brush.
SetColor:
G. setColor (Color. red); // you can specify the Color of the paint brush.
G. setColor (c); // It is used to wash the paint brush and change it to the original color.
FillOval:
G. fillOval (50, 55, 55, 55); // draw a solid elliptic (inner-cut elliptic), location, width and height
FillRect:
G. fillRect (44,44, 44,44); // draw a solid rectangle, position, width, and height.
DrawOval:
G. drawOval (,); // draw a hollow oval (inner tangent oval), position, width and height
DrawRect:
G. drawRect (,); // draw a hollow rectangle, position, width and height.
MouseAdapter:
F. addMouseListener (new listener class); // bind a mouse listener to trigger the listener. The mousePressed (MouseEvent e) method is called by default.
Bind a mouse listener:
1. The Listener class must inherit the class MouseAdapter.
2. It must be implemented (one of the methods is implemented here) mousePressed (MouseEvent e ).
Class Monitor extends MouseAdapter {
// MouseListener --> MouseAdapter --> Monitor (): If you directly implement the interface, you need to implement all the methods of the interface, but the MouseAdapter does not implement all the methods of the interface, we only need to write down the methods we need.
// MounseEvent: when an event occurs, the event SOURCE sends the time-related objects to our processing class through the listener.
// The monitor responds to the mouse-pressed event.
Public void mousePressed (MouseEvent e ){
// Get the event source object, who is the event source, Component, Frame is the event source, the essence is to get the reference of the time source object
MyFrame f = (MyFrame) e. getSource (); // get the reference of the event Source
F. addPoint (new Point (e. getX (), e. getY (); // Point is the coordinate of the getX () class, getY () mouse, taking this coordinate as the origin, and spending a vertex, then add the vertex to the Frame.
// Re-draw the Frame and all its elements
// Canvas re-painting mechanism: the canvas must be changed in status, size, display, or not, and so on.
// Repaint --> update --> paint (); force the screen to refresh and redraw the entire screen.
// Here, the paint () method is called through repaint to traverse the set and forcibly redraw each vertex. once clicked, it is sent from the memory to the screen, and the data is forcibly refreshed once.
F. repaint ();
}
}
WindowAdapter:
// Create a window listener
F. addWindowListener (new listener class); // bind a window listener to the Frame Box (close the listener)
Create a window listener:
1. The Listener class must inherit the abstract class WindowAdapter.
2. It must be implemented (one of the methods is implemented here) The WindowClosing (javaswevent) Method
F. addWindowListener (new MyWinMonitor (); // bind a window listener
Class MyWinMonitor extends WindowAdapter {// create a window listener class
Public void windowClosing (WindowEvent e) {// override method
SetVisible (false );//
System. exit (0); // disable jvm
}
// Anonymous internal class writing:
This. addWindowListener (
New WindowAdapter (){
Public void windowClosing (WindowEvent e ){
SetVisible (true );
System. exit (0); // disable jvm
}
}
);
KeyAdapter:
// Create a keyboard listener:
1. the keyboard listening class must inherit the KeyAdapter abstract class.
2. The keyPressed (KeyEvent e) method must be implemented.
F. addKeyListener (new listener class); // bind a keyboard listener to the frame Box
Class KeyMonitor extends KeyAdapter {
Public void keyPressed (KeyEvent e ){
Int keyCode = e. getKeyCode (); // getKeyCode
If (keyCode = KeyEvent. VK_UP) {// constant VK_UP I only set the UP key here,
System. out. println ("up --------");
}
}
}
GetKeyCode:
GetKeyCode (); // obtain the integer value corresponding to the key.
KeyEvent:
KeyEvent is a class. The constant VK_UP in it has the corresponding integer int value.