Solved

Java Interface Question

Posted on 1998-05-19
7
231 Views
Last Modified: 2013-11-23
I am trying to organize the code in one of my applets so that the initial graphics screen generation is done in a separate class from the "main" class. Here is a simple example of my problem. I am trying to get the Hammer class to draw a label on the screen - but it will not. If I put the new_label code in the "main" classTest class it works fine. I think I am missing some simple fundamental concept here.

import java.applet.Applet;

public class classTest extends Applet
{
    public void init()
    {
        Hammer h = new Hammer();
    }

}
class Hammer extends Frame
{
    Hammer()
    {  
        java.awt.Label new_label;
       
            setLayout(null);
              new_label = new java.awt.Label("New Label");
            new_label.reshape(5,5,100,18);
            add(new_label);
            }
}

Thanks
boones
0
Comment
Question by:boones
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 4

Accepted Solution

by:
evijay earned 250 total points
ID: 1221242
You forgot one small thing. You have to add statement

h.setVisible(true);

in the init () method after creating hammer object.


0
 
LVL 2

Expert Comment

by:aziz061097
ID: 1221243
Did you try doing a h.setVisible(true) in the init ?
0
 
LVL 4

Expert Comment

by:evijay
ID: 1221244
May be you need to try h.setVisible(true) in start() method of applet not in init. I am sorry for that error.

0
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 
LVL 2

Expert Comment

by:aziz061097
ID: 1221245
The following skeleton is something you can use if you want a solution with Interfaces.

public interface ITestInterface
{
      public abstract void
              Method1 (
 
                  );
      public abstract void
            Method2(
                  );
}




public class TestFrame extends Frame implements ITestInterface
 {
//Implement your UI here


  public void Method1 (){
                       }
 

  public void Method2 (){
                       }
}



public class LogicTest {
      public LogicTest(ITestInterface callingFrame ){
      _callingFrame=callingFrame ;

      _callingFrame.Method1();

}
}
0
 

Author Comment

by:boones
ID: 1221246
Evijay,

Your answer does make the code work but not the way I expected. I will give you additional points if you can answer me this. When the code is executed it creates a separate window for my program I do not want this. I do not want a separate window for my applet I want the applet to run in the browser window. How can I tweak my code for this while maintaining the screen initialization calls in the separate class? Thanks.

Aziz, thank you for your comments, they also have been helpful.

Boones
0
 
LVL 4

Expert Comment

by:evijay
ID: 1221247

        import java.applet.Applet;

        public class classTest extends Applet
        {
            public void init()
            {
                Hammer h = new Hammer(this);
            }

        }
        class Hammer
        {
           Applet m_app;
            Hammer(Applet app)
            {    
              m_app = app;            
   java.awt.Label new_label;
                 
       app. setLayout(null);
                new_label = new java.awt.Label("New Label");
        new_label.reshape(5,5,100,18);
        app.add(new_label);
        }
        }

this may require some modification. try it.

0
 

Author Comment

by:boones
ID: 1221248
Adjusted points to 250
0

Featured Post

Independent Software Vendors: 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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

724 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