TeamJava Forum
[ Read Responses | Post a New Response | Return to the Index ]
[ Previous | Previous in Thread | Next ]
Re: Chemical Forumulae
Posted by Morris on Wednesday, 20 August 2003, at 9:46 a.m., in response to Re: Chemical Forumulae, posted by Morris on Wednesday, 20 August 2003, at 9:45 a.m.
So now I'm going back to the tutorials step by step to help me understand how to do this program.
......
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ChemicalAnalyser extends JFrame {
private JTextField textField1;
private JLabel prompt;
String userChemical;
// set up GUI
public ChemicalAnalyser()
{
super( "Chemical Formulae Analyser" );
Container container = getContentPane();
container.setLayout( new FlowLayout() );
// construct textfield with default sizing
prompt = new JLabel("Enter Chemical Formulae");
container.add( prompt );
textField1 = new JTextField( 10 );
container.add( textField1 );
// register event handlers
TextFieldHandler handler = new TextFieldHandler();
textField1.addActionListener( handler );
setSize( 325, 100 );
setVisible( true );
} // end constructor TextFieldTest
public static void main( String args[] )
{
ChemicalAnalyser application = new ChemicalAnalyser();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
// private inner class for event handling
private class TextFieldHandler implements ActionListener {
// process textfield events
public void actionPerformed( ActionEvent event )
{
String output = "";
userChemical = textField1.getText();
// user pressed Enter in JTextField textField1
if ( event.getSource() == textField1 )
output = "The Breakdown of your Chemical Formulae is as follows: \n\n\n" + userChemical;
char charArray[] = new char[ 5 ];
output += "userChemical: " + userChemical;
// test length method
output += "\nLength of userChemical: " + userChemical.length();
// loop through characters in s1 and display reversed
output += "\nThe string reversed is: ";
for ( int count = userChemical.length() - 1; count >= 0; count-- )
output += userChemical.charAt( count ) + " ";
// copy characters from string into charArray
userChemical.getChars( 0, 5, charArray, 0 );
output += "\nThe character array is: ";
for ( int count = 0; count output += charArray[ count ];
//}
JOptionPane.showMessageDialog( null, output );
} // end method actionPerformed
} // end private inner class TextFieldHandler
} // end class TextFieldTest
If any-one has an idea that could help me with this program, I'd very much appreciate it....Thanks.
Responses
Post a New Response
The TeamJava Forum is maintained with WebBBS 2.24.