| Notices | Welcome to the Altnation forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |  |
8th April 2008, 9:40pm
|
#1 | | moot moot
Join Date: Apr 2006 Location: Edinburgh
Posts: 2,904
| Java, text box functions Right I am in no way shape or form any good at Java yet and im el stucko and the sun website isnt proveing fruitful
Ive got some crappy wee code, generator to change Fahrenheit to Celcius and atm im using an option pane to get input from the user but I should be using a textbox and getText() but I have no idea how to use it :/ sounds simple but its just no worky worky, if someone can point me in the right direction of where im ment to put it then would be muchos appriciated. HALP Code: import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Temperature {
public static void main(String[] args) {
Temp buttonFrame = new Temp();
buttonFrame.setSize(350,120);
buttonFrame.setLocation(250,100);
buttonFrame.setVisible(true);
}
}
class Temp extends JFrame{
int celsius = 0;
int fahrenheit = 0;
public Temp ()
{
celsius = Integer.parseInt(JOptionPane.showInputDialog(null,"Please enter temperature in Celsius"));
final JTextField convertText = new JTextField ("Celsius: " + celsius);
JButton convertButton = new JButton ("Convert");
JButton quitButton = new JButton("Quit");
JPanel buttons = new JPanel();
buttons.setLayout (new FlowLayout());
buttons.add (convertButton);
buttons.add (quitButton);
setTitle("Temperature Frame");
setLayout(new BorderLayout());
add(buttons, BorderLayout.SOUTH);
add(convertText, BorderLayout.NORTH);
convertButton.addActionListener (new ActionListener()
{
public void actionPerformed( ActionEvent e)
{
fahrenheit=9/5*celsius+32;
convertText.setText("Fahrenheit: " + fahrenheit);
}
});
quitButton.addActionListener (new ActionListener()
{
public void actionPerformed( ActionEvent e)
{
System.exit(1);
}
});
}
}
__________________
See I'm no king I wear no crown, But desperate times seem over now, But still I weaken somehow It tears me apart. |
| |
9th April 2008, 9:47am
|
#2 | | Adie
Join Date: Jul 2002 Location: Glasgow
Posts: 1,656
| Re: Java, text box functions The general idea is you need to add a text box (JTextField) to your window. This allows the user to type whatever they like into it (so remember to validate the input). When the convert button is pressed you can use the getText() method of the JTextField to get the contents of it and convert that to Fahrenheit. |
| |
9th April 2008, 10:07am
|
#3 | | EVIL/NICE.
Join Date: Feb 2006 Location: Gloucestershire
Posts: 3,419
| Re: Java, text box functions Yeah I do know how to do that, and I can tell you simply the answer if you wish, but through my experience I've always found it easier to follow a wee tutorial to work out the answer myself, as then I remember. So this is what your looking for: http://java.sun.com/docs/books/tutor...ents/text.html
If you still can't get it I will write it out for you.
BTW have you considered looking at the Groovy scripting language? It's WAY easier to write in than Java, but it is still Java so all your Java programs work.
__________________
Every time I'm right a little part of you dies. |
| |
9th April 2008, 10:21am
|
#4 | | Registered User
Join Date: Jun 2001 Location: Aldgate East
Posts: 4,037
| Re: Java, text box functions Quote:
Originally Posted by D.G.E. BTW have you considered looking at the Groovy scripting language? It's WAY easier to write in than Java, but it is still Java so all your Java programs work. | Its not Java. It compiles to Java byte code and can be run within the JVM but it is a seperate language. |
| |
9th April 2008, 10:27am
|
#5 | | EVIL/NICE.
Join Date: Feb 2006 Location: Gloucestershire
Posts: 3,419
| Re: Java, text box functions Quote:
Originally Posted by m0nkeymafia Its not Java. It compiles to Java byte code and can be run within the JVM but it is a seperate language. | Yeah I know, but the JVM can run Groovy, and Groovy can run Java so there as closed to the same thing as you can get. Just find it easier putting that than the whole story. Which IBM's Jerry Cuomo puts it as: Groovy was built from the ground up with Java developers in mind. It's retains much of the same syntax and semantics failure to Java developers while also offering dynamic language features like dynamic typing, mixings, and closures… These are the types of features that make dynamic languages much more productive than conventional languages. Finally, all the Java APIs that you use today can be used in exactly the same way in Groovy. Groovy compiles into byte code, so any Java API you can import and access in a Java program you can import and access in Groovy.
__________________
Every time I'm right a little part of you dies. |
| |
9th April 2008, 11:10am
|
#6 | | moot moot
Join Date: Apr 2006 Location: Edinburgh
Posts: 2,904
| Re: Java, text box functions After a wee bit of dodling I added the line "celsius = new Double (celsiusField.getText().doubleValue();" and hey presto, worky worky. Cheers tho 
__________________
See I'm no king I wear no crown, But desperate times seem over now, But still I weaken somehow It tears me apart. |
| |  | | Thread Tools | | | | Rate This Thread | | |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | |