codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
This 18 message thread spans 2 pages: [1]  2  > >  
  Conversion  blade250 at 15:10 on Tuesday, April 11, 2006
 


Recently ive been trying Java via netbeans and failing, i tired currency havnt got that to work yet, but now trying CM to Feet and inches, where do i even start? i did some programming at university a few years back, i really dont know where to start. How do i input lets say 24CM in one box, click convert and have it come out in another box in feet and inches? and vice versa.

Please Help

John

  Re: Conversion  crwood at 21:57 on Tuesday, April 11, 2006
 


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Conversion implements ActionListener
{
JTextField centimeterField;
JTextField inchField;
JButton convertToInches;
JButton convertToCentimeters;
final double centimetersPerInch = 2.54;

public void actionPerformed(ActionEvent e)
{
JButton button = (JButton)e.getSource();
if(button == convertToCentimeters)
{
String text = inchField.getText();
if(text.equals(""))
return;
double inches = Double.parseDouble(text);
double centimeters = inches * centimetersPerInch;
centimeterField.setText(String.valueOf(centimeters));
}
if(button == convertToInches)
{
String text = centimeterField.getText();
if(text.equals(""))
return;
double centimeters = Double.parseDouble(text);
double inches = centimeters / centimetersPerInch;
inchField.setText(String.valueOf(inches));
}
}

private JPanel getNorthPanel()
{
// instantiate/initialize components
centimeterField = new JTextField(16);
convertToInches = new JButton("convert to inches");
convertToInches.addActionListener(this);
// layout components
JPanel top = new JPanel();
top.add(new JLabel("centimeters:"));
top.add(centimeterField);
JPanel bottom = new JPanel();
bottom.add(convertToInches);
JPanel panel = new JPanel(new GridLayout(2,0));
panel.add(top);
panel.add(bottom);
return panel;
}

private JPanel getSouthPanel()
{
inchField = new JTextField(16);
convertToCentimeters = new JButton("convert to centimeters");
convertToCentimeters.addActionListener(this);
JPanel top = new JPanel();
top.add(new JLabel("inches:"));
top.add(inchField);
JPanel bottom = new JPanel();
bottom.add(convertToCentimeters);
JPanel panel = new JPanel(new GridLayout(2,0));
panel.add(top);
panel.add(bottom);
return panel;
}

public static void main(String[] args)
{
Conversion conversion = new Conversion();
JFrame f = new JFrame("Conversion");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(conversion.getNorthPanel(), "North");
f.getContentPane().add(conversion.getSouthPanel(), "South");
f.setSize(360,240);
f.setLocation(200,200);
f.setVisible(true);
}
}


  Re: Conversion  blade250 at 16:41 on Wednesday, April 12, 2006
 

I Copied and pasted the code to see how it would run, but i couldnt get that to work, further down the code there is a red line under:

/** Creates a new instance of Main */
public Main () {
}

which then leads to the below code, everything below is fine, but Public Main () {
}
Produces and error.

public static void main(String[] args) {
// TODO code application logic here

{
Conversion conversion = new Conversion();
JFrame f = new JFrame("Conversion");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(conversion.getNorthPanel(), "North");
f.getContentPane().add(conversion.getSouthPanel(), "South");
f.setSize(360,240);
f.setLocation(200,200);
f.setVisible(true);
}
}
}

}

  Re: Conversion  crwood at 07:19 on Thursday, April 13, 2006
 

Save the file with the same name as the class name. A class named "Main" (declared as "public class Main") would be saved as "Main.java"
You could add a "1" (one) or some other number at the end of Conversion, eg, "Conversion2" and save it with the same name and it should work okay.

public class Conversion2 implements ActionListener


  Re: Conversion  blade250 at 18:04 on Friday, April 14, 2006
 

I still get the red under what i said before for some reason?

Here is all the code ive said "<-- Red Error" where the errors are, can you help me see what the problem is here with the code? its probably somert stupid but i cant see it.

