Solved

Java Interface Question

Posted on 1998-05-19
7
225 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
  • 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
egit plugin on eclipse 8 82
login jsp example 24 68
expectj telnet failing 5 45
Bot application - advice 3 38
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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 …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
The viewer will learn how to implement Singleton Design Pattern in Java.

828 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