Java 使JButton可单击,而不是JLabel

Java 使JButton可单击,而不是JLabel,java,swing,jbutton,jlabel,actionlistener,Java,Swing,Jbutton,Jlabel,Actionlistener,我有一个例子,我把JLabel放在JButton中,并调整JButton的大小 这里的问题是每次单击按钮时,JLabel都会捕获大部分事件 当我尝试将ActionListener添加到JButton时,它没有起作用 但是,当我尝试将MouseListener添加到JLabel时,所有事件处理程序都可以工作 我希望JButton的ActionListener能够工作。我不希望JLabel捕获所有事件而不破坏它们上的默认配置 我尝试将JLabel focusable属性设置为false,但也不起作用

我有一个例子,我把JLabel放在JButton中,并调整JButton的大小

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

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

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

我希望JButton的ActionListener能够工作。我不希望JLabel捕获所有事件而不破坏它们上的默认配置

我尝试将JLabel focusable属性设置为false,但也不起作用

那我该怎么办呢

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

这是基本属性,默认情况下,顶层的
JComponent
使用所有来自
鼠标的事件

有两种方法

  • (不知道为什么会有
    JLabel
    )如果可以用普通的
    JButton
    代替API中的实现方法

  • 添加
    MouseListener
    (可能没有理由覆盖所有
    MouseEvents
    只添加
    MouseAdapter
    )到
    JLabel
    ,并从
    mouseClicked
    调用
    JButton.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;
    }
}

为什么按钮中需要标签?您可以在
JButton
上调用
setText
,这可以让您对标签做几乎任何事情。为什么需要标签?要在单击按钮时摆脱文本周围的框,并进行ui修改,请尝试使用JButton#setOrderPaint和JButton#setFocusPaoEnted和JButton#setContentAreaFilled以查看它们的帮助,我尽可能将JLabel放在JButton中,这是错误的-按钮不是设计用来作为容器的。因此,你需要很好的理由来打破这一规则(并且会遇到比你预期的更多的问题)。鉴于此,你的问题是颠倒的,也不完整的:你假设了一个你没有陈述的目标的解决方案,并且假设的解决方案存在问题……难道这不是只有在1-标签注册了听众时才会出现的情况吗(或工具提示)和2-wasopaque@MadProgrammer默认情况下(如果使用LayoutManager),没有任何问题,请参见我的编辑,所有事件都已注册、分配。对于普通JButton,我太懒了,无法请求OP的努力。。。。。