Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 JTextFields, getTexts as an integer?
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
lord_pwnerer




PostPosted: Sun Oct 15, 2006 4:59 pm   Post subject: JTextFields, getTexts as an integer?

I want to get the text (numbers) from two text fields using a button which will inturn write the sum of both numbers into a third text field.

I'm having trouble with it. Assuming that all the components have been declared I think that there are two problems. First my variables are Strings, but second, I get an error saying that it is an invalid reference to inner class to try to find a non-local variable. Could someone please help me find a way around these two problems?

code:

JButton dB = new JButton ("Calculate Density");

dB.addActionListener(
    new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String dM = dtxt1.getText();
            String dV = dtxt2.getText();
            String dD = dtxt3.setText(dM + dV);
        }
    }
);
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Sun Oct 15, 2006 5:39 pm   Post subject: (No subject)

Can you please at least link to the full source code?
lord_pwnerer




PostPosted: Tue Oct 17, 2006 4:17 pm   Post subject: (No subject)

yeah, sorry

code:

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

public class densityProgram
{
    public static void densityWindow ()
    {

        try
        {

            UIManager.setLookAndFeel (UIManager.getSystemLookAndFeelClassName ());
        }
        catch (Exception e)
        {

        }

        //Creates the frame, and sets close operation
        JFrame w = new JFrame ("Density Calculator");
        w.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

        //Creates a label which will mask the new frame
        JLabel l = new JLabel ();
        l.setPreferredSize (new Dimension (300, 300));
        l.setOpaque (true);
        l.setBackground (Color.black);

        //Creates Buttons
        JButton dB = new JButton ("Calculate Density");
        JButton mB = new JButton ("Calculate Mass");
        JButton vB = new JButton ("Calculate Volume");
        JButton signin = new JButton ("Sign In");


        //Creates numerous labels for the text fields
        JLabel s = new JLabel (" ");
        JLabel s1 = new JLabel (" ");
        JLabel s2 = new JLabel (" ");
        JLabel s3 = new JLabel (" ");
        JLabel s4 = new JLabel (" ");
        JLabel s5 = new JLabel (" ");
        JLabel s6 = new JLabel (" ");
        JLabel s7 = new JLabel (" ");
        JLabel s8 = new JLabel (" ");
        JLabel s9 = new JLabel (" ");
        JLabel s10 = new JLabel (" ");
        JLabel s11 = new JLabel (" ");
        JLabel s12 = new JLabel (" ");

        JLabel d1 = new JLabel ("Insert Mass");
        JLabel d2 = new JLabel ("Insert Volume");
        JLabel d3 = new JLabel ("Density : ");

        JLabel v1 = new JLabel ("Insert Density");
        JLabel v2 = new JLabel ("Insert Mass");
        JLabel v3 = new JLabel ("Volume :");

        JLabel m1 = new JLabel ("Insert Volume");
        JLabel m2 = new JLabel ("Insert Density");
        JLabel m3 = new JLabel ("Mass :");

        JLabel p1 = new JLabel ("Insert Password");
        JLabel u1 = new JLabel ("Insert Name");

        //Creates numerous text fields
        JTextField dtxt1 = new JTextField ("0", 10);
        JTextField dtxt2 = new JTextField ("0", 10);
        JTextField dtxt3 = new JTextField ("0", 10);

        JTextField mtxt1 = new JTextField ("0", 10);
        JTextField mtxt2 = new JTextField ("0", 10);
        JTextField mtxt3 = new JTextField ("0", 10);

        JTextField vtxt1 = new JTextField ("0", 10);
        JTextField vtxt2 = new JTextField ("0", 10);
        JTextField vtxt3 = new JTextField ("0", 10);

        JTextField ptxt1 = new JTextField ("0", 10);
        JTextField utxt1 = new JTextField ("0", 10);

        //Creates a tabbed pane with panels
        JTabbedPane tab = new JTabbedPane ();
        JPanel d = new JPanel ();
        JPanel m = new JPanel ();
        JPanel v = new JPanel ();

        //The Density Panel
        d.setLayout (new BoxLayout (d, BoxLayout.Y_AXIS));
        d.add (d1);
        d.add (dtxt1);
        d.add (s);
        d.add (d2);
        d.add (dtxt2);
        d.add (s1);
        d.add (dB);
        d.add (s2);
        d.add (d3);
        d.add (dtxt3);

        //The Mass Panel
        m.add (m1);
        m.add (mtxt1);
        m.add (m2);
        m.add (mtxt2);

        //The Volume Panel
        v.add (v1);
        v.add (vtxt1);
        v.add (v2);
        v.add (vtxt2);

        //The tabbed pane
        tab.add (d, "Density");
        tab.add (m, "Mass");
        tab.add (v, "Volume");

        //Adds components to frame
        w.getContentPane ().add (l, BorderLayout.CENTER);

        w.getContentPane ().add (tab, BorderLayout.CENTER);


        //
        w.pack ();
        w.setVisible (true);
        w.setResizable (false);
        w.setSize (300, 300);
    }


    public static void main (String[] args)
    {
        javax.swing.SwingUtilities.invokeLater (new Runnable ()
        {
            public void run ()
            {
                densityWindow ();
            }
        }
        );
    }
}


As you can see, the formatting isn't the best so if someone could please explain how to make it better to me that would be a great help also.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: