Solved

Java Interface Question

Posted on 1998-05-19
7
214 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

705 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now