java - 单击 JFrame 中的 JButton 时如何更改现有图像? ( java )

标签 java image swing jframe

所以我正在制作一个游戏,我想在单击 JButton 时更改屏幕上已有图像的图像,但我尝试的任何方法都不起作用,所以我来这里寻求帮助。

这是我的代码:

package frametest;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class FrameTest extends JFrame implements ActionListener {

    JPanel totalGUI, player1, buttons;
    JLabel p1a, p1b;
    JButton change1, change2;
    ImageIcon C2 = new ImageIcon("Pictures\\cards\\C2.png");
    ImageIcon SK = new ImageIcon("Pictures\\cards\\SK.png");    
    ImageIcon HJ = new ImageIcon("Pictures\\cards\\HJ.png");

    public JPanel createContentPane(){  
        JPanel totalGUI = new JPanel();
        totalGUI.setBackground(new Color( 52, 186, 119 ));
        totalGUI.setLayout(null);

        //Speler 1
        JPanel player1 = new JPanel();
        player1.setLocation(240,431);
        player1.setSize(190,110);
        player1.setBackground(new Color( 52, 186, 119 ));
        totalGUI.add(player1);

        JLabel p1a = new JLabel();
        p1a.setIcon(C2);
        p1a.setLocation(0,0);
        player1.add(p1a);
        pack();

        JLabel p1b = new JLabel();
        p1b.setIcon(SK);
        p1b.setLocation(0,20);
        player1.add(p1b);
        pack();

        //Knoppen
        JPanel buttons = new JPanel();
        buttons.setLayout(null);
        buttons.setLocation(700,435);
        buttons.setSize(200,90);
        totalGUI.add(buttons);

        JButton change1 = new JButton("Jack of Hearts");
        change1.setLocation(0,0);
        change1.setSize(200,30);
        change1.addActionListener(this);
        buttons.add(change1);      

        JButton change2 = new JButton("King of Spades");
        change2.setLocation(0,30);
        change2.setSize(200,30);
        change2.addActionListener(this);
        buttons.add(change2);

        totalGUI.setOpaque(true);
        return totalGUI;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == change1) {
            p1a.setIcon(HJ);
        } else if(e.getSource() == change2 ) {
            p1a.setIcon(SK);
        } 
    }

    private static void createAndShowGUI() {
        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame frame = new JFrame("Poker Game");

        FrameTest demo = new FrameTest();
        frame.setContentPane(demo.createContentPane());

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        frame.setSize(1000,600);     
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() { 
                createAndShowGUI();
            }
        });
    }}

看来我必须在这篇文章中写更多才能发布它,但我不知道该写什么......

最佳答案

问题是您在方法 createContentPane() 中重新声明了变量。

public JPanel createContentPane(){  
        JPanel totalGUI = new JPanel();
        /**/
}

应该是

public JPanel createContentPane(){  
        totalGUI = new JPanel();
        /**/
}

您必须在 createContentPane() 方法中对 player1、buttons、p1a、p1b、change1、change2 执行相同的操作。

关于java - 单击 JFrame 中的 JButton 时如何更改现有图像? ( java ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19875739/

相关文章:

java - AD via LDAP - 如何从查询中返回所有祖先组?

javascript - 如何在 Onsen-UI 中使用自定义图像/图标

java - 编辑 JFrame 中的现有图像

java - 单击按钮后更改 FlowLayout 对齐方式

java - Android/Java - ServerSocket 和 Socket 通信的 ClassNotFoundException

java - 项目中新依赖项的 NoClassDefFoundError

java - 在 java 中使用 Oauth 保护 Rest API

html - 如何使用 CSS 将 "crop"矩形图像变成正方形?

image - FFmpeg 图像到视频和流媒体

java - 调整 Jpanel 网格列的大小