@author Owner
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Main {
public class Conversion implements ActionListener
{
JTextField centimeterField;
JTextField inchField;
JButton convertToInches;
JButton convertToCentimeters;
final double centimetersPerInch = 2.54;

public void actionPerformed(ActionEvent e)
{
JButton button = (JButton)e.getSource();
if(button == convertToCentimeters)
{
String text = inchField.getText();
if(text.equals(""))
return;
double inches = Double.parseDouble(text);
double centimeters = inches * centimetersPerInch;
centimeterField.setText(String.valueOf(centimeters));
}
if(button == convertToInches)
{
String text = centimeterField.getText();
if(text.equals(""))
return;
double centimeters = Double.parseDouble(text);
double inches = centimeters / centimetersPerInch;
inchField.setText(String.valueOf(inches));
}
}

private JPanel getNorthPanel()
{
// instantiate/initialize components
centimeterField = new JTextField(16);
convertToInches = new JButton("convert to inches");
convertToInches.addActionListener(this);
// layout components
JPanel top = new JPanel();
top.add(new JLabel("centimeters:"));
top.add(centimeterField);
JPanel bottom = new JPanel();
bottom.add(convertToInches);
JPanel panel = new JPanel(new GridLayout(2,0));
panel.add(top);
panel.add(bottom);
return panel;
}

private JPanel getSouthPanel()
{
inchField = new JTextField(16);
convertToCentimeters = new JButton("convert to centimeters");
convertToCentimeters.addActionListener(this);
JPanel top = new JPanel();
top.add(new JLabel("inches:"));
top.add(inchField);
JPanel bottom = new JPanel();
bottom.add(convertToCentimeters);
JPanel panel = new JPanel(new GridLayout(2,0));
panel.add(top);
panel.add(bottom);
return panel;
}




/** Creates a new instance of Main */

public class Main() { <-- Red Error!
}



/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Conversion conversion = new Conversion();
JFrame f = new JFrame("Conversion");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(conversion.getNorthPanel(), "North");
f.getContentPane().add(conversion.getSouthPanel(), "South");
f.setSize(360,240);
f.setLocation(200,200);
f.setVisible(true);
}
// TODO code application logic here
}

} <-- Red Error!

  Re: Conversion  crwood at 21:47 on Friday, April 14, 2006
 

Don't do this

public class Main {
public class Conversion implements ActionListener
{

Instead do either this

public class Conversion implements ActionListener
{

or this

public class Main implements ActionListener {

You only want one public class per file. If you want to run the code as class Main then replace the class name "Conversion" with the name "Main" everywhere it appears. It is not necessary/required to add a no-arg constructor; java does this for you (it is called a default constructor) when your class has no constructor. If your class has a constructor with one or more args then you must add a no-arg constructor to be able to use it since java will not provide it.

