java - 将 Button 更改为 JButton 无法修复它(((

标签 java image swing button jbutton

我无法使用 Button 和 JButton

预期结果..(所有信息有效) /image/3qX7v.png 和我有什么 /image/a4gao.png

我想替换这个:

Button butRock = new Button("Rock");
butRock.addActionListener(this);
ButtPan.add(butRock);

对此:

BufferedImage buttonIcon = ImageIO.read(new File("rock.jpg"));
JButton butRock = new JButton(new ImageIcon(buttonIcon));
butRock.addActionListener(this);
ButtPan.add(butRock);

我陷入困境,因为当我将 Button 替换为 JButton 时,程序无法正常工作...当我使用此 JButton 时 问题:如何用图片替换按钮,程序仍然可以正常运行...

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
public class gui extends JFrame implements ActionListener
{
    public JLabel JWoL,JWoLPlayer,JWoLPC,JNumWin,JNumLose,JNumTie, logo;
    public JLabel JWinT,JLoseT,JTieT, rpsP, rpsC, result;
    JPanel LabelsPan;
    static final int WINS = 0, LOSSES = 1, DRAWS = 2;
    int[] counts = new int[3];
    String[] strings = {"| You Win", "| You Lose", "| Draw"};
    JLabel[] labels = {JNumWin, JNumLose, JNumTie};


    @SuppressWarnings("deprecation")
    public static void main(String[] args) throws IOException
    {
        gui theWindow = new gui();
        theWindow.show();
    }

    private void record(int type)
    {
        JWoL.setText(strings[type]);
        counts[type]++;
        labels[type].setText(""+counts[type]);
    }

    public gui() throws IOException
    {
        JPanel logo = new JPanel();
        logo.setLayout(new GridLayout(1,1));

        BufferedImage myPicture = ImageIO.read(new File("logo.jpg"));
        JLabel picLabel = new JLabel(new ImageIcon( myPicture ));
        add(picLabel);


        JPanel ButtPan=new JPanel();
        ButtPan.setLayout(new GridLayout(1,3));


        BufferedImage buttonIcon = ImageIO.read(new File("rock.jpg"));
        JButton butRock = new JButton(new ImageIcon(buttonIcon));
        butRock.addActionListener(this);
        ButtPan.add(butRock);

        Button butPaper = new Button("Paper");
        butPaper.addActionListener(this);
        ButtPan.add(butPaper);

        Button butScissors = new Button("Scissors");
        butScissors.addActionListener(this);
        ButtPan.add(butScissors);


        JWoLPlayer = new JLabel();
        JWoLPC = new JLabel();
        JWoL= new JLabel();


        JLabel rpsPlayer= new JLabel("| Your Choice:  ");
        JLabel rpsComputer= new JLabel("| Computers Choice:  ");
        setTitle("| RoPaS GAME |");


        LabelsPan=new JPanel();
        LabelsPan.setLayout(new GridLayout(3,2));

        rpsP = new JLabel();
        LabelsPan.add(rpsPlayer);
        LabelsPan.add(JWoLPlayer);

        rpsC = new JLabel();
        LabelsPan.add(rpsComputer);
        LabelsPan.add(JWoLPC);

        result =new JLabel();
        LabelsPan.add(JWoL,"Center");


        JPanel WLPan = new JPanel();
        WLPan.setLayout(new GridLayout(3, 2));


        JWinT = new JLabel("Wins: ");
        JLoseT = new JLabel("Losses: ");
        JTieT = new JLabel("Ties: ");

        WLPan.add(JWinT);
        JNumWin = new JLabel();
        WLPan.add(JNumWin);
        WLPan.add(JLoseT);

        JNumLose = new JLabel();
        WLPan.add(JNumLose);
        WLPan.add(JTieT);

        JNumTie = new JLabel();
        WLPan.add(JNumTie);
        JLabel[] labels1 = {JNumWin, JNumLose, JNumTie};
        labels = labels1;


        JPanel TwoPanesN1=new JPanel();

        TwoPanesN1.setLayout(new BorderLayout());
        TwoPanesN1.add(logo,"North");
        TwoPanesN1.add(LabelsPan,"West");
        TwoPanesN1.add(WLPan,"East");

        getContentPane().setLayout(new GridLayout(3,2));
        getContentPane().add(ButtPan);
        getContentPane().add(TwoPanesN1);


        Font fontDisplay = new Font("Arial", Font.BOLD, 16);
        JWoL.setFont(fontDisplay);
        LabelsPan.setFont(fontDisplay);
        setSize(400,250);
        setVisible(true);
        setResizable(false);

        addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent ev){System.exit(0);}});
    }



    public void Play(String PlayerChoice)
    {
        String PCchoice=PCansw();
        JWoLPC.setText(PCchoice);

        if(PlayerChoice.equals(PCchoice))
            record(DRAWS);


        else if(PlayerChoice.equals("Rock"))
            if(PCchoice.equals("Paper"))
                record(LOSSES);
            else
                record(WINS);


        else if(PlayerChoice.equals("Paper"))
            if(PCchoice.equals("Scissors"))
                record(LOSSES);
            else
                record(WINS);


        else if(PlayerChoice.equals("Scissors"))
            if(PCchoice.equals("Rock"))
                record(LOSSES);
            else
                record(WINS);

    }



    public String PCansw()
    {
        String rpsPC2="";
        int rpsPC=(int)(Math.random( )*3)+1;
        if(rpsPC==1)
            rpsPC2= "Rock";
        else if(rpsPC==2)
            rpsPC2= "Paper";
        else if(rpsPC==3)
            rpsPC2= "Scissors";
        return rpsPC2;
    }


    public void actionPerformed(ActionEvent e)
    {
        if(e.getActionCommand().equals("Exit"))
            System.exit(0);
        else
        {
            JWoLPlayer.setText(e.getActionCommand());
            Play(e.getActionCommand());
        }
    }




}

最佳答案

问题是您正在使用 getActionCommand 来确定单击了哪个 JButton,但在 Swing 中默认情况下未设置此属性。你必须打电话

butRock.setActionCommand("Rock");
如果未显式设置,

java.awt.Button 会自动使用 label 属性作为 ActionCommand。来自 docs

If the command name is null (default) then this method returns the label of the button.

<小时/>

一些旁白:

关于java - 将 Button 更改为 JButton 无法修复它(((,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15511092/

相关文章:

java.lang.IllegalStateException : getInputStream() has already been called for this request

java - 如何实现图像无限循环?

java - 当选择/未选择单选按钮时如何隐藏文本字段?

Java Swing 重绘图像

java - 如何获取Eclipse的源代码

java - spring-boot 中的logging.*的@ConfigurationProperties

image - 找到适合图像上绘制的多边形的最大正方形的最佳方法

c# - C#中图像到字节数组的相对路径

java - 如何通过代码在 Netbeans 中添加自定义面板?

java - 使用 Netbeans 导出 JAR