Java swing 在 ContentPane 中交换 ImagePanel

发布于 2024-12-11 20:08:34 字数 1619 浏览 0 评论 0原文

我有一组图像想要在 JFrame 中呈现。它们的大小都相同 - 每个图像都填充 JFrame。我在可见的图层样式之间交换:

    f = new JFrame("xx");
    f.setSize(480, 854);
    contentPane = f.getContentPane();
    ip1 = new ImagePanel(new File("assets/1.jpg"));
    ip2 = new ImagePanel(new File("assets/2.jpg"));
    ip3 = new ImagePanel(new File("assets/3.jpg"));
    f.setVisible(true);
    contentPane.add(ip1);
    contentPane.addMouseListener(mouseListener);

ImagePanel 是:

public class ImagePanel extends JPanel {
    private BufferedImage image;

    @Override
    public void paintComponent(Graphics g) {
        g.drawImage(image, 0, 0, null);
    }

    public ImagePanel(File imageFile) {
        try {
            image = ImageIO.read(imageFile);
        } catch (IOException e) {
            System.out.println("Image could not be read: " + imageFile);
        }
        setVisible(true);
    }
}

我尝试在 MouseListener 中在它们之间交换:

static MouseListener mouseListener = new MouseListener() {

    private int i = 1;

    @Override
    public void mouseClicked(MouseEvent e) {
        // logger.log(Level.INFO, e.toString());
        contentPane.removeAll();
        if (++i > 3) i = 1;
        switch (i) {
            case 1: contentPane.add(ip1); break;
            case 2: contentPane.add(ip2); break;
            case 3: contentPane.add(ip3); break;            
        }
        contentPane.repaint();
    }
};

第一个图像根据需要显示。单击给我白色,第二次单击时相同,第三次单击使我返回到所需的第一张图像。如果我单击白色之间的框架,则会出现第二个和第三个图像,并且一旦渲染,它就会按预期工作。我缺少什么?

嗯,新手!毫无疑问有更好的方法...

谢谢

I have a set of images that I want to present in a JFrame. They are all the same size - each image fills the JFrame. I swap between which is visible, layer style:

    f = new JFrame("xx");
    f.setSize(480, 854);
    contentPane = f.getContentPane();
    ip1 = new ImagePanel(new File("assets/1.jpg"));
    ip2 = new ImagePanel(new File("assets/2.jpg"));
    ip3 = new ImagePanel(new File("assets/3.jpg"));
    f.setVisible(true);
    contentPane.add(ip1);
    contentPane.addMouseListener(mouseListener);

An ImagePanel is:

public class ImagePanel extends JPanel {
    private BufferedImage image;

    @Override
    public void paintComponent(Graphics g) {
        g.drawImage(image, 0, 0, null);
    }

    public ImagePanel(File imageFile) {
        try {
            image = ImageIO.read(imageFile);
        } catch (IOException e) {
            System.out.println("Image could not be read: " + imageFile);
        }
        setVisible(true);
    }
}

And I try swapping between them in the MouseListener:

static MouseListener mouseListener = new MouseListener() {

    private int i = 1;

    @Override
    public void mouseClicked(MouseEvent e) {
        // logger.log(Level.INFO, e.toString());
        contentPane.removeAll();
        if (++i > 3) i = 1;
        switch (i) {
            case 1: contentPane.add(ip1); break;
            case 2: contentPane.add(ip2); break;
            case 3: contentPane.add(ip3); break;            
        }
        contentPane.repaint();
    }
};

The first image displays as desired. Click gives me white, and same on 2nd click, and third brings me back round to my first image as desired. If I click on the frame in between clicks on the white, the 2nd and 3rd images appear, and once rendered it works as expected. What am I missing?

Ack, newbs! No doubt there is a much better way...

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

会发光的星星闪亮亮i 2024-12-18 20:08:34

将两个面板放入 CardLayout

Put both panels in a CardLayout.

花开半夏魅人心 2024-12-18 20:08:34

将您的图像作为图标 JLabel

Swing GUI 的规则

  • JComponent 已经可见,
  • 您想要在这个/这些对象之间交换(s)

那么你必须打电话

myLabel.setIcon(myIconn);
revalidate();
repaint();

put your Images as Icon to the JLabel,

rulles for Swing GUI

  • is JComponent(s) already visible
  • you want to swap betweens this/these Object(s)

then you have to call

myLabel.setIcon(myIconn);
revalidate();
repaint();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文