使JButton可点击而不是JLabel

时间:2013-03-12 07:10:05

标签: java swing jbutton jlabel actionlistener

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

这里的问题是每当我点击按钮时,JLabel就会捕获大部分事件。

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

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

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

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

那我该怎么办呢?

1 个答案:

答案 0 :(得分:2)

  

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

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

有两种方式

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

  • 添加MouseListener(可能没有理由覆盖所有MouseEvents只添加MouseAdapter)到JLabel,从mouseClicked覆盖JButton.doClick() 1}}

修改

@Mad,

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;
    }
}