如何创建一个带有两个图像的JPanel,其中鼠标hover时只显示下面一部分图像?

我正在尝试创建一个按钮面板,其中单击的按钮变为“不同颜色”; 即显示背景图像。 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 } } 

无需重新发明轮子。 而是使用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); } } 

一个不同的想法。 基本上,将图像加载到JLabel ,设置标签的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(); } } } } 

ActionListener添加到按钮并使用imgClicked调用imgClicked

像这样的东西:

  JButton btn = new JButton(); btn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { btn.setIcon(imgClicked); } });