Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Unable to create an instance!!  RSS feed

 
Jacob Thomas
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a main Welcome class from which I try to create two instances. However I am unable to create the objects

****************************************************************************
This is the source code for Welcome class
import java.awt.*;
import java.awt.event.*;

// This class contains the welcome buttons
public class Welcome
{
public static void main(String args[])
{
Button b;
ActionListener al = new ButtonAction();
Frame f = new Frame("Welcome");

f.add(b = new Button("Digital Clock"), BorderLayout.NORTH);
b.setActionCommand("Clock");
b.addActionListener(al);
f.add(b = new Button("Stopwatch"), BorderLayout.CENTER);
b.setActionCommand("Watch");
b.addActionListener(al);
f.add(b = new Button("Quit"), BorderLayout.SOUTH);
b.setActionCommand("Quit");
b.addActionListener(al);
f.pack();
f.show();
}
}

class ButtonAction implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();

if (s.equals("Clock"))
new DigitalClock();
else if (s.equals("Watch"))
new Stopwatch();
else if (s.equals("Quit"))
System.exit(0);
}
}
***************************************************************************
***************************************************************************
This is the source code for Digital Clock class
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class DigitalClock extends Applet
{
//Variable decalrations
Panel p = new Panel();

Label Date = new Label();
Label Time = new Label();

String days[]={"SUN","MON","TUES","WEDNES","THURS","FRI","SATUR"};
String months[]={"JAN","FEB","MAR","APR","MAY","JUN",
"JUL","AUG","SEP","OCT","NOV","DEC"};

ClockUpdate ticking = new ClockUpdate();
// Initialization Parameters
public void init()
{
setLayout(new GridLayout(3,3));
p.setLayout(new GridLayout(3,3));

p.add(new Label("DIGITAL CLOCK",Label.CENTER));
p.add(Date);
p.add(Time);
add(p);

ticking.start();
}
// Main function. Creates a new instance of Digital Clock
public static void main(String args[])
{
DigitalClock dc = new DigitalClock();

Frame f = new Frame("Digital Clock");
f.add(dc, BorderLayout.CENTER);
f.setSize(250, 200);
f.setVisible(true);
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);

dc.init();
dc.start();

f.setVisible(true);
}


// Clock thread which updates the thread every second
class ClockUpdate extends Thread
{
public void run()
{
for(;
{
Calendar time = Calendar.getInstance();

// Obtaining the month, day and year for displaying
String month = months[time.get(Calendar.MONTH)];
String day = days[time.get(Calendar.DAY_OF_WEEK)-1];
String year = ""+(time.get(Calendar.YEAR));

// Obtaining the hour, minutes and second in 24 hour
format
String hours = ""+(time.get(Calendar.HOUR_OF_DAY));
if (Integer.parseInt(hours) <=9) hours="0"+hours;
String mins = ""+(time.get(Calendar.MINUTE));
if(Integer.parseInt(mins) <= 9) mins="0"+mins;
String secs = ""+(time.get(Calendar.SECOND));
if(Integer.parseInt(secs) <= 9) secs="0"+secs;

// Displaying the date and time
Date.setText(month + "-" + year + " " + day +"DAY");
Date.setAlignment(Date.CENTER);

Time.setText("Current Time - "+hours + ":" + mins + ":" +
secs+" Hours");
Time.setAlignment(Time.CENTER);

try
{
sleep(100);
}
catch (Exception e) {}
}
}
}

}
****************************************************************************

Could anybody please tell me why I am not able to create the object from the Welcome class?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jacob,
Welcome to JavaRanch!
To create an instance of "Welcome", you'd say
Welcome w1 = new Welcome();
and I see neither any reason why this wouldn't work, nor any place in your code where you've attempted it. Can you explain in a bit more detail what you tried to do and what problem you observed?
 
Jacob Thomas
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
What I am trying to do in Welcome class is to create an instance of DigitalClock using

new DigitalClock()
Ref code:

class ButtonAction implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
if (s.equals("Clock"))
new DigitalClock();
else if (s.equals("Watch"))
new Stopwatch();
else if (s.equals("Quit"))
System.exit(0);
}
}
I am unable to create an instance of DigitalClock() from Welcome class.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you day you're unable to create it -- does the code not compile, give a runtime error or (as I suspect) simply appear to do nothing when executed?
If it's the latter, the problem is that an Applet isn't a top-level window -- it's a component that must be displayed inside a separate window object, just like what the main() routine in DigitalClock itself does. The code in Welcome would have to do pretty much the same thing -- create a Frame, add the DigitalClock to the Frame, set the size of the Frame, make the Frame visible, and then call init() and start() on the DigitalClock. Simply calling "new DigitalClock()" does indeed create an object, but it's not an object you can see.
To reiterate: the event handler in Welcome needs to do basically everything that's in DigitalClock.main(), not just the one step it's currently doing.
 
Jacob Thomas
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ernest,
Thanks very much. I guess my program will work if I call the main() method of DigitalClock class from Welcome class.
ie, if I replace the line
new DigitalClock()
with

DigitalClock.main() ,
will it work?
Also what exactly is the syntax for calling the main() method from Welcome class?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could do that, yes. You just need a String array to pass as an argument; you could just say
DigitalClock.main(new String[0]);
 
Jacob Thomas
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks very much Ernest. When I run the Welcome class, I get a applet with 3 buttons,
DigitalClock
Stopwatch
Quit
When I click on the DigitalClock, it creates a new applet window for the DigitalClock. But if I close this applet window, the main applet window Welcome also closes. What do you account for this? And if you want the applet window Welcome to be still visible even after you close the DigitalClock, what should you do?
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to the Swing / JFC / AWT forum, where they just love to get all messy with this GUI stuff...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!