0

クラスのボタンを次のようにインスタンス化しました。

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

クリックしても何も起こらないので、次のようなアクション リスナーを追加します。

linkBtn.addActionListener(SOMETHING);

私はこのようなことを試しました:

linkBtn.addActionListener(new LinkButton.OpenUrlAction());

次のエラーが発生します。

LinkBut​​ton.OpenUrlAction を含む外側のインスタンスが必要です

私はまだ正しい構文を見つけていません。

JButton を拡張するクラスは次のとおりです。

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 LinkButton extends JButton
    implements ActionListener {
    /** The target or href of this link. */
    private URI target;
    final static private String defaultText = "<HTML>Click the <FONT color=\"#000099\"><U>link</U></FONT>"
        + " to go to the website.</HTML>";

    public LinkButton(URI target, String text) {
        super(text);
        this.target = target;
        //this.setText(text);
        this.setToolTipText(target.toString());
    }
    public LinkButton(URI target) {
        this( target, target.toString() );
    }    
    public void actionPerformed(ActionEvent e) {
        open(target);
    }
    class OpenUrlAction implements ActionListener {
      @Override public void actionPerformed(ActionEvent e) {
        open(target);
      }
    }
    private static void open(URI uri) {
    if (Desktop.isDesktopSupported()) {
      try {
        Desktop.getDesktop().browse(uri);
      } catch (IOException e) { /* TODO: error handling */ }
    } else { /* TODO: error handling */ }
  }
}
4

4 に答える 4

2

私は提案を受け入れます。私も自分のプログラム構造が好きではありません...

これまでに提供された答えはすべて優れています。

Hovercraft はAction、コードの構造を単純化する s の使用を提案しています。

例えば...

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 LinkButtonExample {

    public static void main(String[] args) {
        new LinkButtonExample();
    }

    public LinkButtonExample() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }

                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new GridBagLayout());
                    frame.add(new JButton(new OpenURLAction(new URL("http://stackoverflow.com/"))));
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                } catch (MalformedURLException ex) {
                    ex.printStackTrace();
                }
            }
        });
    }

    public class OpenURLAction extends AbstractAction {

        private URL url;

        public OpenURLAction(URL url) {

            this("<HTML>Click the <FONT color=\\\"#000099\\\"><U>link</U></FONT> to go to the website.</HTML>", url);

        }

        public OpenURLAction(String text, URL url) {

            putValue(NAME, text);
            setURL(url);

        }

        public void setURL(URL url) {
            this.url = url;
            setEnabled(
                            url != null
                            && Desktop.isDesktopSupported()
                            && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE));
            putValue(SHORT_DESCRIPTION, url == null ? null : url.toString());
        }

        public URL getURL() {
            return url;
        }

        @Override
        public void actionPerformed(ActionEvent e) {

            if (isEnabled()) {

                URL url = getURL();
                if (url != null && Desktop.isDesktopSupported()
                                && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
                    try {
                        Desktop.getDesktop().browse(url.toURI());
                    } catch (    IOException | URISyntaxException ex) {
                        ex.printStackTrace();
                    }
                }

            }

        }
    }
}

詳細については、アクションの使用方法を確認してください。

于 2013-06-22T03:03:32.150 に答える