0

I'm stuck using Button and JButton

expected result.. ( all information working ) https://i.stack.imgur.com/3qX7v.png and what I have https://i.stack.imgur.com/a4gao.png

I want to replace this:

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

To This:

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

I'm stuck because when I replace Button to JButton the program doesn't work properly...when I use this JButton Question: How to replace the button with picture and that program still work properly...

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());
        }
    }




}
Reimeus
  • 158,255
  • 15
  • 216
  • 276
Denis Nurasy
  • 41
  • 1
  • 8
  • 1
    *What exactly* doesn't work properly? What is supposed to happen; what is happening instead? Also, please reduce your code sample to the minimum required to reproduce your problem. – O. R. Mapper Mar 19 '13 at 21:50
  • when I replace button with Jbutton the actual code doesn't work, I mean the the random numbers that have to show up not working and the counting not working – Denis Nurasy Mar 19 '13 at 21:53
  • 1
    could you elaborate on the actual problem in terms of error messages, exceptions, compiler warnings, expected result vs. actual result, etc, etc? – tttthomasssss Mar 19 '13 at 22:07
  • expected result.. ( all information working ) http://i.stack.imgur.com/3qX7v.png and what I have http://i.stack.imgur.com/a4gao.png – Denis Nurasy Mar 19 '13 at 22:14

1 Answers1

3

The problem is that you are using getActionCommand to determine which JButton was clicked but this property is not set by default in Swing. You would have to call

butRock.setActionCommand("Rock");

java.awt.Button automatically uses the label property as the ActionCommand if it not explicitly set. From the docs

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


Some asides:

  • Better to avoid mixing heavy & lightweight components and use lightweight components throughout the application.
  • As your Rock, Paper, Scissors buttons have identical functionality, consider using an Action.
  • A direct instance JFrame is typically used rather than extending the class
  • Use Initial Threads
  • Window#show is deprecated. Use Window#setVisible.
  • Don't use System.exit - See more here
Community
  • 1
  • 1
Reimeus
  • 158,255
  • 15
  • 216
  • 276