简体   繁体   English

单击JButton显示图像

[英]Displaying image on clicking JButton

Im trying to display an image upon clicking a JButton but upon execution the image is not displayed when button is clicked. 我试图在单击JButton时显示图像,但是在执行时单击按钮时不显示图像。

    import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
import java.awt.*;

public class new2 extends JFrame implements ActionListener
{
private boolean b1,b2;  

Container contentPane= getContentPane();
JButton awar=new JButton("@war");
JButton arrow=new JButton("arrow");
private Image image1,image2;

public new2()
    {
    setSize(400, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        contentPane.setLayout(new FlowLayout());

        awar.addActionListener(this);
        contentPane.add(awar).setVisible(true);

        arrow.addActionListener(this);
        contentPane.add(arrow).setVisible(true);

        }

public void init()
    {


    image1=Toolkit.getDefaultToolkit().getImage("@war.jpeg");
    image2=Toolkit.getDefaultToolkit().getImage("arrow.gif");
    }


public void paint(Graphics g)
{

    if(b1==true)
    {
    g.drawImage(image1,0,0,this);
    }
    else if(b2==true)
    {
    g.drawImage(image2,0,0,this);
    }

}

public void actionPerformed(ActionEvent event)
{

        String actionCommand = event.getActionCommand();

    if(actionCommand.equals("@war"))
    {
    b1=true;
    }
    else if(actionCommand.equals("arrow"))
    {
    b2=true;
    }
}

public static void main(String args[])
{
new2 m=new new2();
m.setVisible(true);
}

} }

..display an image upon clicking a JButton but upon execution the image is not displayed when button is clicked. 单击JButton时显示图像,但是在执行时单击按钮时不显示图像。

Use a JToggleButton as shown here . 使用JToggleButton所示这里

The buttons should set the boolean variables, but your paint2 is never called after the action preformed method. 这些按钮应该设置布尔变量,但是在执行操作的方法之后,绝不会调用paint2。 Secondly, it doesn't really even paint anything, it never gets graphics and will cause NPEs. 其次,它实际上甚至都不会绘画任何东西,它永远不会获得图形并会导致NPE。

You should override the JFrame.paint(Graphics g) method. 您应该重写JFrame.paint(Graphics g)方法。 Whenever you want to refresh the content of the JFrame instance call the JFrame.repaint() method. 每当您要刷新JFrame实例的内容时,请调用JFrame.repaint()方法。

/**
 * 
 */
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 MyFrame 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 MyFrame(String filename) {
        setTitle("MyWindow");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setSize(800, 600);

        this.image = new ImageIcon(filename).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, 0, 0, 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() {
                MyFrame frame = new MyFrame("resources/image.jpg");
                frame.setVisible(true);
            }
        });
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM