Monday 15 September 2014

java - How do I add an ActionListener to an instance of my class that extends JButton? -


I have started a button in my class like this:

  linkBtn = New LinkButton (new) URI ("http://www.example.com"), "Click Me");   

When I click it, nothing happens, so I want to add an action listener like this: pre <> linkBtn.addActionListener (some);

I tried this way:

  linkBtn.addActionListener (new link button. OpenUrLections ());   

This returns the following error:

An attached example that requires LinkButton.OpenUrlAction

I Until right syntax was not found.

Here is my class that extends the pocket button:

  import java.awt.Desktop; Import java.awt.event.ActionEvent; Import java.awt.event.ActionListener; Import java.io.IOException; Import java.net.URI; Import javax.swing.JButton; Public class Linkbunballton applies ActionListener {/ ** Target of this link or href * / Private URI target; Last fixed private string default text = "& lt; HTML>    

I'm open to suggestions. I do not like the structure of my program. .

All the answers given so far are outstanding.

Hovercraft has suggested using verb {/ code> s, Structure.

Examples For area ...

  Import java.awt.Desktop; Import java.awt.EventQueue; Import java.awt.GridBagLayout; Import java.awt.event.ActionEvent; Import java.io.IOException; Import java.net.MalformedURLException; Import java.net.urISyntaxException; Import java.net.URL; Import java.util.logging.Level; Import java.util.logging.Logger; Import javax.swing.AbstractAction; Import javax.swing.JButton; Import javax.swing.JFrame; Import javax.swing.UIManager; Import javax.swing.UnsupportedLookAndFeelException; Public class linkbutton example {public static zero main (string [] args) {new link button example (); } Public linkbutton example () {EventQueue.invokeLater {@Override} Public {0} {try {UIManager.setLookAndFeel}} {hold (ClassNotFoundException | InstantiationException | } Jeffre Frame = New Gefram ("Test"); Frame.Set default close operation (JFRM.EXIT_ON_CLOSE); Frame.seletout (new gridboutout ()); Frame.ed (new pocket (New OpenRoundation (new URL ("http: // Stackoverflow.com/"))); Frame.pack (); Frame.SettingsLalitto (empty); Frame.Serviceable (true);} catch (malmarmdurelexception X) {ex.printStackTrace ();}}}); } Public Class OpenRaxation Abstracts Action {Private URL URL; Public OpenArchlication (URL URL) {Click on this ("& lt; HTML» Click  gt; & lt; / font & gt; website. & Lt; / html & gt; ", url); } Public OpenRecquellection (string text, url URL) {putValue (NAME, text); SetURL (url); } Public Zero Set URL (URL URL) {this.url = url; SetEnabled (url! = Null & Desktop .isDesktopSupported () & Desktop .getDesktop (.) Is supported (Desktop.Action.BROWSE)); PutValue (SHORT_DESCRIPTION, url == tap? Null: url.toString ()); } Public URL getURL () {return url; } @ Override Public Zero ActionPerfed (Action Event E) {if (Enabled)) {URL url = getURL (); If (url! = Null & Desktop. IsDesktopSupported () & Desktop .getDesktop (.) Is supported (Desktop.Action.BROWSE) {try {Desktop.getDesktop (). Browse (url.toURI ()); } Hold (IOException | URISyntaxException ex) {ex.printStackTrace (); }}}}}}   

For more information see

No comments:

Post a Comment