#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    2
    Rep Power
    0

    Class Communication


    Okay, I 'm making a sort of game, and I'm having trouble with the classes and variables communicating with each other. I'm fairly new to Java, so if this code looks disgusting... That's why.

    Code:
    import java.awt.Color;
    import java.awt.GridLayout;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.beans.PropertyChangeEvent;
    import java.beans.PropertyChangeListener;
    import java.util.Random;
    
    import javax.swing.JButton;
    import javax.swing.JCheckBox;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JProgressBar;
    import javax.swing.JTextField;
    import javax.swing.SwingWorker;
    
    
    public class StartWindow extends JFrame implements ActionListener, PropertyChangeListener {
    
    	private static final long serialVersionUID = 1L;
    	
    	public final JLabel label1 = new JLabel("		Put your name below:");
    	public final static JTextField usernameIn = new JTextField("Steve Jobs");
    	public final JButton confirmStart = new JButton("Start Game");
    	public final static JCheckBox goToCollege = new JCheckBox("Go to College");
    	public final JLabel label2 = new JLabel("		Selecting College will increase the chances of jobs accepting you into the workplace.");
    	public final JLabel label3 = new JLabel("		Not selecting College will start you out with more money.");
    	public final JLabel skipLine = new JLabel("");
    	public final JLabel label4 = new JLabel("		Note: Once you start the game, you can not change your name unless you restart.");
    	
    	public final static JFrame frame1 = new JFrame();
    	
    	public final static String username = StartWindow.usernameIn.getText();
    
    	
    	public final static void startWindow() {
    
    		// Start
    		
    		final Toolkit toolkit;
    
    		final int WIDTH = 600;
    		final int HEIGHT = (WIDTH / 11 * 9);
    		final int ACTION_COMMAND_KEY;
    		final int close = 5;
    		
    
    		final JPanel startWindow = new JPanel();
    		GridLayout gl = new GridLayout(15, 15);
    		startWindow.setLayout(gl);
    		startWindow.setBackground(Color.CYAN);
    		
    		
    		
    		System.out.println(Game.infos + "Running...");
    		System.out.println(Game.infos + "Building Start Window...");
    
    		// Start Window Components
    
    
    			final JLabel label1 = new JLabel("		Put your name below:");
    			final JTextField usernameIn = new JTextField("Steve Jobs");
    			final JButton confirmStart = new JButton("Start Game");
    			final JCheckBox goToCollege = new JCheckBox("Go to College");
    			final JLabel label2 = new JLabel("		Selecting College will increase the chances of jobs accepting you into the workplace.");
    			final JLabel label3 = new JLabel("		Not selecting College will start you out with more money.");
    			final JLabel skipLine = new JLabel("");
    			final JLabel label4 = new JLabel("		Note: Once you start the game, you can not change your name unless you restart.");
    
    		confirmStart.setMnemonic(KeyEvent.VK_ENTER);
    		confirmStart.setEnabled(true);
    		goToCollege.setMnemonic(KeyEvent.VK_C);
    		goToCollege.setEnabled(true);
    		goToCollege.setSelected(true);
    
    		
    		
    		
    
    		// Start Window Layout
    
    		startWindow.add(label1);
    		startWindow.add(usernameIn);
    		startWindow.add(goToCollege);
    		startWindow.add(skipLine);
    		startWindow.add(label2);
    		startWindow.add(label3);
    		startWindow.add(skipLine);
    		startWindow.add(confirmStart);
    		startWindow.add(label4);
    		
    		frame1.setSize(WIDTH, HEIGHT);
    		frame1.setTitle("Winner at Life - Beginning Choices");
    		frame1.setContentPane(startWindow);
    		frame1.setVisible(true);
    		frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		
    
    		// Miscellaneous
    
    		
    		
    		confirmStart.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent event) {
    				confirmStart.setEnabled(false);
    				goToCollege.setEnabled(false);
    				usernameIn.setEnabled(false);
    				System.out.println(Game.infos + "Initializing Start...");
    				System.out.println(Game.alerts + "User name: " + username);
    				JOptionPane.showMessageDialog(startWindow,"Your name is " + username, Game.info, JOptionPane.PLAIN_MESSAGE);
    				if(goToCollege.isSelected() == true) {
    					System.out.println(Game.alerts + "College was selected");
    					JOptionPane.showMessageDialog(startWindow, "You are going to college", Game.info, JOptionPane.PLAIN_MESSAGE);
    				}
    				else if(goToCollege.isSelected() == false) {
    					System.out.println(Game.alerts + "College was not selected");
    					JOptionPane.showMessageDialog(startWindow, "You are not going to college", Game.info, JOptionPane.PLAIN_MESSAGE);
    				}
    				usernameIn.getDocument().putProperty("username", usernameIn);
    				System.out.println(Game.alerts + "Closed starting frame");
    				StartGame.startGame();
    			}
    		});
    
    		System.out.println(Game.infos + "Classes fetched!");
    		System.out.println(Game.infos + "Starting Window Building Complete!");
    		
    
    	}
    	
    
    	@Override
    	public void propertyChange(PropertyChangeEvent arg0) {
    		
    	}
    
    	@Override
    	public void actionPerformed(ActionEvent arg0) {
    		
    	}
    
    }
    Here's the other class file that's having trouble.

    Code:
    import java.awt.Color;
    import java.awt.GridLayout;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.beans.PropertyChangeEvent;
    import java.beans.PropertyChangeListener;
    import java.util.Random;
    
    import javax.swing.JButton;
    import javax.swing.JCheckBox;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JProgressBar;
    import javax.swing.JTextField;
    import javax.swing.SwingWorker;
    
    public class MainWindow extends JFrame implements ActionListener, PropertyChangeListener {
    	
    	private static final long serialVersionUID = 1L;
    
    	public final static void mainWindow() {
    		
    		final int WIDTH = 900;
    		final int HEIGHT = (WIDTH / 11 * 9);
    	
    		final JPanel mainWindow = new JPanel();
    		GridLayout gl = new GridLayout(30, 30);
    		mainWindow.setLayout(gl);
    		
    		JLabel label1 = new JLabel("		Player name: " + StartWindow.username);
    		JLabel label2 = new JLabel("		College: no");
    		JLabel label3 = new JLabel("		College: yes");
    		
    		
    		mainWindow.add(label1);
    		if(StartWindow.goToCollege.isSelected() == true) {
    			mainWindow.add(label3);
    		}
    		if(StartWindow.goToCollege.isSelected() == false) {
    			mainWindow.add(label2);
    		}
    		
    		
    		JFrame frame2 = new JFrame();
    		frame2.setTitle("Winner at Life");
    		frame2.setDefaultCloseOperation(EXIT_ON_CLOSE);
    		frame2.setSize(WIDTH, HEIGHT);
    		frame2.setContentPane(mainWindow);
    		frame2.setVisible(true);
    		
    	}
    By the way, there are other class files that just tell each other to run. I feel like that's the only communication I can get. Running methods from other classes.
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    If you are getting error messages, copy the full text and paste it here,
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    2
    Rep Power
    0
    Originally Posted by NormR
    If you are getting error messages, copy the full text and paste it here,
    I'm not getting any error messages. I click the button to start the game, and the username is incorrect. I don't know what I did, but college is working again...
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    working again
    Glad you got it working.

IMN logo majestic logo threadwatch logo seochat tools logo