2014-01-02 2 views
-1

вот мой код, который я написал, я хочу отобразить изображение, которое находится в моем классе, так что то, что я должен сделать для того же самого, что и biometric1, - это рабочее пространство eclipse затем название проекта «D: \ biometric1 \ TestJniPrj \ single.jpg»;Я хочу показать изображение на кнопке щелчка в Swing

package org.test.ivb; 

import java.awt.Color; 
import java.awt.EventQueue; 

import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.border.BevelBorder; 

public class ImageWindow extends JFrame { 
    private static final long serialVersionUID = 1L; 
    private JFrame frame; 

    /* 
    * private void btnCaptureActionPerformed(java.awt.event.ActionEvent evt) 
    * {//GEN-FIRST:event_jButton8ActionPerformed try { CaptureFrame capone = 
    * new GetCapture(); this.initFingerprint(this.openFile()); } catch 
    * (IOException e1) { e1.printStackTrace(); } catch (InterruptedException 
    * e1) { e1.printStackTrace(); } 
    * 
    * } 
    */ 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        ImageWindow window = new ImageWindow(); 
        window.frame.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    public ImageWindow() { 
     initialize(); 
    } 

    private void initialize() { 
     frame = new JFrame(); 
     frame.setBounds(100, 100, 450, 378); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().setLayout(null); 

     JPanel panel = new JPanel(); 
     panel.setForeground(Color.PINK); 
     panel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, 
       null)); 
     panel.setBounds(27, 11, 268, 303); 
     frame.getContentPane().add(panel); 

     JButton btnNewButton = new JButton("capture"); 
     btnNewButton.setToolTipText(""); 
     btnNewButton.setSelectedIcon(new ImageIcon(
       "D:\\biometric1\\TestJniPrj\\single.jpg")); 
     btnNewButton.setBounds(318, 146, 89, 23); 
     frame.getContentPane().add(btnNewButton); 
    } 
} 
+0

* «Я хочу показать изображение на кнопку мыши в свинг» * ИТАК ... пойти на это, у вас есть мое разрешение. Здесь нет вопроса, просто заявление о намерениях. –

+0

См. [* Как использовать кнопки, флажки и радио кнопки *) (http://docs.oracle.com/javase/tutorial/uiswing/components/button.html) и [info] (http: // stackoverflow .com/tags/embedded-resource/info) для [tag: embedded-resoure]. – trashgod

ответ

1
package main; 

import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.*; 
import javax.swing.border.BevelBorder; 

public class ImageWindow extends JPanel { 

    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 
    private JFrame frame; 
    private Image img; 

    /** 
    * Launch the application. 
    */ 

    /* 
    * private void btnCaptureActionPerformed(java.awt.event.ActionEvent evt) 
    * {//GEN-FIRST:event_jButton8ActionPerformed try { CaptureFrame capone = 
    * new GetCapture(); this.initFingerprint(this.openFile()); } catch 
    * (IOException e1) { e1.printStackTrace(); } catch (InterruptedException 
    * e1) { e1.printStackTrace(); } 
    * 
    * } 
    */ 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 

      public void run() { 
       try { 
        ImageWindow window = new ImageWindow(); 
        window.frame.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    /** 
    * Create the application. 
    */ 
    public ImageWindow() { 
     initialize(); 
    } 

    /** 
    * Initialize the contents of the frame. 
    */ 
    private void initialize() { 
     frame = new JFrame(); 
     frame.setBounds(100, 100, 450, 378); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().setLayout(null); 

     this.setForeground(Color.PINK); 
     this.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null)); 
     this.setBounds(27, 11, 268, 303); 
     frame.getContentPane().add(this); 

     JButton btnNewButton = new JButton("capture"); 
     btnNewButton.setToolTipText(""); 
     btnNewButton.setSelectedIcon(new ImageIcon("D:\\biometric1\\TestJniPrj\\single.jpg")); 
     btnNewButton.setBounds(318, 146, 89, 23); 
     frame.getContentPane().add(btnNewButton); 

     btnNewButton.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       img = Toolkit.getDefaultToolkit().createImage(ImageWindow.this.getClass().getResource("test.png")); 
       ImageWindow.this.repaint(); 
      } 
     }); 
    } 

    public void paintComponent(Graphics g) { 
     if (img != null) { 
      g.drawImage(img, 0, 0, this); 
     } 
    } 
} 

надеюсь, что это помогает :)

+0

Thanxs doomsdaymachine it хорошо подходит – vaib

+1

без проблем! рад, что сработал :) – doomsdaymachine

Смежные вопросы