Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


[java] Swing doesn't want to work in my browser (2nd try)


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
No replies to this topic

#1 BMason   Members   -  Reputation: 122

Like
Likes
Like

Posted 04 May 2000 - 05:41 PM

I tried to paste my html code in there with the
 tags but for some reason it didn''t keep it there as code. So it screwed up my post. 2nd try:

Hi. I was tasked with writing an applet for my programming class. This was basically my first time doing a "real" applet that did more than just a few lines on the screen. I did everything using the AWT components and it ran in the browser fine. Then I switched over to Swing (although there''s not much of a difference, just got to add a "J" in front of everything ).

Anyway, I rewrote it in Swing and compiled everything and when I loaded it into my browser it said "Class XXX" not found, where XXX was the name of my class that I referred to in the  tag and was sitting in the exact same folder. I couldn''t get it to work at all.

So I tried to load it using the appletviewer and it loaded flawlessly with no problems (I made no changes to the files). I thought maybe for some reason when I installed IE5 it installed the java 1.1.1 runtime files on my system and overrode the 1.2.2 files, so I downloaded the 1.2.2 runtime files and installed them and it still doesn''t run. I was hoping that someone might be able to look at my code and tell me what''s wrong. Be wary that we''re trying to consentrate on making everything as object-orientedly as possible, so there might be more classes than is really necessary here. But the actually program WORKS. It runs in the appletviewer with no problems, just not in my browser. I''m wondering if maybe my applet code is missing something that only the browser would require. Also, for all my applet-related objects I imported all the same stuff (java.awt.*, java.awt.Event.*, javax.swing.*) because this was my first time with a real applet and I wasn''t entirely sure what I would need in each file at the time. Ok, here goes:

(Oops, the HTML source loaded eventhough I used code... that''s strange. I uploaded the html source file to my ftp server here: http://home.earthlink.net/~shaggy999/applet.html)


My GuessApplet source (the actual applet):
[code]
import java.awt.*;
import java.awt.Event.*;
import javax.swing.*;

public class GuessApplet extends JApplet {

	RandomGame guessTheNumber = new RandomGame();
	appletGUI gui = new appletGUI(this);
	
	public void getNewGuess() {
	
		gui.updateOutput(guessTheNumber.nextGuess(Integer.parseInt(gui.getInput())));
		gui.dispGuesses(guessTheNumber.getGuesses());
	}

	public void startNewGame() {
	
		guessTheNumber.newGame();
		gui.dispGuesses(guessTheNumber.getGuesses());
	}
}
      

My GUI code:
import java.awt.*;
import java.awt.Event.*;
import javax.swing.*;

public class appletGUI {
	
	// JPanels that we add components to.
	private JPanel northPanel;
	private JPanel southPanel;
	private JPanel centerPanel;
	private JPanel eastPanel;
	
	// Components
	
		// Label Component
		private JLabel instructions;
		private JLabel totalGuesses;
		
		// Button Components
		private JButton getInputButton;
		private JButton newGameButton;
		
		// TextField Component
		private JTextField inputField;
		
		// TextArea Component
		private JTextArea outputField;
		
		// ScrollPane Component
		private JScrollPane scrollOutputField;
		
	// Action Listeners
	private ButtonListener buttonInputListener;
		
	
	public appletGUI(GuessApplet applet) {
		// Set up the Panels with a layout.

		initGUI(applet);
	}

	// This initializes the GUI for the applet.
	private void initGUI(GuessApplet applet) {
	
		// Initialize the Panels
		northPanel = new JPanel();
		southPanel = new JPanel();
		centerPanel = new JPanel();
		eastPanel = new JPanel();
		
		northPanel.setLayout (new GridLayout (1,2,3,3));
		southPanel.setLayout (new GridLayout (1,1));
		centerPanel.setLayout (new GridLayout (1,1,20,20));
		eastPanel.setLayout (new GridLayout (2,1,20,10));
		
		
		// Initialize the Label Components.
		instructions = new JLabel ("Guess a number between 1 and 100.");
		totalGuesses = new JLabel ("Total Guesses: 0");
		
		// Initialize the Button Components.
		getInputButton = new JButton ("Guess");
		newGameButton = new JButton ("New Game");
		
		// Initialize the TextField Component
		inputField = new JTextField (10);
		inputField.setEditable (true);
		
		// Initialize the TextArea Component
		outputField = new JTextArea (20, 30);
		outputField.setEditable (false);
		
		// Initialize the Scrollpane Component
		scrollOutputField = new JScrollPane(outputField);
		
		// Initialize the Button Listener and add it to the buttons.
		buttonInputListener = new ButtonListener(applet);
		getInputButton.addActionListener(buttonInputListener);
		newGameButton.addActionListener(buttonInputListener);
				
		// Add the Components to the panels.
		northPanel.add (instructions);
		northPanel.add (inputField);
		
		centerPanel.add (outputField);
		centerPanel.add (scrollOutputField);
		
		southPanel.add (totalGuesses);

		eastPanel.add (getInputButton);
		eastPanel.add (newGameButton);
		
		// Set up the ConentPane.
		Container contentPane = applet.getContentPane();
		contentPane.setLayout (new BorderLayout(10,10));
		contentPane.add (northPanel, BorderLayout.NORTH);
		contentPane.add (southPanel, BorderLayout.SOUTH);
		contentPane.add (centerPanel, BorderLayout.CENTER);
		contentPane.add (eastPanel, BorderLayout.EAST);

		applet.resize(450, 150);
		applet.setVisible(true);
	}
	
	public void updateOutput(String output) {
	
		String oldOutput = outputField.getText();
		outputField.setText(oldOutput + output + "\n");
	}
	
	public String getInput() {
	
		return inputField.getText();
	}
	
	public void dispGuesses(int guesses) {
		totalGuesses.setText("Total Guesses: " + guesses);
	}
}
   


My listener class:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ButtonListener implements ActionListener {

	private GuessApplet applet;
	
	public ButtonListener (GuessApplet app) {
	
		applet = app;
	}
	
	public void actionPerformed(ActionEvent event) {
	
		if(event.getActionCommand().compareTo("Guess") == 0)
			applet.getNewGuess();
		else
			applet.startNewGame();
	}
}
   


My randomgame class:
import java.util.*;

public class RandomGame {

	Random numberGenerator;
	int theAnswer, totalGuesses;

	public RandomGame () {

		totalGuesses = 0;
		theAnswer = 0;
		numberGenerator = new Random();
		newGame();
	}
	
	public void newGame () {
		totalGuesses = 0;
		theAnswer = (int)(numberGenerator.nextDouble() * 100);
	}

	public String nextGuess (int guess) {
	
		String response;
		totalGuesses++;
		
		if (guess < theAnswer)
			response = "Your guess of " + guess + " was too low.";
		else if (guess > theAnswer)
			response = "Your guess of " + guess + " was too high.";
		else {
			response = "Correct! " + guess + " is the number!\nIt took you "
						+ totalGuesses + " tries!";
			newGame();
		}
		
		return response;
	}
	
	public int getGuesses() {
	
		return totalGuesses;
	}
}
   



Sponsor:



Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS