Java 将按钮更改为JButton can';我不能修理它(((

Java 将按钮更改为JButton can';我不能修理它(((,java,image,swing,button,jbutton,Java,Image,Swing,Button,Jbutton,我被按钮和按钮卡住了 预期结果…(所有信息均有效) 还有我所拥有的 我想替换这个: 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(but

我被按钮和按钮卡住了

预期结果…(所有信息均有效) 还有我所拥有的

我想替换这个:

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);
我被卡住了,因为当我把按钮换成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

如果命令名为null(默认值),则此方法返回按钮的标签


一些旁白:

  • 在整个应用程序中更好地避免和使用轻量级组件
  • 当你的石头、剪纸、剪刀按钮具有相同的功能时,考虑使用.
  • 通常使用直接实例
    JFrame
    ,而不是扩展类
  • 使用
  • 已弃用。请使用
    Window\setVisible
  • 不要使用
    系统。退出
    -查看更多

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

butRock.setActionCommand("Rock");
java.awt.Button
如果未显式设置,则会自动将label属性用作
ActionCommand

如果命令名为null(默认值),则此方法返回按钮的标签


一些旁白:

  • 在整个应用程序中更好地避免和使用轻量级组件
  • 当你的石头、剪纸、剪刀按钮具有相同的功能时,考虑使用.
  • 通常使用直接实例
    JFrame
    ,而不是扩展类
  • 使用
  • 已弃用。请使用
    Window\setVisible
  • 不要使用
    系统。退出
    -查看更多

到底是什么不正常工作?应该发生什么;发生了什么?另外,请将代码样本减少到再现问题所需的最小值。当我用Jbutton替换按钮时,实际的代码不工作,我是指必须显示的随机数不工作,计数不工作。您可以吗根据错误消息、异常、编译器警告、预期结果与实际结果等详细说明实际问题?预期结果…(所有信息有效)还有,我所拥有的,到底是什么工作不正常?应该发生什么;发生了什么?另外,请将代码样本减少到再现问题所需的最小值。当我用Jbutton替换按钮时,实际代码不工作,我指的是必须显示的随机数不工作,计数不工作ING您能否详细说明实际问题,包括错误消息、异常、编译器警告、预期结果与实际结果等?预期结果…(所有信息都有效)以及我所掌握的信息