我想使用鼠标点击事件的图像,但它不起作用
问题描述:
我想使用图像作为一种按钮,但我能想到的唯一方法是添加一个鼠标单击事件。这不工作还有什么想法?这里是我的代码:我想使用鼠标点击事件的图像,但它不起作用
import java.awt.* ;
import java.awt.event.MouseEvent;
import java.awt.Point;
import java.awt.image.* ;
import java.io.* ;
import javax.imageio.* ;
import javax.swing.* ;
public class test {
public static void main(String[]args) {
new test();
}
public test() {
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("test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new Pane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class Pane extends JPanel {
private BufferedImage background;
private BufferedImage Levels;
private BufferedImage Exit;
private BufferedImage PietKiezen;
private BufferedImage ZwartePiet;
private BufferedImage Sinterklaas;
public Pane() {
try {
background = ImageIO.read(new
File("C:/Users/H/Documents/NetBeansProjects/Spel/src/Spel/Dak.gif"));
Levels = ImageIO.read(new
File("C:/Users/H/Documents/NetBeansProjects/Spel/src/Spel/Levelsk.gif"));
Exit = ImageIO.read(new
File("C:/Users/H/Documents/NetBeansProjects/Spel/src/Spel/Exitk.gif"));
PietKiezen = ImageIO.read(new
File("C:/Users/H/Documents/NetBeansProjects/Spel/src/Spel/PietKiezen.gif"));
ZwartePiet = ImageIO.read(new
File("C:/Users/H/Documents/NetBeansProjects/Spel/src/Spel/ZwartePiet.gif"));
Sinterklaas = ImageIO.read(new
File("C:/Users/H/Documents/NetBeansProjects/Spel/src/Spel/Sinterklaas.gif"));
} catch (IOException ex) {
ex.printStackTrace();
}
}
@Override
public Dimension getPreferredSize() {
return background == null ? new Dimension(200, 200) : new
Dimension(background.getWidth(), background.getHeight());
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g.create();
if (background != null) {
int x = (getWidth() - background.getWidth())/2;
int y = (getHeight() - background.getHeight())/2;
g2d.drawImage(background, x, y, this);
}
if (PietKiezen != null) {
int x = 970;
int y = 130;
g2d.drawImage(PietKiezen, x, y, this);
}
if (Levels != null) {
int x = 970;
int y = 260;
g2d.drawImage(Levels, x, y, this);
}
if (Exit != null) {
int x = 970;
int y = 390;
g2d.drawImage(Exit, x, y, this);
}
if (ZwartePiet != null) {
int x = 600;
int y = 256;
g2d.drawImage(ZwartePiet, x, y, this);
}
if (Sinterklaas != null) {
int x = 800;
int y = 256;
g2d.drawImage(Sinterklaas, x, y, this);
}
g2d.dispose();
}
public void mouseClicked(MouseEvent me) {
Point clicked = me.getPoint();
Rectangle bounds = new Rectangle(172, 62, Exit.getWidth(), Exit.getHeight());
if (bounds.contains(clicked)) {
JOptionPane optionPane = new JOptionPane(
"The only way to close is\n"
+ "by pressing one of the following buttons.",
JOptionPane.QUESTION_MESSAGE,
JOptionPane.YES_NO_OPTION);
}
}
}
}
我现在试图做的是,当退出图像被点击时,有一个弹出式屏幕。为什么我没有看到弹出屏幕?有没有另一种方法可以将图像转换为按钮,而不会看到它自己的按钮?
答
它不起作用,因为你需要添加一个监听器到面板,而不是创建一个方法(没人呼叫)。
将你的方法到一个匿名类实现监听器的构造像这样(未测试的代码,慎用):
public Pane() {
...
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent me) {
Point clicked = me.getPoint();
Rectangle bounds = new Rectangle(172, 62, Exit.getWidth(), Exit.getHeight());
if (bounds.contains(clicked)) {
JOptionPane optionPane = new JOptionPane(
"The only way to close is\n"
+ "by pressing one of the following buttons.",
JOptionPane.QUESTION_MESSAGE,
JOptionPane.YES_NO_OPTION);
}
}
});
...
}
答
您必须实现的MouseListener您的类,你必须注册你的形象图标与MouseListener一起使用。
检查下面的示例代码:
JPanel signUpPanel = new JPanel();
springLayout.putConstraint(SpringLayout.NORTH, signUpPanel, 10, SpringLayout.NORTH, this);
springLayout.putConstraint(SpringLayout.WEST, signUpPanel, 100, SpringLayout.EAST, this);
springLayout.putConstraint(SpringLayout.SOUTH, signUpPanel, 100, SpringLayout.SOUTH, this);
springLayout.putConstraint(SpringLayout.EAST, signUpPanel, 0, SpringLayout.EAST, this);
signUpPanel.setLayout(new GridLayout(4, 4, 3, 3));
facebookIcon = new JLabel(new ImageIcon("img" + File.separator + "facebook.png"));
facebookIcon.setToolTipText("Facebook SignUp - Automated process");
facebookIcon.addMouseListener(this);
signUpPanel.add(facebookIcon);
signUpPanel.add(new JLabel(new ImageIcon("img" + File.separator + "betvibes.png")));
signUpPanel.add(new JLabel(new ImageIcon("img" + File.separator + "blogger.png")));
signUpPanel.add(new JLabel(new ImageIcon("img" + File.separator + "flickr.png")));
gmailIcon = new JLabel(new ImageIcon("img" + File.separator + "google.png"));
gmailIcon.setToolTipText("Gmail SignUp - Automated process");
gmailIcon.addMouseListener(this);
signUpPanel.add(gmailIcon);
signUpPanel.add(new JLabel(new ImageIcon("img" + File.separator + "lastfm.png")));
signUpPanel.add(new JLabel(new ImageIcon("img" + File.separator + "linkedin.png")));
signUpPanel.add(new JLabel(new ImageIcon("img" + File.separator + "technorati.png")));
signUpPanel.add(new JLabel(new ImageIcon("img" + File.separator + "twitter.png")));
signUpPanel.add(new JLabel(new ImageIcon("img" + File.separator + "delicious.png")));
signUpPanel.add(new JLabel(new ImageIcon("img" + File.separator + "myspace.png")));
signUpPanel.add(new JLabel(new ImageIcon("img" + File.separator + "reddit.png")));
signUpPanel.add(new JLabel(new ImageIcon("img" + File.separator + "yahoo.png")));
signUpPanel.add(new JLabel(new ImageIcon("img" + File.separator + "vimeo.png")));
signUpPanel.add(new JLabel(new ImageIcon("img" + File.separator + "tumblr.png")));
signUpPanel.add(new JLabel(new ImageIcon("img" + File.separator + "rss.png")));
add(signUpPanel);
绘制一个矩形,在你画法,在那里你“想”的使用应单击要仔细检查你的逻辑是正确的 – MadProgrammer 2014-10-29 09:04:46
*“我想用图片作为一种按钮,..“*而是使用'Image'作为JButton的'ImageIcon'(未装饰,可能没有边框),我敢打赌它可以用于鼠标和键盘。 – 2014-10-29 09:21:04
带图像的'Button'也适用于这里。如果不想使用按钮的边框,则使用“空边框”即可将其删除。 – 2014-10-29 09:28:31