Hi guys, I'm working on my first real project with a GUI but I feel that my code is confusing and I tend to be jumping from class to class a lot. Could someone please take a look and see if everything is okay? I'm mainly worried that I shouldn't be jumping back and forth all the time
The program is not finished yet so some things may be wrong or not completed. There is a lot of code >.< sorry about that, I've separated the different classes as well.
import import java.awt.GridLayout; import javax.swing.JFrame; // Brings all the code together public class MainFrame extends JFrame{ private Player player; private Generate generate; private GameListener listener; public MainFrame(){ super("Hi-Lo game"); setLayout(new GridLayout(2,2,3,3)); // Creating instances of the 2 main classes here player = new Player(); add(player); generate = new Generate(); add(generate); //Not sure if this is correct. It will allow me to use the same instances listener = new GameListener(player, generate); setSize(400, 200); setVisible(true); setResizable(false); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; // Made into a panel to allow a nice layout public class Player extends JPanel{ //Buttons for the player class as well as displays on bet/bank protected static JButton bet5Button; protected static JButton bet10Button; protected static JLabel displayBet; protected static JLabel displayBank; protected static JButton high; protected static JButton low; private static JOptionPane error; private static int bank = 100; private static int bet = 0; int[] nums ={2,3,4,}; public Player(){ bet5Button = new JButton("Bet 5"); bet10Button = new JButton("Bet 10"); displayBet = new JLabel("Bet: " + bet); displayBank = new JLabel("Bank: " + bank); high = new JButton("Hi"); low = new JButton("Lo"); add(bet5Button); add(bet10Button); add(displayBet); add(displayBank); add(high); add(low); } // This will check what button has been pressed and remove/update money and bets public void removeAmount(JButton button){ if(button == bet5Button){ bank = bank - 5; bet = bet + 5; }else if(button == bet10Button){ bank = bank - 10; bet = bet + 10; } checker(); // Checks if the bet is allowed etc and will display errors etc displayBet.setText("Bank: " + bank); displayBank.setText("Bet: " + bet); // This shall update the label } public void checker(){ if(bank <=0 && bet == 0){ // Test this JOptionPane.showMessageDialog(error, "Game over"); }else if(bank < bet && bank <= 0){ JOptionPane.showMessageDialog(error, "Not enough money!"); } } }
import java.util.ArrayList; import java.util.Random; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JPanel; public class Generate extends JPanel{ private static JLabel generatedNumber; private static Random rand; // Array will store all the random numbers, will allow the program to check protected static ArrayList<Integer> generatedArray = new ArrayList<Integer>(); private int generatedConvert; public Generate(){ // Generates random number, may change this at a later date. rand = new Random(); generatedConvert = rand.nextInt(11); generatedNumber = new JLabel("Number " + generatedConvert); add(generatedNumber); } // Will generate the numbers for the game and store them in the array public void randNumber(){ generatedConvert = rand.nextInt(11); generatedArray.add(generatedConvert); generatedNumber.setText("Number " + generatedConvert); } // Not yet complete public void numberChecker(JButton button){ int get1 = generatedArray.get(generatedArray.size()-1); // Need to fix this!!!! int get2 = generatedArray.get(generatedArray.size()-2); if(button == Player.high){ if(get1 > get2){ System.out.println("Testing"); } } } }
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class GameListener implements ActionListener { private Player player; private Generate generate; // Constructor takes the instances public GameListener(Player player, Generate generate){ this.player = player; this.generate = generate; // Adding the listeners to get input and adjust game Player.high.addActionListener(this); Player.low.addActionListener(this); Player.bet5Button.addActionListener(this); Player.bet10Button.addActionListener(this); } @Override // Performs all the actions with the GUI public void actionPerformed(ActionEvent e) { if(e.getSource() == Player.bet5Button){ player.removeAmount(Player.bet5Button); }else if(e.getSource() == Player.bet10Button){ player.removeAmount(Player.bet10Button); } if(e.getSource() == Player.high){ generate.randNumber(); generate.numberChecker(Player.high); } } }