3 Replies - 327 Views - Last Post: 20 December 2013 - 07:05 AM Rate Topic: -----

#1 Spiked Penguin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 19-December 13

Going to new class

Posted 20 December 2013 - 12:40 AM

Ok, so I have a class for when the user selects to make a new game but, how do I link the classes.

I have my main.java then I imported the class import Options.newGame; now I just need to put the reference in my if statement. Sorry it's been a while since I did this kind of thing. I found a post from a while ago on another forum I use to use and I can't tell what's going on. Forgot so much :(

import java.util.Scanner;
import Options.exitGame;
import Options.loadGame;
import Options.newGame;

public class main {
	
	public static void main(String[] args){
		Scanner choice = new Scanner(System.in);
		String input;
		
		System.out.println("\t\t\tWelcome to Fantasy World.");
		System.out.println("Select what you would like to do.");
		System.out.println("1) New Game");
		System.out.println("2) Load Game");
		System.out.println("3) Exit Game");
		
		input = choice.nextLine();
		
		if(choice == 1){
		
			newGame.newGame();
		}
	}

}



The code is incomplete.

Sorry, I couldn't find the edit button (if there is one). I feel like I have to make an object for the class like my other example shows. If you wish for it to be provided I can get it.

Is This A Good Question/Topic? 0
  • +

Replies To: Going to new class

#2 x68zeppelin80x  Icon User is offline

  • D.I.C Addict

Reputation: 130
  • View blog
  • Posts: 576
  • Joined: 07-March 09

Re: Going to new class

Posted 20 December 2013 - 04:19 AM

  • What does Options class look like?
  • What does your package structure look like?


You have not provided enough information. It appears as if you want to either statically import fields/method from the Options class or you are incorrectly instantiating the newGame object...

This post has been edited by x68zeppelin80x: 20 December 2013 - 05:11 AM

Was This Post Helpful? 0
  • +
  • -

#3 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,689
  • Joined: 13-March 10

Re: Going to new class

Posted 20 December 2013 - 06:30 AM

Your if-statement looks wrong.
Was This Post Helpful? 0
  • +
  • -

#4 burakaltr  Icon User is online

  • D.I.C Regular

Reputation: 91
  • View blog
  • Posts: 274
  • Joined: 07-November 10

Re: Going to new class

Posted 20 December 2013 - 07:05 AM

import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
public class AnonymousClass implements ActionListener {
    JButton button;
    int count = 0;
    public static void main(String[] args) {
        AnonymousClass gui = new AnonymousClass();
        gui.go();
    }
    public void go() {
        button = new JButton("Click me!");
        JFrame frame = new JFrame();
        frame.setLayout(new GridBagLayout());
        frame.getContentPane().add(button);

        frame.setSize(500, 500);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         
        
       
        button.addActionListener(new out(this, count));
    }}
    

class out implements ActionListener {
    AnonymousClass clas;
    int count;
    out(AnonymousClass clas, int count) {
        this.clas = clas;
        this.count = count;
    }@
    Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        clas.button.setText("I've been clicked " + count + " times");

    }

}



You should pass the current Object to the other class by using ' this ' keyword

in the fashion above
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1