#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    74
    Rep Power
    2

    Add two TextField


    can somebody help me how to put two text fields? im experimenting and i cant figure it out how. (dont ask me why i used null layout, i have my reason)

    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.lang.Math.*;
    import java.util.LinkedList;
    import java.awt.Insets;
    import javax.swing.border.*;
    import java.awt.Font;
    public class calproto extends JFrame
    {
        //north
        private JTextField field1;
        private TextField field2;
        private Button add, one, two, equal;
        private Label label;
        private double input1;
        private double input2;
        private double result;
        private double mem1;
        private double mem2;
        private double mem3;
        String display = "";
        Boolean addbool = false;
        
        public calproto()
        {
            setLayout(null);
            setSize(300,300);
            setResizable(false);
            setVisible(true);        
            
            Font cback = new Font("Lucida Console", Font.PLAIN, 9);
            
            field2 = new TextField();
            add(field2);
            field2.setBounds(5, 2, 284, 25);
            field2.setForeground(Color.white);
            field2.setBackground(Color.black);
            
            field1 = new JTextField();
            add(field1);
            field1.setBounds(5, 2, 284, 25);
            field1.setHorizontalAlignment(JTextField.RIGHT);
            field1.setBorder(BorderFactory.createEmptyBorder());
            field1.setForeground(Color.white);
            field1.setBackground(Color.black);
            
            add = new Button("+");
            add(add);
            add.setBounds(1,220,140,50);
            
            one = new Button("1");
            add(one);
            one.setBounds(1,80,140,100);
            
            two = new Button("2");
            add(two);
            two.setBounds(141,80,140,100);
            
            equal = new Button("=");
            add(equal);
            equal.setBounds(141,220,140,50);
            
            one.addActionListener(new numone());
            two.addActionListener(new numtwo());
            add.addActionListener(new ADD());
            equal.addActionListener(new equal1());
        }
        
        public class numone implements ActionListener
        {
            public void actionPerformed(ActionEvent ae)
            {
                display = field1.getText();
                field1.setText(display + "1");
            }
        }
        public class numtwo implements ActionListener
        {
            public void actionPerformed(ActionEvent ae)
            {
                display = field1.getText();
                field1.setText(display + "2");
            }
        }
        public class ADD implements ActionListener
        {
            public void actionPerformed(ActionEvent ae)
            {
                display = field1.getText();
                field1.setText(display + "1");
            }
        }
        public class equal1 implements ActionListener
        {
            public void actionPerformed(ActionEvent ae)
            {
                display = field1.getText();
                field1.setText(display + "1");
            }
        }
                    
    
        
        public static void main()
        {
            calproto finals = new calproto();
            finals.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    }
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    What happens when you compile and execute the posted code?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    74
    Rep Power
    2
    Originally Posted by NormR
    What happens when you compile and execute the posted code?
    it supposedly shows two text fields, but it only show one.

    one upper and one lower.
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    The posted code will not execute. The main() method is coded incorrectly.

    Can you post here the x,y locations for each of the textfields you are trying to display?
    Where is field1?
    Where is field2?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    74
    Rep Power
    2
    Originally Posted by NormR
    The posted code will not execute. The main() method is coded incorrectly.

    Can you post here the x,y locations for each of the textfields you are trying to display?
    Where is field1?
    Where is field2?
    it works now thanks, i was doing a wrong setting for the location. i figured it out when you answered my other post. THANK YOU A LOOOOT!

    by the way, what is wrong with the main method? no String[] args? i leave it that way at the moment
    Last edited by Kuiva; October 12th, 2013 at 12:26 PM.

IMN logo majestic logo threadwatch logo seochat tools logo