?
Solved

Java call a class

Posted on 2009-02-20
2
Medium Priority
?
1,129 Views
Last Modified: 2013-11-23
Hi Everyone,

I have two Java apps. One is called "General" and the other is called "TempCalc".

General has some buttons that when you click open other programs such as "Hot Tub".

However, the TempCalc isn't opening. It works fine on it's own, but I can't call it, and I can't figure out what I missed.

Thanks
Mark
//I haven't began to "clean this up" at all, so things are wacky that's why.
 
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
 
import java.awt.event.*;
import java.awt.*;
import java.util.Date;
import java.util.TimeZone;
import java.text.SimpleDateFormat;
 
 
@SuppressWarnings("serial")
public class General extends JFrame
{
	private JFrame mainFrame;
	private JButton cmdExit;
	private JLabel labelDate;
	private JButton Help;
	private JButton HotTubs;
	private JButton Pools;
	private JButton TempCalc;
	GridLayout generalLaoyout = new GridLayout(0,2);
		
	@SuppressWarnings("deprecation")
	public General()//default constructor
	{
		
		
		//Date
		SimpleDateFormat SDF;
		SDF = new SimpleDateFormat("MMMM dd, yyyy");
		TimeZone local = TimeZone.getDefault();
		SDF.setTimeZone(local);
		String dateString = SDF.format(new Date() );
		String date = dateString;
		
		mainFrame = new JFrame("Landscape Program");
		
		Help = new JButton("Help");
		HotTubs = new JButton("Hot Tubs");
		Pools = new JButton("Pools");
		TempCalc = new JButton("Temp Calc");
		cmdExit = new JButton("Exit");
		labelDate = new JLabel ("Today's Date is: " + date);
		
		Container c = mainFrame.getContentPane();
		
		c.setLayout(new FlowLayout());
		
		c.add(Pools);
		c.add(HotTubs);
		c.add(TempCalc);
		c.add(Help);
		c.add(cmdExit);
		c.add(labelDate);
		
		cmdExit.setMnemonic('E');
		HotTubs.setMnemonic('B');
		Pools.setMnemonic('P');
		TempCalc.setMnemonic('T');
		Help.setMnemonic('H');
		
		
		mainFrame.setSize(300,300);
		
		mainFrame.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent E){System.exit(0);}
		});
		
		ButtonsHandler bhandler = new ButtonsHandler();
		cmdExit.addActionListener(bhandler);
		Help.addActionListener(bhandler);
		Pools.addActionListener(bhandler);
		HotTubs.addActionListener(bhandler);
		TempCalc.addActionListener(bhandler);
		
		mainFrame.show();
	}
	
	class ButtonsHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			if(e.getSource() == cmdExit)
				System.exit(0);
			if(e.getSource() == Help)
				System.exit(0);
			if(e.getSource() == HotTubs)
				new HotTubs();
			if(e.getSource() == Pools)
				new poolCalc();
			if(e.getSource() == TempCalc)
				new TempCalc();
			
		}
	}
	public static void main(String[] args)
		{
			@SuppressWarnings("unused")
			General app;
			app = new General();
		}
		
}
 
 
//The TempCalc app is below
 
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
public class TempCalc extends JFrame
{
	private Thermometer thermo = new Thermometer();
	private JLabel fahrLabel = new JLabel("Degrees Fahrenheit");
	private JLabel celsiusLabel = new JLabel("Degrees Celsius");
	private JTextField fahrField = new JTextField("32.0");
	private JTextField celsiusField = new JTextField("0.0");
	private JButton fahrButton = new JButton("Convert >>>");
	private JButton celsiusButton = new JButton("<<< Convert");
 
	public TempCalc()
	{
		JPanel dataPanel = new JPanel(new GridLayout(2, 2, 12, 6));
		dataPanel.add(fahrLabel);
		dataPanel.add(celsiusLabel);
		dataPanel.add(fahrField);
		dataPanel.add(celsiusField);
 
		JPanel buttonPanel = new JPanel();
		buttonPanel.add(fahrButton);
		buttonPanel.add(celsiusButton);
		Container container = getContentPane();
		container.add(dataPanel, BorderLayout.CENTER);
		container.add(buttonPanel, BorderLayout.SOUTH);
		fahrButton.addActionListener(new FahrenheitListener());
		celsiusButton.addActionListener(new CelsiusListener());
	}
 
	class Thermometer
	{
		private double degreesCelsius;
		
		public void setCelsius(double degrees)
		{
			degreesCelsius = degrees;
		}
 
		public void setFahrenheit(double degrees)
		{
			degreesCelsius = (degrees - 32.0) * 5.0 / 9.0;
		}
 
		public double getCelsius()
		{
			return degreesCelsius;
		}
 
		public double getFahrenheit()
		{
			return degreesCelsius * (9.0 / 5.0) + 32.0;
		}
	}
	
	private class FahrenheitListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			try
			{
				String input = fahrField.getText();
				double fahr = Double.parseDouble(input);
				thermo.setFahrenheit(fahr);
				double celsius = thermo.getCelsius();
				celsiusField.setText(String.format("%.2f", celsius));
			}
			catch(Exception ex)
			{
				JOptionPane.showMessageDialog(null, "ERROR: Input Syntax");
			}
		}
	}
 
	private class CelsiusListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			String input = celsiusField.getText();
			double celsius = Double.parseDouble(input);
			thermo.setCelsius(celsius);
			double fahr = thermo.getFahrenheit();
			fahrField.setText(String.format("%.2f", fahr));
		}
	}
 
	public static void main(String [] args)
	{
		TempCalc theGUI = new TempCalc();
		theGUI.setTitle("F to C Converter");
		theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		theGUI.pack();
		theGUI.setVisible(true);
	}
}

Open in new window

0
Comment
Question by:IncubusJax
2 Comments
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 23697658
>                                 new TempCalc();

you need to do whats done in main, or just call main

TempCalc.main(null);

0
 

Author Comment

by:IncubusJax
ID: 23697669
You know I was having a major brain fart. That's exactly what I did. Durr... ;)

Thanks!
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

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 functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month16 days, 8 hours left to enroll

864 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