Solved

Java Interface Question

Posted on 1998-05-19
7
221 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Python Assistance 7 80
mockito example issue 8 72
nextBoolean(double p) for Random class 3 40
hibernate example for saving data 19 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)
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…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
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.

770 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