java - 从多个 JLabel 获取鼠标点击输入的较小程序

标签 java swing jlabel actionevent

我在一个框架中有一堆标签,基本上用作显示文本的多行输出。在程序的某些点,用户需要从给定的选项中选择一个选项。他们执行此操作的方法是单击相应的 JLabel。

我尝试使用 JLabel 数组,但功能不起作用。然后,我为每个 JLabel 制作了一个单独的 ActionEvent,这可以工作,但是是否有一个优雅或有效的代码?

最佳答案

不客气。

您描述了您的问题,我想我可以理解。

您可以找出 ActionListener 的相似之处,并将这些相似之处放入 super-Class

对我来说,另一种可能性似乎是“一体化”ActionListener。它的 actionPerformed -方法有一个 ActionEvent 类型的参数。此 ActionEvent 提供了一个 getSource() 方法,您可以检索发生 ActionEventJComponent。因此,如果您为 JLabel 指定名称,则可以在 actionPerformed-Method

中检查名称

编辑

像这样:

public void actionPerformed(final ActionEvent actionEvent) {
    final String name = ((JComponent) actionEvent.getSource()).getName();
    switch (name) {
        case "label1":
            handleLabel1();
            break;
        case "label2":
            handleLabel2();
            break;
        case "label3":
            handleLabel3();
            break;
        default:
            break;
    }
}

编辑 1

import java.awt.AWTEvent;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

public class Application extends JFrame {
    private abstract class MyComponentListener implements MouseListener, ActionListener {
        protected void actionOnButton(final AWTEvent e) {
            final JButton button = (JButton) e.getSource();
            switch (button.getName()) {
                case "button1":
                    System.out.println("Hello World");
                    break;
                default:
                    System.out.println("mouse button one clicked on " + button.getName());
                    break;
            }
            // more general
            this.actionOnComponent(e);
        }

        protected void actionOnComponent(final AWTEvent e) {
            final JComponent component = (JComponent) e.getSource();
            final int index = Integer.valueOf(component.getName().replaceAll("[^0-9]", "")).intValue();
            switch (index) {
                case 1:
                    System.out.println("Hello World");
                    break;
                default:
                    System.out.println("mouse button one clicked on " + component.getName());
                    break;
            }
        }

        protected void actionOnLabel(final AWTEvent e) {
            final JLabel label = (JLabel) e.getSource();
            switch (label.getName()) {
                case "label1":
                    System.out.println("Hello World");
                    break;
                default:
                    System.out.println("mouse button one clicked on " + label.getName());
                    break;
            }
            // more general
            this.actionOnComponent(e);
        }
    }

    private class MyListener extends MyComponentListener {
        /**
         * {@link ActionListener#actionPerformed(ActionEvent)}
         */
        @Override
        public void actionPerformed(final ActionEvent e) {
            System.out.println("next line(s) generated by ActionListener");
            this.actionOnButton(e);
        }

        @Override
        public void mouseClicked(final MouseEvent e) {
            // whole process of mouse button down and up
            if ((e.getButton() == 1)) {
                System.out.println("next line(s) generated by MouseListener");
                if ((e.getSource() instanceof JLabel)) {
                    this.actionOnLabel(e);
                } else if (e.getSource() instanceof JButton) {
                    this.actionOnButton(e);
                }
            }
        }

        @Override
        public void mouseEntered(final MouseEvent e) {
            // on mouse over
        }

        @Override
        public void mouseExited(final MouseEvent e) {
            // on mouse out
        }

        @Override
        public void mousePressed(final MouseEvent e) {
            // mouse button down
        }

        @Override
        public void mouseReleased(final MouseEvent e) {
            // mouse button up after down
        }
    }

    private static final long serialVersionUID = 1L;

    private Application() {
        super();
    }

    public void start() {
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.getContentPane().setLayout(new BoxLayout(this.getContentPane(), BoxLayout.PAGE_AXIS));
        this.addLabel("Hello World - Label", "label1");
        this.addLabel("Label 2", "label2");
        this.addLabel("Label 3", "label3");
        this.addButton("Hello Word - Button", "button1");
        this.addButton("Button 2", "button2");
        final Dimension dimension = new Dimension(500, 150);
        this.setSize(dimension);
        this.setPreferredSize(dimension);
        this.setVisible(true);
    }

    private void addButton(final String text, final String name) {
        final JButton button = new JButton(text);
        button.setName(name);
        button.addMouseListener(new MyListener());
        button.addActionListener(new MyListener());
        this.getContentPane().add(button);
    }

    private void addLabel(final String text, final String name) {
        final JLabel label = new JLabel(text);
        label.setName(name);
        label.addMouseListener(new MyListener());
        this.getContentPane().add(label);
    }

    public static void main(final String[] args) {
        final Application app = new Application();
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                app.start();
            }
        });
    }
}

关于java - 从多个 JLabel 获取鼠标点击输入的较小程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56717866/

相关文章:

java - protected/private - 何必呢?

java - 具有单线程执行器的 RESTful Web 服务的架构?

java - 当文本更改时如何阻止 JLabel 更改其大小?

Java Swing - 位置测量单位?

java - 如何 EasyMock 对返回通配符泛型的方法的调用?

java - 自定义期望中传入参数的格式

swing 中的 java.lang.NullPointerException

java - Swing 圆形 JFrame

java - 我想在基于Java的Net beans中创建一个桌面应用程序

java - 在 swing 中使用 jLabels 进行循环