通过点击JButton显示图像 [英] Show image by click JButton

查看:140
本文介绍了通过点击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();

容器容器= 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);

返回面板;
}

@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在我的地图控制器,但我不知道如何做到这一点。

 包视图; 

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;
私人JButton守护者
私人JPanel面板;
private Container window = getContentPane();
私人KeeperController控制器;


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

}

}

按钮保持需要在点击时显示图像。

解决方案

同样地,其他人总是使用 JLabel 来显示图像。这样就可以在需要时添加/删除它们,而不是绘画。此外,在你的代码中,你覆盖 paint(...),对于 Swing 我们更喜欢覆盖 paintComponent(...)方法相应的 JComponent 如果所述组件有一个。



这里尝试这个代码,我分离了Controller部分,你可能会得到一些想法,关于如何做的事情:

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

容器容器= 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);

返回面板;
}

/ **
* @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实现ActionListener
{
private JButton showButton;
私人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);
}
}
}

此代码表示您如何阅读使用 ImageIO URL

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

容器容器= 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);

返回面板;
}

/ **
* @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实现ActionListener
{
private JButton showButton;
私人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(异常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 从不使用 NORTH EAST ,<$ c $对于BorderLayout,c> WEST 和 SOUTH 它们已被 PAGE_START LINE_START LINE_END PAGE_END



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




  • PAGE_START

  • PAGE_END

  • LINESTART

  • LINE_END

  • CENTER



版本说明:在JDK 1.4版本之前,各个区域的首选名称是不同的,从指南针的点(例如,顶部区域的BorderLayout.NORTH)到Wordier的我们在例子中使用的常数。我们的示例使用的常量是首选,因为它们是标准的,并使程序能够适应具有不同方向的语言。



目录结构: / p>

 您的项目
| |
class src
| |
img * .class(或包文件夹)

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


I have a problem. I don't know how to display an image by clicking a JButton.

I have a class which can show and hide an image:

/**
 * 
 */
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);
            }
        });
    }
}

I'm working with MVC so I want the code for the JButton in my map for controllers but I don't know how to do this.

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

        }

}

The button keeper needs to show the image when it is clicked.

解决方案

As said likewise, by others, always use JLabel to display images. That way it's easy to add/remove them as an when needed, instead of painting. Moreover, in your code you are overriding paint(...), for Swing we prefer to override paintComponent(...) method of the respective JComponent if the said component in question has one.

Here try this code, I had separated the Controller part, you might get some idea, as to how to do things :

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

This code represents how you read using ImageIO and URL,

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

Moreover, when you are using BorderLayout never use NORTH, EAST, WEST and SOUTH for BorderLayout. They have been replaced with PAGE_START, LINE_START, LINE_END and PAGE_END respectively.

A BorderLayout object has five areas. These areas are specified by the BorderLayout constants:

  • PAGE_START
  • PAGE_END
  • LINESTART
  • LINE_END
  • CENTER

Version note: Before JDK release 1.4, the preferred names for the various areas were different, ranging from points of the compass (for example, BorderLayout.NORTH for the top area) to wordier versions of the constants we use in our examples. The constants our examples use are preferred because they are standard and enable programs to adjust to languages that have different orientations.

Directory Structure :

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

Now use getClass().getResource("/img/star.png");

这篇关于通过点击JButton显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