Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Have I laid out my code wrong?

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    18
    My Mood
    Sleepy
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Have I laid out my code wrong?

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


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Have I laid out my code wrong?

    It seems ok in this situation. Modulating the classes is usually a good thing because it makes the code more manageable long-term.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    18
    My Mood
    Sleepy
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Have I laid out my code wrong?

    Thanks for letting me know I feel pretty confident in the theory side of java and what most stuff does, I just worry that I don't present the code in the correct way.

Similar Threads

  1. [SOLVED] can anyone help me what is wrong with my code?
    By marc172 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 22nd, 2013, 09:53 PM
  2. what is wrong with this code?
    By Linus in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 18th, 2013, 05:28 PM
  3. What's wrong with my code!? seriously...
    By MLIAKIRA in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 23rd, 2013, 02:21 PM
  4. What's wrong with my code :(
    By ZDreamer08 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 27th, 2013, 08:59 AM
  5. What's Wrong With My Code?
    By arunjib in forum What's Wrong With My Code?
    Replies: 18
    Last Post: May 7th, 2011, 08:47 PM