?
Solved

Why doesn't my java applet work?

Posted on 2009-04-19
4
Medium Priority
?
467 Views
Last Modified: 2013-11-23
I am creating an applet that will display a name in blue the first time a button is pushed then seem to disappear and at the same time draw the name again in larger font and gray. I tried using different words so I could figure out what was going on but no luck.
0
Comment
Question by:mabcoleman
  • 2
  • 2
4 Comments
 
LVL 92

Expert Comment

by:objects
ID: 24181307
can u post your code

0
 

Author Comment

by:mabcoleman
ID: 24181349

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class JBlueGray extends JApplet implements ActionListener
{
     JButton pressMe = new JButton("Press Me");
   String showString;
   String string1 =("Myself");
   String string2 =("Myself");
	String string3 = ("Myself");
   public void init()
   {
     
     Container con = getContentPane();
     con.setLayout(new FlowLayout());
     con.add(pressMe);
     pressMe.addActionListener(this);
     showString = string2;
	  showString = string3;
   }
   public void paint(Graphics g)
   {
     Graphics2D g2D = (Graphics2D)g;
     Font arialFont = new Font("Arial", Font.BOLD, 26);
     g2D.setFont(arialFont);
	  Font arialFont1 = new Font("Arial", Font.ITALIC, 46);
     g2D.setColor(Color.WHITE);
     g2D.drawString(showString, 15, 100);
	  
     if(showString.equals(string1))
	  
     {
        showString = string2;
 
     }
	 	  
else if(showString.equals(string2))
{
 
        showString = string3;
            }
     g2D.setColor(Color.GRAY);
	  g2D.setFont(arialFont1);
     g2D.drawString(showString, 15, 100);
     pressMe.repaint();
	  
   }
	else 
	{
		showString = string1;
		}		
		g2D.setColor(Color.BLUE);
		g3D.drawString(showString, 15, 100);
		pressMe.repaint();
		
	   public void actionPerformed(ActionEvent e)
   {
     repaint();
   }
}
 
   
 
 
	

Open in new window

0
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 24181381
try something like this (that code doesnt cpompile)

import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JApplet;
import javax.swing.JButton;

public class JBlueGray extends JApplet implements ActionListener {
      JButton pressMe = new JButton("Press Me");
      String showString;
      String string1 = ("Myself");
      String string2 = ("Myself");
      String string3 = ("Myself");

      public void init() {

            Container con = getContentPane();
            con.setLayout(new FlowLayout());
            con.add(pressMe);
            pressMe.addActionListener(this);
            showString = string2;
            showString = string3;
      }

      public void paint(Graphics g) {
            Graphics2D g2D = (Graphics2D) g;
            Font arialFont = new Font("Arial", Font.BOLD, 26);
            g2D.setFont(arialFont);
            Font arialFont1 = new Font("Arial", Font.ITALIC, 46);
            g2D.setColor(Color.WHITE);
            g2D.drawString(showString, 15, 100);

            if (showString.equals(string1))

            {
                  showString = string2;

            }

            else if (showString.equals(string2)) {

                  showString = string3;
                  g2D.setColor(Color.GRAY);
                  g2D.setFont(arialFont1);
                  g2D.drawString(showString, 15, 100);
                  pressMe.repaint();

            } else {
                  showString = string1;
            }
            g2D.setColor(Color.BLUE);
            g2D.drawString(showString, 15, 100);
            pressMe.repaint();
      }

      public void actionPerformed(ActionEvent e) {
            repaint();
      }
}

0
 

Author Comment

by:mabcoleman
ID: 24181408
Thanks I think I have it now. I'm still not sure why the other wouldn't compile when I added the else if statement.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses
Course of the Month13 days, 11 hours left to enroll

755 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question