  Re: Conversion  blade250 at 16:37 on Monday, April 17, 2006
 

Thanks for your help but still under:

/** Creates a new instance of Main */
public Main() {
}

i get an error, if i change it to Conversion then:

public class Conversion implements ActionListener
{

Becomes red, if i delete it most things become red, what exactly should be under:

/** Creates a new instance of Main */
public Main() {



<Added>

Hey, Thanks ive finally got it working thanks to your help, im gona try adding a few things to it and see how it goes, Thanks so much.

John

  Re: Conversion  blade250 at 14:56 on Wednesday, April 19, 2006
 

Again Thanks for your help.

If i wanted to have CM to be converted into Feet and inches and have feet in one box and inches in another how would i go about that? im not to good at maths and cant figure out the math to get from inches to feet lol

Any ideas?

Hate java but i really need to get the hang of it.

<Added>

ok ive got that if i times what ever inch by 0.0833333333 i get the answer in feet, where in the code can this go?

  Re: Conversion  crwood at 18:17 on Wednesday, April 19, 2006
 


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Conversion2 implements ActionListener
{
JTextField footField;
JTextField inchField;
JButton convertToInches;
JButton convertToFeet;
final double inchsPerFoot = 12.0;

public void actionPerformed(ActionEvent e)
{
JButton button = (JButton)e.getSource();
if(button == convertToFeet)
{
String text = inchField.getText();
if(text.equals(""))
return;
double inches = Double.parseDouble(text);
double feet = inches / inchsPerFoot;
footField.setText(String.valueOf(feet));
}
if(button == convertToInches)
{
String text = footField.getText();
if(text.equals(""))
return;
double feet = Double.parseDouble(text);
double inches = feet * inchsPerFoot;
inchField.setText(String.valueOf(inches));
}
}

private JPanel getNorthPanel()
{
// instantiate/initialize components
footField = new JTextField(16);
convertToInches = new JButton("convert to inches");
convertToInches.addActionListener(this);
// layout components
JPanel top = new JPanel();
top.add(new JLabel("feet:"));
top.add(footField);
JPanel bottom = new JPanel();
bottom.add(convertToInches);
JPanel panel = new JPanel(new GridLayout(2,0));
panel.add(top);
panel.add(bottom);
return panel;
}

private JPanel getSouthPanel()
{
inchField = new JTextField(16);
convertToFeet = new JButton("convert to feet");
convertToFeet.addActionListener(this);
JPanel top = new JPanel();
top.add(new JLabel("inches:"));
top.add(inchField);
JPanel bottom = new JPanel();
bottom.add(convertToFeet);
JPanel panel = new JPanel(new GridLayout(2,0));
panel.add(top);
panel.add(bottom);
return panel;
}

public static void main(String[] args)
{
Conversion2 conversion = new Conversion2();
JFrame f = new JFrame("Conversion 2");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(conversion.getNorthPanel(), "North");
f.getContentPane().add(conversion.getSouthPanel(), "South");
f.pack();
f.setLocation(200,200);
f.setVisible(true);
}
}


  Re: Conversion  blade250 at 17:46 on Tuesday, April 25, 2006
 

Thanks again, im able to look at the code now and understand more, So far i can convert from CM to Inches and from Inches to Feet thats great, how would i Convert lets say 256 Cm and get a display of feet in one output box and inches in another?

Like:

256 centimeters =

[8] -Feet . [39895013] Inches

By maybe to just 2 Decimal Places.

Thanks to your help and others i can now analyze the code more, im taking a short course in Code Analysis at the momement, i think im on C++ Soon.

  Re: Conversion  crwood at 07:26 on Wednesday, April 26, 2006
 


import java.awt.*;
import java.awt.event.*;
import java.text.NumberFormat;
import javax.swing.*;

public class Conversion3 implements ActionListener
{
JTextField centimeterField;
JTextField inchField;
JTextField footField;
NumberFormat nf;
final double centimetersPerInch = 2.54;
final double inchesPerFoot = 12.0;

public Conversion3()
{
nf = NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(2);
}

public void actionPerformed(ActionEvent e)
{
JTextField textField = (JTextField)e.getSource();
double value = Double.parseDouble(textField.getText());
if(textField == centimeterField)
{
inchField.setText(nf.format(centimetersToInches(value)));
footField.setText(nf.format(centimetersToFeet(value)));
}
if(textField == inchField)
{
centimeterField.setText(nf.format(inchesToCentimeters(value)));
footField.setText(nf.format(inchesToFeet(value)));
}
if(textField == footField)
{
centimeterField.setText(nf.format(feetToCentimeters(value)));
inchField.setText(nf.format(feetToInches(value)));
}
}

private double centimetersToInches(double centimeters)
{
return centimeters / centimetersPerInch;
}

private double centimetersToFeet(double centimeters)
{
double inches = centimeters / centimetersPerInch;
return inches / inchesPerFoot;
}

private double inchesToCentimeters(double inches)
{
return inches * centimetersPerInch;
}

private double inchesToFeet(double inches)
{
return inches / inchesPerFoot;
}

private double feetToCentimeters(double feet)
{
double inches = feet * inchesPerFoot;
return inches * centimetersPerInch;
}

private double feetToInches(double feet)
{
return feet * inchesPerFoot;
}

private JPanel getContent()
{
centimeterField = new JTextField(12);
inchField = new JTextField(12);
footField = new JTextField(12);
centimeterField.addActionListener(this);
inchField.addActionListener(this);
footField.addActionListener(this);
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(2,2,2,2);
gbc.weightx = 1.0;
addComponents(new JLabel("centimeters"), centimeterField, panel, gbc);
addComponents(new JLabel("inches"), inchField, panel, gbc);
addComponents(new JLabel("feet"), footField, panel, gbc);
return panel;
}

private void addComponents(Component c1, Component c2, Container c,
GridBagConstraints gbc)
{
gbc.anchor = GridBagConstraints.EAST;
gbc.gridwidth = GridBagConstraints.RELATIVE;
c.add(c1, gbc);
gbc.anchor = GridBagConstraints.WEST;
gbc.gridwidth = GridBagConstraints.REMAINDER;
c.add(c2, gbc);
}

public static void main(String[] args)
{
JFrame f = new JFrame("enter data and press enter key");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new Conversion3().getContent());
f.setSize(300,200);
f.setLocation(200,200);
f.setVisible(true);
}
}


