Create gui program java
ActionEvent; import java. ActionListener; import javax. JButton; import javax. JFrame; import javax. SOUTH, button ; button. Improve this answer. Bala R Bala R k 22 22 gold badges silver badges bronze badges.
They say this is a must because Swing is not thread-safe? Crozin Crozin Plz no. Then open it an type import java.
So inside that you need to put createWindow ; As this is what you called your function above and it is calling the function, you do not need to call this function as it does it when you run the program. Netbeans also has project templates that you can use to quickly get started. Each time you click the button, the counter's value increases by 1.
Dissecting the AWTCounter. In other words, this class AWTCounter is a Frame , and inherits all the attributes and behaviors of a Frame , such as the title bar and content pane. Lines 11 to 47 define a constructor, which is used to setup the GUI components and event handlers.
In Line 13, the setLayout inherited from the superclass Frame is used to set the layout of the container. FlowLayout is used which arranges the components in left-to-right and flows into next row in a top-to-bottom manner. A Label , TextField non-editable , and Button are constructed.
We invoke the add method inherited from the superclass Frame to add these components into container. In Line , we invoke the setSize and the setTitle inherited from the superclass Frame to set the initial size and the title of the Frame. The setVisible true method Line 42 is then invoked to show the display. Line is used to setup the callback event-handler, which will be discussed in length later.
In brief, whenever the button is clicked, the actionPerformed will be called. In the actionPerformed Lines , the counter value increases by 1 and displayed on the TextField.
The constructor is executed to initialize the GUI components and setup the event-handlers. The GUI program then waits for the user action. For example, if we insert the following code before and after the setvisible : System.
Frame Line 6 - the top-level window container. In the constructor Line 14 , we constructs 4 components - 2 anonymous java. Label s and 2 java. TextField s. The Frame adds the components, in GridLayout. The listener class needs to implement ActionListener interface and provides implementation to method actionPerformed.
Callback Methods In the above examples, the method actionPerformed is known as a callback method. JavaScript can attach a Callback method to an Event Directly In some languages, you can directly attach a method or function to an event such as mouse-click.
The sequence of steps is illustrated above: The source object registers its listener s for a certain type of event. The source is triggered by a user. The source create a XxxEvent object, which encapsulates the necessary information about the activation. For example, the x, y position of the mouse pointer, the text entered, etc. Finally, for each of the XxxEvent listeners in the listener list, the source invokes the appropriate handler on the listener s , which provides the programmed response.
The listener s is required to implement ActionListener interface, and override the actionPerformed method to provide the response. In Line , we write an inner class called BtnCountListener , which override the actionPerformed to increment and display the count.
An inner class is a class defined inside an outer class, and it can access the private entities of the outer class. We will elaborate on the inner class in the next section. The source object registers listener via the addActionListener. The ActionEvent listener is required to implement the ActionListener interface, and override the actionPerformed method to provide the programmed response upon activation.
We identify the super Frame as the source object. It is required to implement the WindowListener interface, which declares 7 abstract methods: windowOpened , windowClosed , windowClosing , windowActivated , windowDeactivated , windowIconified and windowDeiconified. We override the windowClosing handler to terminate the program using System. We ignore the other 6 handlers, but required to provide an empty body for compilation.
The sequence diagram is as follow: Example 4: MouseEvent and MouseListener Interface A MouseEvent is fired when you press, release, or click press followed by release a mouse-button left or right button at the source object; or position the mouse-pointer at enter and away exit from the source object. To demonstrate the MouseEvent : We identity super Frame as the source object.
It is required to implement the MouseListener interface, which declares 5 abstract methods: mouseClicked , mousePressed , mouseReleased , mouseEntered , and mouseExit. We override the mouseClicked to display the x, y coordinates of the mouse click on the two displayed TextField s. We ignore all the other handlers for simplicity - but you need to provide an empty body for compilation.
We override the mouseMoved to display the x, y position of the mouse pointer. We ignore the MouseDragged handler by providing an empty body for compilation. Example 6: KeyEvent and KeyListener Interface A KeyEvent is fired when you pressed, released, and typed pressed followed by released a key on the source object.
The KeyEvent listener needs to implement the KeyListener interface, which declares 3 abstract methods: keyTyped , keyPressed , keyReleased. We override the keyTyped to display key typed on the display TextArea. We ignore the keyPressed and keyReleased. An example is as follows: import java. What are Inner classes? That is, it could contain constructors, member variables and member methods. You can create an instance of a nested class via the new operator and constructor.
A nested class is a member of the outer class, just like any member variables and methods defined inside a class. This is the property that makes inner class useful. A nested class can have private , public , protected , or the default access, just like any member variables and methods defined inside a class. A private inner class is only accessible by the enclosing outer class, and is not accessible by any other classes.
A nested class is NOT a subclass of the outer class. That is, the nested class does not inherit the variables and methods of the outer class. It is an ordinary self-contained class. The nested class, being defined inside an outer class, can access private members of the outer class. To place a piece of class definition codes closer to where it is going to be used, to make the program clearer and easier to understand.
For namespace management. An anonymous instance of the BtnCountListener inner class is constructed. The btnCount source object adds this instance as a listener, as follows: btnCount. Inner class provides a much cleaner solution! Example 8: An Anonymous Inner Class as Event Listener Instead of using a named inner class called BtnCountListner in the previous example , we shall use an inner class without a name, known as anonymous inner class as the ActionListener in this example. An anonymous instance of an anonymous inner class is constructed, and passed as the argument of the addActionListener method as follows: btnCount.
It is local to the method and cannot be marked with access modifier such as public , private or static , just like any local variable of a method. An anonymous inner class must always extend a superclass or implement an interface.
The keyword " extends " or " implements " is NOT required in its declaration. An anonymous inner class must implement all the abstract methods in the superclass or in the interface. An anonymous inner class always uses the default no-arg constructor from its superclass to create an instance. If an anonymous inner class implements an interface, it uses the java. Example Using the Same Listener Instance for All the Buttons If you use the same instance as the listener for all the 3 buttons, you need to determine which button has fired the event.
Using ActionEvent 's getActionCommand In the following example, we use the same instance of a "named" inner class as the listener for all the 3 buttons. Downcast back to Button. Layout Managers and Panel A container has a so-called layout manager to arrange its components. Container public void setLayout LayoutManager mgr To set up the layout of a Container such as Frame , JFrame , Panel , or JPanel , you have to: Construct an instance of the chosen layout object, via new and constructor, e.
The Panel container sets to this layout. Container's getLayout method You can get the current layout via Container 's getLayout method. Accessibility API: provides assistive technology for the disabled. Pluggable look and feel supports. Drag-and-drop support between Java and native applications. Swing components are lightweight.
The AWT components are heavyweight in terms of system resource utilization. Each AWT component has its own opaque native display, and always displays on top of the lightweight components. AWT components rely heavily on the underlying windowing subsystem of the native operating system.
Every action that affects the GUI, e. The result of processing an event may be a manipulation of the bits of color on the screen or it may result in calls to methods in the developer's code. If we look at the process to handle the clicking of a button, what we see is that the user's mouse click sets off a chain of events.
The button object responds to the mouse click by creating a button click event that is placed into the event queue. The event loop, when it is free to do so, picks up that event and processes it. The processing of a button click event involves calling methods on specially registered objects called "listeners" who are "listening" for the click event.
The listeners for a button click are objects implementing the java. ActionListener interface and the button click event processing involves calling the listener's actionPerformed method, which the developer has implemented to do whatever is needed when that particular button is clicked. Note that multiple listeners may be "added" to any given button and the button click processing will call each ActionListener 's actionPerformed method in turn.
In the other direction, if we look at the painting process, supposed the developer's code wishes to call repaint a frame component on the screen. The developer's code will call the repaint method of the frame, but all that does is to generate a repaint event that is placed into the event queue along with all the other events.
When the event loop is able, it processes that repaint event and in doing so, the GUI subsystem will generate a special java. Graphics instance that is used to represent the physical screen of the computer. This Graphics object is passed as an input parameter to a call to the paintComponent method of the frame. Technically, the system calls the frame's paint method which in turn calls paintComponent , but we don't need to worry about that. Since the frame is a container, it as any good Composite Design pattern implementation will do, in turn calls the paintComponent method of all of its sub-components.
If one of those components is itself a container, then the painting process cascades down the composite tree until every component has had a chance to paint itself upon the screen the Graphics object. The developer can insert custom painting operations by simply overriding the paintComponent method of the desired component.
JLabel -- A piece of non-editable text on the screen. JButton -- A button that can be clicked. Clicking the button will cause its actionPerformed event to be fired which will call all installed ActionListener 's actionPerformed methods.
JTextField -- A single line of user-editable text. JTextArea -- A box with multiple lines of text displayed. Can be set for editable or non-editable. The English characters will be replaced by any other special character.
Having understood a little about various Java GUI components. Note: We implement an ActionListener that listens to an event. The ActionListener class can be found in java. It has only one method actionPerformed. From the snippet above, we instantiate a JPanel class using the variable name as a panel.
Then, we set the layout to be a null value, meaning: the layout should take the entire width and height of the screen. Line 3: Positioning frame on the window, pixel from the left, pixel from the top of the screen. Line 4: Declares the frame on the panel. Line 5: Setting the size of the entire frame - pixels wide width and pixels long height.
Line 6: This will end and close the process whenever we exit the window. Next, we instantiate the username label and position. It is set at pixels from the left of the frame, 8 pixels from the top of the frame, with 70 pixels as its width, and 20 pixels as its height.
0コメント