Reputation: 89
I am trying to use The Jbutton action listener clicks for to set my pressed variable from 0 to 1,2, or 3. if this number becomes switched then will the suites of cards on the text field. My problem is not setting my pressed variable equal to a new number and just keeping it at 0.
My main file
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package cardapp;
/**
*
* @author Angela
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class CardApp extends JFrame implements ActionListener {
private JButton oneButton,
twoButton,
threeButton;
private int width = 25;
private int height = 15;
private JTextField TextField = new JTextField(3);
Hand handObject = new Hand();
private int pressedNumber;
public CardApp() {
JPanel buttonPanel = new JPanel(new GridLayout(1, 3));
oneButton = new JButton("1");
twoButton = new JButton("2");
threeButton = new JButton("3");
// Listen for events on each button
oneButton.addActionListener(this);
twoButton.addActionListener(this);
threeButton.addActionListener(this);
// Add each to the panel of buttons
buttonPanel.add(oneButton);
buttonPanel.add(twoButton);
buttonPanel.add(threeButton);
// Add everything to a main panel attached to the content pane
JPanel mainPanel = new JPanel(new BorderLayout());
getContentPane().add(mainPanel);
mainPanel.add(TextField, BorderLayout.CENTER);
mainPanel.add(buttonPanel, BorderLayout.SOUTH);
setTitle("Sabacc Example by Angela Rucci");
setSize(375, 200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
handObject.Discards(pressedNumber);
TextField.setText(handObject.ListOfCards());
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == oneButton){
pressedNumber = 1;
handObject.Discards(pressedNumber);
}
if (e.getSource() == twoButton){
pressedNumber = 2;
handObject.Discards(pressedNumber);
}
if (e.getSource() == threeButton){
pressedNumber = 3;
handObject.Discards(pressedNumber);
}
TextField.setText(handObject.ListOfCards());
}
public static void main(String[] args) {
CardApp c = new CardApp();
}
}
my file where it will generate a new suit for the pressed button.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package cardapp;
import java.util.Random;
//import javax.swing.JButton;
import javax.swing.JOptionPane;
//import javax.swing.*;
//import java.awt.*;
//import java.awt.event.*;
/**
*
* @author Angela
*/
public class Hand {
String [] Suits = {"C", "H", "S", "D"};
String [] probability = {"C","H","R","S","D"};
Random randomInt = new Random ();
String RandomSuit;
String RandomShuffle;
String ThreeSuits;
String LeftSuit;
String MiddleSuit;
String RightSuit;
int pressed;
public int Discards(int pressedNumber){
Randomizer();
return pressed;
}
public void Randomizer (){
int RandomSuitNumber = randomInt.nextInt(4);//this is generator a random number
//------------------Decide what hand to randomize --------------------------//
if (pressed==1){
LeftSuit= Suits[RandomSuitNumber];
}
if (pressed==2){
MiddleSuit=Suits[RandomSuitNumber];
}
if (pressed==3){
RightSuit=Suits[RandomSuitNumber];}
//----------------20% chance of new random set------------------------------------//
int ProabilityRandomNum = randomInt.nextInt(5);
RandomShuffle= probability[ProabilityRandomNum];
//------------If proability array equals R then change all of the suits----------//
if (RandomShuffle.equals("R") || pressed== 0){
JOptionPane.showMessageDialog(null, "Randomized Hand!");
int leftNumber = randomInt.nextInt(4);
int middleNumber = randomInt.nextInt(4);
int rightNumber = randomInt.nextInt(4);
LeftSuit= Suits[leftNumber];
MiddleSuit= Suits[middleNumber];
RightSuit= Suits[rightNumber];}
ThreeSuits = (LeftSuit + MiddleSuit + RightSuit);
}
public String ListOfCards (){
return ThreeSuits;
}
public void GameOver(){
if (LeftSuit == MiddleSuit && MiddleSuit == RightSuit && RightSuit== LeftSuit){
JOptionPane.showMessageDialog(null, "WINNER!!");
}
}
}
Upvotes: 2
Views: 1118
Reputation: 347332
You pass the value of pressedNumber
from CardApp
to Hand
, but never actually use it...
public int Discards(int pressedNumber) {
Randomizer();
return pressed;
}
Did you mean to do something like...
public int Discards(int pressedNumber) {
pressed = pressedNumber;
Randomizer();
return pressed;
}
Upvotes: 4