Java重绘图像

发布于 2024-10-14 10:59:35 字数 533 浏览 4 评论 0原文

我的脚本有问题;我想在按下按钮时重新绘制一个新图像(显示另一个图像),但该按钮没有执行任何操作......

ActionListener one = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                panel2.revalidate();
                panel2.repaint();
            }
        };

        btn1.addActionListener(one);



        JLabel test1 = new JLabel(myDeckOfCards.giveCardPlayer1().getImage());

        panel2.add(lab1);
        panel2.add(test1);
        panel2.add(pn5);
        panel2.add(pn1);
        panel2.add(btn1);

I have a problem with my script; I want repaint a new image (another one is shown) when a button is pressed, but the button doesn't do anything...

ActionListener one = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                panel2.revalidate();
                panel2.repaint();
            }
        };

        btn1.addActionListener(one);



        JLabel test1 = new JLabel(myDeckOfCards.giveCardPlayer1().getImage());

        panel2.add(lab1);
        panel2.add(test1);
        panel2.add(pn5);
        panel2.add(pn1);
        panel2.add(btn1);

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

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

发布评论

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

评论(1

伴梦长久 2024-10-21 10:59:35

actionPerformed 中,您需要获取 JLabel 并对其调用 setIcon(),传入新图像。

有几种方法可以获取 JLabel,一种是确保声明了一个 final 变量以将其包含在 actionPerformed 方法范围内的某个位置,另一种是查找从 panel2 内部(不推荐)。

如果您为此目的声明了一个成熟的类,您还可以通过构造函数将其传递给您的 ActionListener

编辑

final JLabel test1 = new JLabel(myDeckOfCards.giveCardPlayer1().getImage());

ActionListener one = new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        // Get 'anotherIcon' from somewhere, presumably from a similar
        // place to where you retrieved the initial icon
        test1.setIcon(anotherIcon);
    }
};

btn1.addActionListener(one);

panel2.add(lab1);
panel2.add(test1);
panel2.add(pn5);
panel2.add(pn1);
panel2.add(btn1);

Inside actionPerformed you need to get hold of your JLabel and call setIcon() on it, passing in the new image.

There's a few ways to get the JLabel, one is to make sure you have a final variable declared to contain it somewhere in scope of your actionPerformed method, and another is to find it from inside the panel2 (not recommended).

You could also pass it in to your ActionListener through a constructor if you declare a full-fledged class for that purpose.

EDIT:

final JLabel test1 = new JLabel(myDeckOfCards.giveCardPlayer1().getImage());

ActionListener one = new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        // Get 'anotherIcon' from somewhere, presumably from a similar
        // place to where you retrieved the initial icon
        test1.setIcon(anotherIcon);
    }
};

btn1.addActionListener(one);

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