点击显示图片JButton

问题描述:

我有一个问题。我不知道如何通过单击JButton来显示图像。点击显示图片JButton

我有一个类可以显示和隐藏的图像:

/** 
* 
*/ 
package com.samples; 

import java.awt.BorderLayout; 
import java.awt.Container; 
import java.awt.EventQueue; 
import java.awt.FlowLayout; 
import java.awt.Graphics; 
import java.awt.Image; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.WindowConstants; 

/** 
* @author 
* 
*/ 
public class New2 extends JFrame implements ActionListener { 

    private static String SHOW_ACTION = "show"; 
    private static String HIDE_ACTION = "hide"; 

    private Image image = null; 
    private boolean showImage = false; 

    public New2(String filename) { 
     setTitle("MyWindow"); 
     setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
     setSize(800, 600); 

     this.image = new ImageIcon("..//src/img/Ster.png").getImage(); 

     Container container = getContentPane(); 
     container.setLayout(new BorderLayout()); 
     container.add(createControls(), BorderLayout.SOUTH); 
    } 

    private JPanel createControls() { 
     JButton showButton = new JButton("Show"); 
     showButton.addActionListener(this); 
     showButton.setActionCommand(SHOW_ACTION); 

     JButton hideButton = new JButton("Hide"); 
     hideButton.addActionListener(this); 
     hideButton.setActionCommand(HIDE_ACTION); 

     JPanel panel = new JPanel(); 
     panel.setLayout(new FlowLayout(FlowLayout.CENTER)); 

     panel.add(showButton); 
     panel.add(hideButton); 

     return panel; 
    } 

    @Override 
    public void paint(Graphics g) { 
     super.paint(g); 

     if (showImage) { 
      g.drawImage(image, 100, 200, image.getWidth(null), image.getHeight(null), null); 
     } 
    } 

    @Override 
    public void actionPerformed(ActionEvent event) { 
     String actionCommand = event.getActionCommand(); 

     if (SHOW_ACTION.equals(actionCommand)) { 
      showImage = true; 
     } else if (HIDE_ACTION.equals(actionCommand)) { 
      showImage = false; 
     } 

     repaint(); 
    } 

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

      @Override 
      public void run() { 
       New2 frame = new New2("resources/image.jpg"); 
       frame.setVisible(true); 
      } 
     }); 
    } 
} 

我与MVC的工作,所以我想在我的地图控制器对JButton的代码,但我不知道该怎么做这个。

package View; 

import java.awt.Color; 
import java.awt.Container; 
import java.awt.Dimension; 
import java.awt.Font; 
import java.awt.Image; 
import java.awt.event.ActionListener; 

import javax.swing.BorderFactory; 
import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JMenu; 
import javax.swing.JMenuBar; 
import javax.swing.JMenuItem; 
import javax.swing.JPanel; 


import Controller.HomeController; 
import Controller.KeeperController; 

public class Selectie extends JFrame{ 

    private JLabel label, label1, label2; 
    private JButton keeper; 
    private JPanel panel; 
    private Container window = getContentPane(); 
    private KeeperController controller; 


    public Selectie() 
    { 
     initGUI(); 

    } 

    public void initGUI() 
    { 
     setLayout(null); 
     setTitle(); 
     setSize(800,600); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     label = new JLabel();  
     label.setBounds(0, 0, 266, 800); 
     label.setBackground(Color.RED); 
     label.setOpaque(true); 
     window.add(label); 

     label1 = new JLabel(); 
     label1.setBounds(266, 0, 266, 800); 
     label1.setBackground(Color.BLACK); 
     label1.setOpaque(true); 
     window.add(label1); 

     label2 = new JLabel(); 
     label2.setBounds(532, 0, 266, 800); 
     label2.setBackground(Color.RED); 
     label2.setOpaque(true); 
     window.add(label2); 

     keeper = new JButton("1. "+""+" Kenneth Vermeer"); 
     keeper.setBounds(60, 500, 200, 25); 
     keeper.setFocusable(false); 
     keeper.setBorderPainted(false); 
     keeper.setContentAreaFilled(false); 
     keeper.setFont(new Font("Arial",Font.PLAIN,17)); 
     label.add(keeper); 

     } 

} 

按钮管理器在点击时需要显示图像。

+0

