Jbutton作用听众 [英] Jbutton acction listener

查看:115
本文介绍了Jbutton作用听众的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个表单。有一个按钮,当点击按钮时,会出现一个指定的照片。我的问题是,当我单击按钮时,弹出图片,如果光标通过表格边界,图像就会消失。这是我的代码:

I am trying to create a form. there is a button that when clicking the button, a photo which is specified would appear. my problem is, when I click the button, the picture pops up and if the cursor passes the form boundary, the image disappears. here is my code:

import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import static java.lang.Math.abs;
import static java.lang.Math.min;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;

public class SeamCarving extends JFrame
{     
public static void main(String[] args) throws IOException {
    final BufferedImage input = ImageIO.read(new File("path"));

    final BufferedImage[] toPaint = new BufferedImage[]{input};
    final Frame frame = new Frame("Seams") {

        @Override
        public void update(Graphics g) {
            final BufferedImage im = toPaint[0];
            if (im != null) {
                g.clearRect(0, 0, getWidth(), getHeight());
                g.drawImage(im, 0, 0, this);
            }
        }
    };
    frame.setSize(input.getWidth(), input.getHeight());
    frame.setVisible(true);
    frame.add(startButton);

    startButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            BufferedImage out = input;
            out = deleteVerticalSeam(out);
            toPaint[0] = out;
            frame.repaint();
            System.out.println("Do Something Clicked");
        }
    });
 }
}


推荐答案

Don 't override update ,这不是在Swing中实现绘画的方式。尝试直接绘制到顶级容器(如 JFrame )最多是有问题的。

Don't override update, this isn't how painting is achieved in Swing. Attempting to paint directly to a top level container like JFrame is problematic at best.

相反,以 JPanel 并改为使用它的 paintComponent 方法。确保你也调用 super.paintComponent

Instead, start with a JPanel and use it's paintComponent method instead. Make sure you call super.paintComponent as well.

事实上,你可能只需要使用 JLabel 代替显示图片。

In fact, you could probably just use a JLabel to display the image instead.

看看;

  • Performing Custom Painting
  • How to use labels

更多详情

更新为示例

我仍然认为 JLabel 会更简单,但我知道什么。

I still think a JLabel would be simpler solution, but what do I know.

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class SeamCarving {

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

    public SeamCarving() {
        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 BufferedImage input;
        private BufferedImage[] toPaint;

        public TestPane() {
            try {
                input = ImageIO.read(new File("C:\\hold\\thumbnails\\2005-09-29-3957.jpeg"));
                toPaint = new BufferedImage[1];
            } catch (IOException ex) {
                ex.printStackTrace();
            }

            setLayout(new GridBagLayout());
            JButton startButton = new JButton("Start");
            startButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    BufferedImage out = input;
                    out = input; //deleteVerticalSeam(out);
                    toPaint[0] = out;
                    repaint();
                    System.out.println("Do Something Clicked");
                }
            });
            add(startButton);
        }

        @Override
        public Dimension getPreferredSize() {
            return input == null ? new Dimension(400, 400) : new Dimension(input.getWidth(), input.getHeight());
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (toPaint[0] != null) {
                Graphics2D g2d = (Graphics2D) g.create();
                g2d.drawImage(input, 0, 0, this);
                g2d.dispose();
            }
        }
    }

}

覆盖更新的问题是 paint 子系统可以选择避免调用并最终调用直接绘画,绕过你的画作。

The problem with overriding update is the paint subsystem can choose to avoid calling and end up calling paint directly, circumventing your painting.

绘画还涉及绘制儿童组件(如你的按钮)和边框,你已经通过不调用 super.update 来方便地丢弃。

Painting also involves painting child components (like your button) and borders, which you've conveniently discarded by not calling super.update.

这篇关于Jbutton作用听众的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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