-1

我有一个案例,我将 JLabel 放在 JButton 中并调整 JButton 的大小。

这里的问题是每次我单击按钮时,JLabel 都会捕获大部分事件。

当我尝试将 ActionListener 添加到 JButton 时,它不起作用。

但是当我尝试将 MouseListener 添加到 JLabel 时,所有事件处理程序都可以工作。

我希望 JButton 的 ActionListener 工作。我不希望 JLabel 在不破坏我的默认配置的情况下捕获所有事件。

我尝试将 JLabel 可聚焦属性设置为 false,但它也不起作用。

那我该怎么办呢?

4

1 回答 1

2

我有一个案例,我将 JLabel 放在 JButton 中并调整 JButton 的大小。

这是基本属性,默认情况下,顶层JComponent消耗所有事件来自Mouse&Keyboard

有两种方法

  • (不知道为什么会这样JLabel)如果可以JButton在 API 中使用普通方法和实现的方法

  • add MouseListener(也许没有理由覆盖所有add MouseEventsonly MouseAdapter)来电JLabelmouseClickedJButton.doClick()

编辑

@疯狂的,

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.swing.*;

public class JButtonAndIcon {

    private JLabel label = new JLabel();
    private Random random = new Random();
    private ImageIcon image1; // returns null don't worry about in Swing
    private ImageIcon image2; // returns null don't worry about in Swing
    private Timer backTtimer;
    private int HEIGHT = 300, WEIGHT = 200;


    public JButtonAndIcon() {
        label.setPreferredSize(new Dimension(HEIGHT, WEIGHT));
        final JButton button = new JButton("Push");
        button.setBorderPainted(false);
        button.setBorder(null);
        button.setFocusable(false);
        button.setMargin(new Insets(0, 0, 0, 0));
        button.setContentAreaFilled(false);
        button.setLayout(new BorderLayout());
        button.add(label);
        button.setMultiClickThreshhold(1000);
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (button.getIcon() == image1) {
                    label.setIcon(image2);
                } else {
                    label.setIcon(image1);
                    if(backTtimer.isRunning()){
                         backTtimer.restart();
                    }                   
                }
            }
        });
        JFrame frame = new JFrame("Test");
        frame.add(button);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        startBackground();
        frame.setVisible(true);
    }

    public static void main(String[] args) throws IOException {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                JButtonAndIcon t = new JButtonAndIcon();
            }
        });
    }

    private void startBackground() {
        backTtimer = new javax.swing.Timer(1500, updateBackground());
        backTtimer.start();
        backTtimer.setRepeats(true);
    }

    private Action updateBackground() {
        return new AbstractAction("Background action") {

            private final long serialVersionUID = 1L;

            @Override
            public void actionPerformed(ActionEvent e) {
                label.setIcon(new ImageIcon(getImage()));
            }
        };
    }

    public BufferedImage getImage() {
        int w = label.getWidth();
        int h = label.getHeight();
        GradientPaint gp = new GradientPaint(0f, 0f, new Color(
                127 + random.nextInt(128),
                127 + random.nextInt(128),
                127 + random.nextInt(128)),
                w, w,
                new Color(random.nextInt(128), random.nextInt(128), random.nextInt(128)));
        BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = bi.createGraphics();
        g2d.setPaint(gp);
        g2d.fillRect(0, 0, w, h);
        g2d.setColor(Color.BLACK);
        return bi;
    }
}
于 2013-03-12T07:13:20.520 回答