你需要的类'New2'还是仅仅是一个例子吗?在这种情况下,我们宁愿看看控制器类('Controller.HomeController','Controller.KeeperController'),然后'New2'。 – 2012-03-15 14:25:13

+0

@lsmail:我添加了代表目录结构的图表,以及'路径'应该如何访问图像。看一看:-) – 2012-03-16 11:20:25

正如其他人所说的,总是使用JLabel来显示图像。这样就很容易在需要时添加/删除它们,而不是绘画。此外,在您的代码中,您优先于paint(...),对于Swing,如果所讨论的组件有一个,我们优先覆盖各自JComponentpaintComponent(...)方法。

这里试试这个代码,我已经分手的控制器部分,你可能会得到一些想法,至于如何做的事情:

import java.awt.BorderLayout; 
import java.awt.Container; 
import java.awt.EventQueue; 
import java.awt.FlowLayout; 
import java.awt.Graphics; 
import java.awt.Image; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.Icon; 
import javax.swing.JButton; 
import javax.swing.JLabel; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 
import javax.swing.UIManager; 
import javax.swing.WindowConstants; 

/** 
* @author 
* 
*/ 
public class New2 extends JFrame 
{ 

    private static String SHOW_ACTION = "show"; 
    private static String HIDE_ACTION = "hide"; 

    public New2(String filename) 
    { 
     setTitle("MyWindow"); 
     setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
     setSize(800, 600);   

     Container container = getContentPane(); 
     container.setLayout(new BorderLayout()); 
     container.add(createControls(), BorderLayout.CENTER); 
    } 

    private JPanel createControls() 
    { 
     JButton showButton = new JButton("Show");   
     showButton.setActionCommand(SHOW_ACTION); 

     JButton hideButton = new JButton("Hide");   
     hideButton.setActionCommand(HIDE_ACTION); 

     JLabel imageLabel = new JLabel(); 

     New2Controller n2c = new New2Controller(showButton 
                     , hideButton, imageLabel); 
     showButton.addActionListener(n2c);   
     hideButton.addActionListener(n2c); 

     JPanel panel = new JPanel(); 
     panel.setLayout(new FlowLayout(FlowLayout.CENTER)); 

     panel.add(imageLabel); 
     panel.add(showButton); 
     panel.add(hideButton); 

     return panel; 
    } 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) 
    { 
     EventQueue.invokeLater(new Runnable() 
     { 
      @Override 
      public void run() 
      { 
       New2 frame = new New2("/img/image.jpg"); 
       frame.setVisible(true); 
      } 
     }); 
    } 
} 

class New2Controller implements ActionListener 
{ 
    private JButton showButton; 
    private JButton hideButton; 
    private JLabel imageLabel; 
    private static String SHOW_ACTION = "show"; 
    private static String HIDE_ACTION = "hide"; 
    private Icon infoIcon = UIManager.getIcon("OptionPane.informationIcon"); 

    public New2Controller(JButton show, JButton hide, JLabel label) 
    { 
     showButton = show; 
     hideButton = hide; 
     imageLabel = label; 
    } 

    public void actionPerformed(ActionEvent event) 
    { 
     String actionCommand = event.getActionCommand(); 

     if (SHOW_ACTION.equals(actionCommand)) 
     { 
      SwingUtilities.invokeLater(new Runnable() 
      { 
       public void run() 
       {      
        imageLabel.setIcon(infoIcon); 
       } 
      }); 
     } 
     else if (HIDE_ACTION.equals(actionCommand)) 
     { 
      imageLabel.setIcon(null); 
     } 
    } 
} 

这个代码表示你读了如何使用ImageIOURL

import java.awt.BorderLayout; 
import java.awt.Container; 
import java.awt.EventQueue; 
import java.awt.FlowLayout; 
import java.awt.Graphics; 
import java.awt.Image; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JLabel; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 
import javax.swing.WindowConstants; 

import javax.imageio.ImageIO; 

/** 
 * @author 
 * 
 */ 
public class New2 extends JFrame 
{ 
    private static String SHOW_ACTION = "show"; 
    private static String HIDE_ACTION = "hide"; 

    public New2(String filename)  
    { 
        setTitle("MyWindow"); 
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
        setSize(800, 600);         

        Container container = getContentPane(); 
        container.setLayout(new BorderLayout()); 
        container.add(createControls(), BorderLayout.CENTER); 
    } 

