0

我正在尝试创建一个按钮面板,其中单击的按钮变为“不同颜色”;即显示背景图像。ps 我只需要这种方法(带有 2 张图像),而不需要其他任何东西。谢谢 !

例如:

public class TestPane extends JPanel {      

        private BufferedImage   imgUnclicked;
        private BufferedImage   imgClicked;
        private Point           mousePoint;

        public TestPane() {
            try {
                imgUnclicked = ImageIO.read(new File("C:\\Users\\Me\\Desktop\\tmp\\Uncolored.png"));
                imgClicked = ImageIO.read(new File("C:\\Users\\Me\\Desktop\\tmp\\Colored.png"));
            } catch (IOException ex) {
                Logger.getLogger(Spotlight.class.getName()).log(Level.SEVERE, null, ex);
            }

            addMouseMotionListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    mousePoint = e.getPoint();
                    repaint();
                }
            });
        }
    }
    @Override
    protected void paintComponent(Graphics g) {
        //Draw imgClicked
        //Draw imgUnclicked with some rectangular area around mouse click subtracted
    }
}
4

3 回答 3

5

无需重新发明轮子。而是使用JToggleButton(适当配置)。按钮将对鼠标键盘输入做出反应。

import java.awt.*;
import java.net.*;
import javax.imageio.ImageIO;
import javax.swing.*;

class ChangeImageOnClick {

    public static void main(String[] args) throws Exception {
        URL url1 = new URL("http://i.stack.imgur.com/gJmeJ.png");
        final Image img1 = ImageIO.read(url1);
        URL url2 = new URL("http://i.stack.imgur.com/wCF8S.png");
        final Image img2 = ImageIO.read(url2);
        Runnable r = new Runnable() {

            @Override
            public void run() {
                JToggleButton btn = new JToggleButton("Click me!");
                btn.setIcon(new ImageIcon(img1));
                btn.setSelectedIcon(new ImageIcon(img2));

                btn.setContentAreaFilled(false);
                btn.setBorderPainted(false);

                JOptionPane.showMessageDialog(null, btn);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}
于 2013-09-06T10:58:59.573 回答
2

一个不同的想法。基本上,将图像加载到 aJLabel中,设置Layout标签并为其添加两个不透明组件。

通过使用简单的MouseListener,您可以根据需要使组件不可见或透明...

在此处输入图像描述在此处输入图像描述

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class HideAndShow {

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

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

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new BorderLayout());
            try {
                BufferedImage img = ImageIO.read(new File("/Users/swhitehead/Dropbox/MegaTokyo/Haibane_Miho___Take_2_by_garrbage.png"));
                JLabel label = new JLabel(new ImageIcon(img.getScaledInstance(-1, 200, Image.SCALE_SMOOTH)));
                add(label);

                label.setLayout(new GridLayout(2, 1));

                JPanel top = new JPanel();
                top.add(new JLabel("Top"));
                JPanel bottom = new JPanel();
                bottom.add(new JLabel("Bottom"));

                MouseAdapter ma = new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        ((JPanel)e.getComponent()).setOpaque(false);
                        repaint();
                    }
                };

                top.addMouseListener(ma);
                bottom.addMouseListener(ma);

                label.add(top);
                label.add(bottom);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}
于 2013-09-06T10:12:30.533 回答
1

将一个添加ActionListener到您的按钮并setIcon使用调用imgClicked

像这样的东西:

    JButton btn = new JButton();
    btn.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            btn.setIcon(imgClicked);
        }
    });
于 2013-09-06T10:07:55.200 回答