Alternative Nation
Go Back   Alternative Nation > Lounge > Computers & Technology > Web Dev & Programming

Notices

Reply
 
Thread Tools Rate Thread
Old 8th April 2008, 9:40pm   #1
moot moot
 
Sazzarella's Avatar
 
Join Date: Apr 2006
Location: Edinburgh
Posts: 2,904
Images: 25
Sazzarella = 2nd BestSazzarella = 2nd BestSazzarella = 2nd Best
Send a message via MSN to Sazzarella
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.
Sazzarella is offline   Reply With Quote
Old 9th April 2008, 9:47am   #2
Adie
 
mini_dez's Avatar
 
Join Date: Jul 2002
Location: Glasgow
Posts: 1,656
Images: 15
mini_dez sneaks like ninja in the nightmini_dez sneaks like ninja in the night
Send a message via MSN to mini_dez
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.
__________________
Myspace.com/Mini_Dez
mini_dez is offline   Reply With Quote
Old 9th April 2008, 10:07am   #3
EVIL/NICE.
 
Draven's Avatar
 
Join Date: Feb 2006
Location: Gloucestershire
Posts: 3,419
Draven is Deadly!Draven is Deadly!Draven is Deadly!
Send a message via MSN to Draven
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.
Draven is offline   Reply With Quote
Old 9th April 2008, 10:21am   #4
Registered User
 
m0nkeymafia's Avatar
 
Join Date: Jun 2001
Location: Aldgate East
Posts: 4,037
Images: 4
m0nkeymafia is Deadly!m0nkeymafia is Deadly!m0nkeymafia is Deadly!
Send a message via MSN to m0nkeymafia
Re: Java, text box functions

Quote:
Originally Posted by D.G.E. View Post
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.
m0nkeymafia is online now   Reply With Quote
Old 9th April 2008, 10:27am   #5
EVIL/NICE.
 
Draven's Avatar
 
Join Date: Feb 2006
Location: Gloucestershire
Posts: 3,419
Draven is Deadly!Draven is Deadly!Draven is Deadly!
Send a message via MSN to Draven
Re: Java, text box functions

Quote:
Originally Posted by m0nkeymafia View Post
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.
Draven is offline   Reply With Quote
Old 9th April 2008, 11:10am   #6
moot moot
 
Sazzarella's Avatar
 
Join Date: Apr 2006
Location: Edinburgh
Posts: 2,904
Images: 25
Sazzarella = 2nd BestSazzarella = 2nd BestSazzarella = 2nd Best
Send a message via MSN to Sazzarella
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.
Sazzarella is offline   Reply With Quote
Reply

Bookmarks
Digg del.icio.us StumbleUpon Google

Thread Tools
Rate This Thread
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Pangea 



 Classic Grand Advert





Powered by vBadvanced CMPS v3.0.0
Advertisement
   



All times are GMT +0. The time now is 3:09pm.

Forums Directory
Copyright 2000-2008, Alternative Nation

SEO by vBSEO 3.1.0 ©2007, Crawlability, Inc.
Page generated in 0.47797 seconds with 15 queries