2013-04-11 71 views
0

大家好,我想问,我有这个程序,它有6个按钮,每个都会显示不同的东西,但我想知道如何让它显示图片,当我点击“图片1”还应该在哪里放置这些图片,以便程序知道在哪里找到它们? 感谢希望你能帮到: p.s.我使用NetBeans如何添加一个图片到这个程序

import java.awt.Toolkit; 
import java.awt.Dimension; 
import java.awt.Container; 
import java.awt.FlowLayout; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
    public class Gui { 
static JFrame aWindow = new JFrame("This Is Me:"); 
    public static void main(String[] args) { 
    int windowWidth = 600;          
    int windowHeight = 500;          
    aWindow.setBounds(500,500, windowWidth, windowHeight);  
    aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
FlowLayout flow = new FlowLayout();        
Container content = aWindow.getContentPane();     
content.setLayout(flow);            
    for(int i = 1; i <= 5; i++) { 
      content.add(new JButton("Picture " + i));}   
    String path = "__2 copy.jpg"; 
    aWindow.setVisible(true);         
    } 
} 

回答

4

我怎么可以让它出现画面当我点击“图片1”?

您需要先将一个ActionListener附加到按钮上。在actionPerformed方法中,您需要加载图像。

加载图像的最佳方式是使用ImageIO API。如何实现这取决于图像的存储方式。

如果图像存储应用程序JAR中(捆绑资源,见下文),你可以使用类似...

ImageIO.read(getClass().getResource("/path/to/image.jpg"); 

如果外部存储......

ImageIO.read(new File("relative/path/to/image.jpg"); 

加载图像后,您可以使用ImageIcon来包装由ImageIO加载的图像并将其应用于JLabel

看看...

了解更多详情。

当然,您可以创建自己的图像组件来显示图像,但这是一个相当先进的话题,我不确定这是否是您想要的。

这些图片应放在哪里,以便程序知道 在哪里找到它们?

这取决于。你想要图像总是可用的程序,或者你想改变它们吗?

如果图像不可能经常更改,可以将它们作为内部资源嵌入到Jar文件中。这是如何完成的取决于你如何构建应用程序,但一般而言,你可以在你的项目源代码中创建一个目录并将图像放置在那里。然后您可以通过Classloader引用图像。

如果您希望快速更改图像(无需重新构建应用程序),然后将它们存储在与应用程序Jar相同的目录中。这将允许你引用它们为File期从

更新以实例为相对位置(即new File("Image.jpg");

enter image description here

import java.awt.BorderLayout; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.FlowLayout; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import javax.imageio.ImageIO; 
import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JOptionPane; 
import javax.swing.JPanel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class TestImages { 

    public static void main(String[] args) { 
     new TestImages(); 
    } 

    public TestImages() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(new TestPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class TestPane extends JPanel { 

     private JLabel lblPic; 

     public TestPane() { 
      setLayout(new BorderLayout()); 
      JPanel buttons = new JPanel(new FlowLayout(FlowLayout.CENTER)); 
      JButton btnFile = new JButton("Load from file"); 
      JButton btnResource = new JButton("Load from resource"); 

      buttons.add(btnFile); 
      buttons.add(btnResource); 

      btnFile.addActionListener(new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        try { 
         BufferedImage image = ImageIO.read(new File("Pony01.png")); 
         lblPic.setIcon(new ImageIcon(image)); 
        } catch (Exception exp) { 
         JOptionPane.showMessageDialog(TestPane.this, "Failed to load image", "Fail", JOptionPane.ERROR_MESSAGE); 
         exp.printStackTrace(); 
        } 
       } 
      }); 

      btnResource.addActionListener(new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        try { 
         BufferedImage image = ImageIO.read(getClass().getResource("/Pony02.png")); 
         lblPic.setIcon(new ImageIcon(image)); 
        } catch (Exception exp) { 
         JOptionPane.showMessageDialog(TestPane.this, "Failed to load image", "Fail", JOptionPane.ERROR_MESSAGE); 
         exp.printStackTrace(); 
        } 
       } 
      }); 

      lblPic = new JLabel(); 
      lblPic.setVerticalAlignment(JLabel.CENTER); 
      lblPic.setHorizontalAlignment(JLabel.CENTER); 
      add(lblPic); 
      add(buttons, BorderLayout.NORTH); 
     } 

     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(300, 300); 
     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      Graphics2D g2d = (Graphics2D) g.create(); 
      g2d.dispose(); 
     } 
    } 
} 

你会很明显,需要提供自己的图片。嵌入式资源应该生活在你的源代码的顶层文件夹中(通常称为default包)

+0

感谢您的意见,对不起,我仍然是真正的新手,我明白我需要包括一个ActionListener,但我是不确定我把它放在哪里,或者如何做整条线。此外,我添加了图像IO,但它有一个错误,我包括一个导入 – 2013-04-11 01:45:20

+0

因此,我链接的资源都没有帮助你? – MadProgrammer 2013-04-11 02:07:49

+0

+1链接到所有教程。太糟糕了,OP没有花时间阅读它们并下载示例程序。我无法想象你的例子和教程例子给了什么问题。 – camickr 2013-04-11 02:13:25

相关问题