我正在尝试将图像用作一种按钮,但我能想到的唯一方法是向其添加鼠标单击事件。这还没有任何想法吗?这是我的代码:
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);
}
}
}
}
我现在尝试做的是,当点击退出图像时,会出现一个弹出屏幕。为什么我没有弹出屏幕?还有另一种方法可以将图像变成按钮而不会看到自己的按钮吗?
答案 0 :(得分:2)
它不起作用,因为你需要向面板添加一个监听器,而不是创建一个方法(没有人调用)。
将您的方法移动到构造函数中的侦听器的匿名类实现中(如未经测试的代码,谨慎使用):
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);
}
}
});
...
}
答案 1 :(得分:0)
您必须在类中实现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);