java - Callbacks and listeners ; MouseListener -
I am trying to write a project with listeners, I work my buttons where they change color, now I need my mouse listers to print the text of what I'm doing to the mouse, for example: "The mouse has entered the yellow area, the mouse has got out of the yellow area, the mouse is in the yellow area / Clicked, etc. " I have applied them but nothing is working to print the text. Here is my code:
Import java.awt.event.ActionEvent; Import java.awt.event.ActionListener; Import java.awt.event.MouseEvent; Import java.awt.event.MouseListener; Import javax.swing.JButton; Import javax.swing.JFrame; Import javax.swing.JPanel; Import java.awt.GridLayout; Import java.awt.color; Import java.awt.Graphics; Import java.awt.Graphics2D; Import java.awt.geom.Line2D; Import java.awt.geom.Point2D; Import javax.swing.JComponent; Public square swingb {// frame properties personal constant final distance FRAME_WIDTH = 400; Personal fixed final distance FRAME_HEIGHT = 400; Public Stable Zero Main (string [] args) {// Instant a frame (main window) JFrame frame = new JFrame (); // button (one for each color) Ultimate Button BRD = New Pocket ("Red"); Jebton Bianno = New Pocket ("Yellow"); Jebton WLE = New Jebton ("Blue"); // Here we create a panel made of other panels (placed in one / grid) to support the last JPanel container of the button and "art" example = new JPNL (new grid layout (2,1)); Ultimate zpean panel = new zpinal (new grid layout (1,1)); Last Junkel ButtonPhenel = New ZipLine (new gridlate (3,1)); // An example of a particular class to play with you (Art is defined // below) Art artbox = new Art (); Panel.add (artBox); // button button in the panel.ed (BRD) add buttons; ButtonPanel.add (beeellow); ButtonPanel.add (bBlue); // Put the panels together and add them to the frame container.ad (panel); Container.add (buttonPanel); Frame.add (container); / * Your code goes here / / declares your audience and adds them here // // If you are going to call addMouseListener for each addActionListener and // // If you want to deal with a JPanel called "Panel" / Up / * End Your Code * Apply / ActionListener / RedButtonListener Player, MouseListener {Pure Zero Action Action (ActionEvent Event) {panel.setBackground (Color.RED); } @ Override Public Wide Mouseclat (MouseEvent E) {bRed.addMouseListener (this); AddMouseListener (this); // Todo auto generated method stub}
An XxxxListener will not work unless you Add it to something, until it actually hears something to respond to MouseListener, it should be added to the list of components through someComponent.addMouseListener (myMouseListener)
. You want to read the audience's tutorials to get the exact details. First Google hit for Java Swing Mouse Listener Tutorials:
My main criticism of your question is that you are asking how to do something before trying to do it, and sorry, but it is not that you will ever like to learn the program. . Tutorials are available for this, and you have got a computer programming lab at your fingertips, so use it, play, play, write code, run it, change it, push it to the limit and then move on Grow, find out what does not work. Believe me, you are not going to blow your computer, you will not bring doom and negligence with effort. For simple questions that can be answered by the test, do not ask us here - find out for yourself that this is what is learning and which programming is about.
Edit
OK, you are adding your mouse listener inside to the mouseleastner, which is not working Why would not it add to your class creator or some other init or set method? Additionally, your compiler will complain because the mouseclient implementer does not apply all methods of the Matlit interface, and it is not allowed (unless there is a class summary, which is what you want). So give it to the remaining mouse listener methods.
For example:
Increases the public square Fu JPNell {Private Button Button = New Pocket ("Button"); Public Fu () {MyListener myListener = New MyListener (); Button.addActionListener (myListener); Button.addMouseListener (myListener); (Button) connecting; }
Where
class applies MyListener MouseListener, ActionListener {// ..... here bunch of code}
Comments
Post a Comment