2014-10-29 44 views
1

我想使用圖像作爲一種按鈕,但我能想到的唯一方法是添加一個鼠標單擊事件。這不工作還有什麼想法?這裏是我的代碼:我想使用鼠標點擊事件的圖像,但它不起作用

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

繪製一個矩形,在你畫法,在那裏你「想」的使用應單擊要仔細檢查你的邏輯是正確的 – MadProgrammer 2014-10-29 09:04:46

+1

*「我想用圖片作爲一種按鈕,..「*而是使用'Image'作爲JButton的'ImageIcon'(未裝飾,可能沒有邊框),我敢打賭它可以用於鼠標和鍵盤。 – 2014-10-29 09:21:04

+0

帶圖像的'Button'也適用於這裏。如果不想使用按鈕的邊框,則使用「空邊框」即可將其刪除。 – 2014-10-29 09:28:31

回答

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

      } 
     } 
    }); 
    ... 
} 
+0

這是行不通的...如果我點擊圖片,什麼也沒有發生 – Luit 2014-10-29 09:09:28

+0

你應該檢查你的界限,這可能是問題在於,因爲你有'if(bounds.contains(...'line in there。做一個簡單的測試,在'mouseClicked'方法的開始處添加一些打印輸出或對話框彈出窗口,您應該看到現在您對鼠標點擊事件作出反應。 – eitanfar 2014-10-29 09:11:20

+0

我試過了,但沒有奏效... – Luit 2014-10-29 09:15:17

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