  Re: Conversion  crwood at 18:23 on Wednesday, April 26, 2006
 

Can you make tabs in Java?...can you show me the simplest way possible?
See How to Use Tabbed Panes in the swing tutorial.
So this would involve button groups right?
Not necessarily. You can make it that way if you want.
i want to design my own Forms in Design view. Is this possible?
Yes. There are many ways to do these things. Imagination is the key.
how would things be declared in the main class? or do you just declare what you need in each Forms code?
Generally you place one component on each tab. You can have each component in its own class or group them in a single class. As long as it makes sense to you and works you have a lot of leeway.

  Re: Conversion  blade250 at 13:37 on Thursday, April 27, 2006
 

Just been reading those guide.

Ive been in the design view and created a JTabbedPane, stretch this out and place 3 Tabs ontop by dragging 3 more JTabbedPane's this has created 3 tabs, however all are called Tab1. When i try and change the names it wont let me because it has already generated there names in blue code which cannot be altered. I cant find in the Propities where to change there labels, i can change there variable names tho. In the guide on how to use tabs i tried to copy and paste the code to see how it would run, however where does the code go? in main? on the Jframe? and under which part? Ive read quite a few java guides now and can do a few things in java such as make a calculator, make stick men move and calculate Tax incomes on familys, but i really struggle on just the basic start of each program, like where the code goes.

Basically i just need 3 Tabs and to be able to either copy the Length conversion in under "Length Tab" or Design my own layout then put the Basic Conversion code in.


  Re: Conversion  blade250 at 14:24 on Thursday, April 27, 2006
 

Maybe tabs are beyond me a the moment, so i want to make this simpler. Basically i want to use the code which converts CM to inches and foot from Conversion3 but i want to design my own form, using an absolute layout with the heading Conversion and 3 Textfields CM, Inches and foot, how do i get the Conversion 3 code to work with that? i then, perhaps instead of using tabs want to put 3 or 4 other conversions and calculations on the same form, all with diff buttons, so a convert button for length etc, i think this would be easier for me, how do i set bout this, n how do iget the basic conversion 3 code to work with my own layout and textfields?

  Re: Conversion  crwood at 20:09 on Thursday, April 27, 2006
 

To get this to work you will need to change the method signature of the "getContent" method in Conversion3 from

private JPanel getContent()

to

protected JPanel getContent()

so it can be called from outside the class.

import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.Date;
import javax.swing.*;

public class ConversionTabs
{
private JTabbedPane getTabbedPane()
{
Conversion3 conversion3 = new Conversion3();
VolumeConversion volCon = new VolumeConversion();
TimeConversion timeConv = new TimeConversion();
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("length", conversion3.getContent());
tabbedPane.addTab("volume", volCon.getContent());
tabbedPane.addTab("time", timeConv.getContent());
return tabbedPane;
}

public static void main(String[] args)
{
ConversionTabs conversionTabs = new ConversionTabs();
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(conversionTabs.getTabbedPane());
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
}
}

class VolumeConversion
{
protected JLabel getContent()
{
JLabel label = new JLabel("<html><center>volume conversion<br>"+
"component goes here", JLabel.CENTER);
return label;
}
}

class TimeConversion
{
protected JLabel getContent()
{
DateFormat df = new SimpleDateFormat("dd MMMM yyyy");
Date now = new Date();
return new JLabel(df.format(now), JLabel.CENTER);
}
}


This 18 message thread spans 2 pages: [1]  2  > >  







CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  matrix addition
•  Re: Storing data from HTML to Excel or TXT
•  Re: function within loop problem
•  Re: Ô‡´ò¥¯¥é¥Ö¤à ¥Æ©`¥é©`¥á¥¤¥É£ò£±£±¥¢¥¤¥¢¥ó ¤Î£··¬ ¤Ç¤¹
•  Re: Replace
•  Re: タイトリスト AP2アイアン 712ã�®æƒ…å �
•  Re: SMS from Perl using HTTP request
•  Re: Charl Schwartzel
•  Re: Adhyayan - Annual Student Conference and Online Coding Festival


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2013