So far we have been in touch with the Java command line, but in order for the user to use, there must be a graphical interface, so this chapter is mainly about how to use the GUI (graphical user interface)
Create a gui:javax.swing.*the layout-related content in this chapter is the content of the javax.swing package, the common components are jframe,jbutton,jradiobutton,jcheckbox,jlabel,jlist,jscrollpane,jslider,jtextarea,j Textfield,jtable, etc. Create GUI First step: Create window (JFrame)Jframe:gui from the Creation window, JFrame represents the Window object on the screen, and the button and text can be placed on the window, soYou must first use the Frame object before you can use the object's Getcontentpane (). Add () method adds a component to the windowJFrame frame = new JFrame (); creating a GUI second step: Create a component (for example, a button)JButton: Defines a button, which can have a name and need to be functional. JButton button=new JButton ("click Me"); Create GUI Step three: Add components to the frameOn The window you can add a button like this:Frame.getcontentpane (). Add (button);Frame frame cannot add components directly, you must add components through the panel creating a GUI Fourth step: Show FrameTo display JFrame, you must define the size of the frame and whether it is visible as truerame.setsize (300,300);
frame.setvisible (true); wiget: Buttons, forms, radio button, etc. are all a widget Some widgets can draw graphics, such as graphics objects. All of the code for the drawing program is in JPanel's paintcomponent (Graphics g), which invokes the drawing image when loading the frame of the Panel or Changing window windows, unless redrawn with reaping (). Otherwise, no call drawing is madePaintcomponent's parametric graphics are actually a graphics2d type
LayoutA frame has 5 zones by default (the BorderLayout layout manager we described below), and if no region is specified, the widget is added to the intermediate areaThe following example adds a button in an intermediate position:Frame.getcontentpane (). Add (Borderlayout.center,button); the five layouts for frame are as follows:
Swingcomponent (component, or component): something that is placed above the GUI that the user can see and interact with, such as button, Text field, and so on, all components inherit from Java.swing.JComponent
Layout ManagerThe layout manager is used to control the display of the associated components, with multiple types of layout managers, each of which can customize the layout management rulesthe inner panel or component is controlled by the outer panel
three layout managers with a wide range of applicationsBorderLayout: The default layout manager for the framework. The default 5 zones, each zone can only be one component (the layout manager is the example of the above layout cited), of course, if it is a panel component, the Panel can also put componentsthere are eastern Borderlayout.east, western Borderlayout.west, southern Borderlayout.south, North Borderlayout.north, The Central district Borderlayout.center five areas. South and North: Height can be decided by itself, but the width cannot be decided by itself;East and West: Width can be decided by itself, but height cannot be decided by itself;Central: Can only pick up the rest of the place FlowLayout: The default layout manager for the panel. Display components from left to right (sorted by join time), show no Line wrapping BoxLayout: The default is arranged in a vertical direction, and a single line shows a component (in order of accession). It can also be displayed in rows, but not wrapped, forcing a line break through a line break mechanism. do not write the steps of swing, go to the API according to the desired components, such as manipulating the text field, etc.
Monitor button execution action: java.awt.event.*in Java.awt.event This package, there is a set of event classes (with names with event), such as MouseEvent, KeyEvent, WindowEvent, ActionEvent, and so on. At the same time, the event class will have a corresponding listener interface, such as MouseListener A button performs a function that requires three points to be implemented:1. Button bindings. 2. Listen for the event that the button was pressed. 3. The method that implements the listener interface (which defines the method of event handling) passes the parameters of the event object is a method used by an event source and listener interface. An event event (such as a mouse down) is generated when the source is manipulated, and each event class (mouse action, etc.) corresponds to a listening interface (MouseListener, etc.). We need to implement this listener interface, define all the methods that the mouse event class might correspond to, and pass an event to the listener. The event source requires a binding to register the listener, such as the addActionListener (listener), so that the corresponding event is executed when the listener's event occurs. Note:each component can trigger events that are not exactly the same, and each event source component has its own event. all event source bindings listen for events in the Addxxxlistener structure.an Event event argument is passed to the method that implements the listener interface, which can be obtained by the Event.getsource () method (the event source object name) here is a button to register the listener that implements the ActionListener interface, listen for the button's ActionEvent event, and execute an example of the corresponding actionperformed method
Inner classWhat happens when multiple buttons need to listen for the same event but want to perform different methods for different buttons? If you use GetSource () to distinguish which button event source, can be implemented, but not very good, if you use the external class class and no way to manipulate the frame on other classes. While the inner class can solve this problem (define multiple inner classes in the outer class to implement the Listener interface, different buttons bind different listener classes)
Inner class: One class is nested within another class, and an inner class can use all external methods and variables (either private or not)class myouterclass{Class myinnerclass{//Internal classesvoid Go () {} }} an inner class can only create instances of an inner class from an instance of an external class, and each inner class instance is a corresponding instance of the current outer class instance one by one.There are two ways to create an internal class instance from an instance of an external class:1. An internal class is instantiated in the code of an external class2. Instantiate the external class first, and then the reference type of the inner class instance is "outer classes. Inner class", creating an instance by "external class instance. New inner Class ()"myouter outerobj=new myouter ();Myouter.myinner innerobj=outerobj.new Myinner ();
Java Fundamentals VII [graphical user interface] (read head first Java record)