    private JPanel createControls()  
    { 
        JButton showButton = new JButton("Show");         
        showButton.setActionCommand(SHOW_ACTION); 

        JButton hideButton = new JButton("Hide");         
        hideButton.setActionCommand(HIDE_ACTION); 

     JLabel imageLabel = new JLabel(); 

     New2Controller n2c = new New2Controller(showButton 
             , hideButton, imageLabel); 
     showButton.addActionListener(n2c);   
     hideButton.addActionListener(n2c); 

     JPanel panel = new JPanel(); 
        panel.setLayout(new FlowLayout(FlowLayout.CENTER)); 

     panel.add(imageLabel); 
        panel.add(showButton); 
        panel.add(hideButton); 

        return panel; 
    } 

    /** 
     * @param args 
     */ 
    public static void main(String[] args)  
    { 
     EventQueue.invokeLater(new Runnable()  
     { 
      @Override 
      public void run()  
      { 
       New2 frame = new New2("/img/image.jpg"); 
       frame.setVisible(true); 
      } 
     }); 
    } 
} 

class New2Controller implements ActionListener 
{ 
    private JButton showButton; 
    private JButton hideButton; 
    private JLabel imageLabel; 
    private Image image; 
    private ImageIcon imageIcon; 
    private static String SHOW_ACTION = "show"; 
    private static String HIDE_ACTION = "hide"; 

    public New2Controller(JButton show, JButton hide, JLabel label) 
    { 
     showButton = show; 
     hideButton = hide; 
     imageLabel = label; 
     try 
     { 
      image = ImageIO.read(getClass().getResource("/img/caIcon.png")); 
     } 
     catch(Exception e) 
     { 
      e.printStackTrace(); 
     } 
     imageIcon = new ImageIcon(image); 
    } 

    public void actionPerformed(ActionEvent event) 
    { 
     String actionCommand = event.getActionCommand(); 

        if (SHOW_ACTION.equals(actionCommand))  
     { 
      SwingUtilities.invokeLater(new Runnable() 
      { 
       public void run() 
       {      
        imageLabel.setIcon(imageIcon); 
       } 
      }); 
     }  
     else if (HIDE_ACTION.equals(actionCommand))  
     { 
      imageLabel.setIcon(null); 
        } 
    } 
} 

此外,当您使用BorderLayout从未使用BorderLayout的NORTHEASTWESTSOUTH。它们分别被替换为PAGE_START,LINE_START,LINE_ENDPAGE_END

BorderLayout对象有五个区域。这些区域由BorderLayout的常数规定:

  • PAGE_START
  • PAGE_END
  • LINESTART
  • LINE_END
  • 中心

版本注释:JDK 1.4版之前,首选各个地区的名称是不同的,从指南针的角度来看(例如, mple,BorderLayout.NORTH用于顶部区域)转换为我们在示例中使用的常量的较为理想的版本。我们的例子使用的常量是首选,因为它们是标准的,并且使程序能够适应具有不同方向的语言。

目录结构:

       Your Project 
           |   | 
           classes  src 
           |  | 
          img *.class(or package Folder) 

现在使用getClass().getResource("/img/star.png");

+1

基于图像的例子的一个好方法是在互联网上通过热链接(通过'URL')或者从JRE使用的图标中拉出它们。 – 2012-03-15 14:59:30

+1

@AndrewThompson:啊哈,是的,就像我见过你做了那么多次:-),那是件好事。像往常一样,有时候你的头像只能朝一个方向滚动,因为它习惯于这样做。三江源把我的头往另一个方向:-)和不保持微笑:-) – 2012-03-15 15:26:10

+0

*“见过你这样做,很多时候” *从JRE拉的图标是一个聪明的伎俩,mKorbel给我看,但我总是忘记在哪里找到他们的细节。 :( – 2012-03-15 15:32:20

您可以简单地添加一个JLable来在其上显示图像。在此之后,您可以根据条件设置可见JLabel。

1)使用JLabel#setIcon()代替绘画图像作为背景,以JFrame

2)创建方法分成class Selectie

private void setIconToLabel (Icon icon){ 

    myDesiredLabel.setIcon(icon); 
} 

3)不创建新JFrame为另一图像使用CardLayout instaed

+0

@Ismail:这是迄今为止最好的答案。 – 2012-03-15 14:18:42