Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use JAVA GUI and MouseListener

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to use JAVA GUI and MouseListener". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn how to use JAVA GUI and MouseListener.

The so-called listener, it must be bound to some GUI part, for example, I declared a JFrame f, I want to monitor it with the mouse, you can use f.addMouseListener (this)

Use the MouseListener class as the listener of JFrame import java.awt.event.*;import javax.swing.*; public class ImplementMouseListener implements MouseListener {/ / Note the s after implement, notice the spelling of Listener, list'e'ner, with a 'ten' JFrame f; public void ImplementMouseListener () {/ / constructor of this class f=new JFrame () / / Note that the first two letters of JFrame are capitalized, otherwise a difficult error f.addMouseListener (this) may occur; / / Note that you need to add this in parentheses, although you don't know what the meaning is f.setSize (300150); f.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); / / it's a little unclear what f.setVisible (true) means. / / the method show from the type Window is deprecated, meaning show () this method and the deprecation of} public void mousePressed (MouseEvent e) {} public void mouseReleased (MouseEvent e) {} public void mouseEntered (MouseEvent e) {} public void mouseExited (MouseEvent e) {} public void mouseClicked (MouseEvent e) {f.setTitle ("click coordinates not" + e.getX () + "," + e.getY ()) / / you need to declare JFrame as a member object of this class, not in the constructor, otherwise this sentence cannot run} / / without looking at the main method, there is a complete class above. The host is only equivalent to displaying the running process in a class. Public static void main (String [] args) {/ / static must precede void, otherwise it will report an error new ImplementMouseListener (); / / Don't forget to put parentheses}}

Notice the setTitle method, distinguishing it from the name of the window when you constructed the JFrame. This setTitle is also a method in JFrame to set the name of the window. I uninstalled it in the mouseCliked event so that the name of the window is reset after each mouse click.

Pay attention to the mouse event e, which also contains many useful methods

F.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE)

The so-called close refers to the fork in the upper right corner of the window bar. Use this command to determine what happens to that fork. The parameter JFrame.EXIT_ON_CLOSE in parentheses of this command is actually a constant 3 defined in javax.swing.WindowContants.

Click on the upper right corner of the window to close, four ways to close this.setDefaultCloseOperation (0); / / DO_NOTHING_ON_CLOSE, do nothing. This.setDefaultCloseOperation (1); / / HIDE_ON_CLOSE, hide only the interface, setVisible (false). This.setDefaultCloseOperation (2); / / DISPOSE_ON_CLOSE, hide and release the form, dispose (), and the program ends when the last window is released. This.setDefaultCloseOperation (3); / / EXIT_ON_CLOSE, close the application directly, System.exit (0). One main function corresponds to an entire program. Use the MouseAdapter class as the JFrame listener import java.awt.*;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import javax.swing.*; public class ExtendMouseAdapter extends MouseAdapter {/ / if the package is not introduced, you can click MouseAdapter and there will be a prompt to introduce the package JFrame f / / MouseAdapeter is an abstract class, while MouseListener is an interface (interface) public ExtendMouseAdapter () {f=new JFrame ("Mouse listener Test window"); f.setSize (300150); f.addMouseListener (this); f.setVisible (true); f.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE) / / JFrame.EXIT_ON_CLOSE is a constant defined in java.swing.WindowConstants, equivalent to 3} public void mouseClicked (MouseEvent e) {/ / the first letter of the class is always uppercase such as, MouseEvent, while the first letter of a method is always lowercase, such as mouseClicked f.setTitle ("Click position is" + e.getX () + "+ e.getY () } public static void main (String [] args) {new ExtendMouseAdapter ();}} MouseAdapter and MouseListener classes actually encapsulate five mouse events

The difference between them is that the latter is an interface (interface) that uses implements for class inheritance and implements these five methods after inheritance (even if it is an empty implementation), while the former is an abstract class, which also inherits from MouseListener and turns all five mouse events into a null implementation.

Note: when a java project contains multiple source files, and there are multiple source files and main methods, click run will not report an error.

So how do you determine which main method is running?

As shown in the figure:

Click on the drop-down flag to see the running configuration (run configurations)

Where you can set which main method to run:

Don't get the illusion that when you construct a GUI window, you have to write it in the constructor of the class. You can actually write it freely, such as unloading the main method.

At this point, I believe you have a deeper understanding of "how to use JAVA GUI and MouseListener". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report