java - Jbutton Action 监听器

标签 java image swing jbutton frame

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

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

最佳答案

不要覆盖update,这不是在Swing 中实现绘画的方式。尝试直接绘制到顶级容器(如 JFrame)充其量是有问题的。

相反,从 JPanel 开始并使用它的 paintComponent 方法。确保您也调用了 super.paintComponent

事实上,您可能只使用 JLabel 来显示图像。

看一看;

更多详情

更新了示例

我仍然认为 JLabel 会是更简单的解决方案,但我知道什么。

Before After

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

}

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

绘制还涉及绘制子组件(如按钮)和边框,您可以通过不调用 super.update 方便地丢弃它们。

关于java - Jbutton Action 监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21036624/

相关文章:

java - ARCore – 光估计

使用多种方法的Java构造函数重载

java - 如何为 ResteasyClient/HttpClient keystore 指定证书别名?

image - 将任意正方形包装成矩形

java - 将graphics.Images转换为org.zkoss.image.Image

python - 为什么调整图像大小会导致 channel 数增加?

java - Win 上带有 JMenuItem setHorizo​​ntalTextPosition 的双图标

Java Swing 将类动态加载到面板中

java - 当尺寸未知时有效加载位图

java - 直接将XML记录显示到jtable记录