Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 370
  • Last Modified:

the position of the textfield

Hi Experts,

    I am trying to write a calculator ....... and I am wondering how to set the Textfield at a specific place ...... i.e. I want        line 01, the textfield right below the label. And the line 02, the results display in the textfield ........

    Please give me some suggestions ... many thanks !

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

public class Calculator extends JFrame implements ActionListener
{
   
    private JLabel myCounter ;
    private String myString ;
    private int n ;
    private JButton b1, b2, b3, b4, b5, b6, b7, b8, b9, b0 ;
    private JButton b_plus, b_minus, b_multiply, b_devide, b_equal, b_AC, b_dot ;
    private float f1, f2, f3 ;
    private int index_1, index_2 ;
   
     public Calculator(){
         
          setTitle("Good Morning ! Meow ~");
         JLabel myLabel = new JLabel("How are you today ?"); // calling the constructor:  JLabel(String text)
         myCounter = new JLabel("You have pressed the button !") ;
         myString = new String("") ;
         JPanel Pane1 = new JPanel() ;
         Pane1.setLayout(new FlowLayout()) ;
         Pane1.add(myLabel);
        Pane1.add(new JTextArea(1,5)) ;
   
         JPanel Pane2 = new JPanel() ;
         Pane2.setLayout(new GridLayout(4,5)) ;
   
         b1 = new JButton("1");  b1.addActionListener(this); Pane2.add(b1) ;
         b2 = new JButton("2");  b2.addActionListener(this); Pane2.add(b2) ;
         b3 = new JButton("3");  b3.addActionListener(this); Pane2.add(b3) ;
         b4 = new JButton("4");  b4.addActionListener(this); Pane2.add(b4) ;
         b5 = new JButton("5");  b5.addActionListener(this); Pane2.add(b5) ;
         b6 = new JButton("6");  b6.addActionListener(this); Pane2.add(b6) ;
         b7 = new JButton("7");  b7.addActionListener(this); Pane2.add(b7) ;
         b8 = new JButton("8");  b8.addActionListener(this); Pane2.add(b8) ;
         b9 = new JButton("9");  b9.addActionListener(this); Pane2.add(b9) ;
   
    b_plus = new JButton("+");      b_plus.addActionListener(this);      Pane2.add(b_plus) ;
    b_minus = new JButton("-");     b_minus.addActionListener(this);     Pane2.add(b_minus);
    b_multiply = new JButton("*");  b_multiply.addActionListener(this);  

    Pane2.add(b_multiply) ;
    b_devide = new JButton("/");    b_devide.addActionListener(this);    Pane2.add(b_devide) ;
    b_equal = new JButton("=");     b_equal.addActionListener(this);     Pane2.add(b_equal) ;
    b_dot = new JButton(".");       b_dot.addActionListener(this);       Pane2.add(b_dot) ;
    b_AC = new JButton("AC");       b_AC.addActionListener(this);       Pane2.add(b_AC) ;
 
    Container myPane = this.getContentPane();
    myPane.setLayout(new BorderLayout()) ;
    myPane.add(Pane1,BorderLayout.NORTH) ;
    myPane.add(Pane2, BorderLayout.CENTER) ;
    myPane.add(myCounter, BorderLayout.SOUTH) ;
   
     
     setSize(400,500) ;

    show();
  }
   
   
    public void actionPerformed(ActionEvent e) {
        n++ ;    
       if (e.getSource() == b1){ myString += "1" ;}
       if (e.getSource() == b2){ myString += "2" ;}
       if (e.getSource() == b3){ myString += "3" ;}
       if (e.getSource() == b4){ myString += "4" ;}
       if (e.getSource() == b5){ myString += "5" ;}
       if (e.getSource() == b6){ myString += "6" ;}
       if (e.getSource() == b7){ myString += "7" ;}
       if (e.getSource() == b8){ myString += "8" ;}
       if (e.getSource() == b9){ myString += "9" ;}
       if (e.getSource() == b0){ myString += "0" ;}
       if (e.getSource() == b_plus){
           f1 = Float.parseFloat(myString);
           myString += "+" ;
           index_1 = myString.length(); }
       
       if (e.getSource() == b_minus){ myString += "-" ;}
       if (e.getSource() == b_multiply){ myString += "*" ;}
       if (e.getSource() == b_devide){ myString += "/" ;}
       if (e.getSource() == b_equal){
           myString += "=" ;
           index_2 = myString.length() ;
           f2 = Float.parseFloat(myString.substring(index_1-1,index_2-1)) ;
           f3 = f1+f2 ;
           myString = Float.toString(f3) ;
       }  
       
       if (e.getSource() == b_AC){ myString = "" ;}
       if (e.getSource() == b_dot){ myString += "." ;}  
       
        myCounter.setText(" You have pressed the button "+ n + " times ! " + myString ) ;  // line 02
     
     }
    public static void main(String[] arg) {
        Calculator MyCal = new Calculator() ;
        MyCal.addWindowListener(
        new WindowAdapter()
        {
           public void windowClosing (WindowEvent e)
           {
               System.exit(0);
           }
          }
     );
 
     // MeowFrame.setSize(60,80) ;
         //MeowFrame.pack() ;
         //MeowFrame.show() ;
   }
}

0
meow00
Asked:
meow00
  • 2
3 Solutions
 
suprapto45Commented:
Hi again,

It is quite easy. Let me write you a code
0
 
suprapto45Commented:
Well,
1. To make the textfield below the label, you have to setLayout of the Panel into GridLayout thus the Panel will be divided into 2 rows and 1 column. I also changed your new JTextArea() into a new JTextField().

         JPanel Pane1 = new JPanel() ;
         Pane1.setLayout(new GridLayout(2,1)) ;
         Pane1.add(myLabel);

        Pane1.add(text1) ;   // text1 is the JTextField

2. And the line 02, the results display in the textfield. To accomplish it, use text1.setText("blah blah");


I send you the new codes. Please learn it and I hope that my codes will make you understan Java better.

/* Start Here */

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

public class MeowTest extends JFrame implements ActionListener
{
   
    private JLabel myCounter ;
    private String myString ;
    private int n ;
    private JButton b1, b2, b3, b4, b5, b6, b7, b8, b9, b0 ;
    private JButton b_plus, b_minus, b_multiply, b_devide, b_equal, b_AC, b_dot ;
    private float f1, f2, f3 ;
    private int index_1, index_2 ;

     /* Look Here */          
     JTextField text1 = new JTextField(20);
   
     public MeowTest(){
         
          setTitle("Good Morning ! Meow ~");
             
         JLabel myLabel = new JLabel("How are you today ?"); // calling the constructor:  JLabel(String text)
         myCounter = new JLabel("You have pressed the button !") ;
         myString = new String("") ;

         /* Look Here */
         JPanel Pane1 = new JPanel() ;
         Pane1.setLayout(new GridLayout(2,1)) ;
         Pane1.add(myLabel);

        Pane1.add(text1) ;
            
   
         JPanel Pane2 = new JPanel() ;
         Pane2.setLayout(new GridLayout(4,5)) ;
   
         b1 = new JButton("1");  b1.addActionListener(this); Pane2.add(b1) ;
         b2 = new JButton("2");  b2.addActionListener(this); Pane2.add(b2) ;
         b3 = new JButton("3");  b3.addActionListener(this); Pane2.add(b3) ;
         b4 = new JButton("4");  b4.addActionListener(this); Pane2.add(b4) ;
         b5 = new JButton("5");  b5.addActionListener(this); Pane2.add(b5) ;
         b6 = new JButton("6");  b6.addActionListener(this); Pane2.add(b6) ;
         b7 = new JButton("7");  b7.addActionListener(this); Pane2.add(b7) ;
         b8 = new JButton("8");  b8.addActionListener(this); Pane2.add(b8) ;
         b9 = new JButton("9");  b9.addActionListener(this); Pane2.add(b9) ;
   
    b_plus = new JButton("+");      b_plus.addActionListener(this);      Pane2.add(b_plus) ;
    b_minus = new JButton("-");     b_minus.addActionListener(this);     Pane2.add(b_minus);
    b_multiply = new JButton("*");  b_multiply.addActionListener(this);  

    Pane2.add(b_multiply) ;
    b_devide = new JButton("/");    b_devide.addActionListener(this);    Pane2.add(b_devide) ;
    b_equal = new JButton("=");     b_equal.addActionListener(this);     Pane2.add(b_equal) ;
    b_dot = new JButton(".");       b_dot.addActionListener(this);       Pane2.add(b_dot) ;
    b_AC = new JButton("AC");       b_AC.addActionListener(this);       Pane2.add(b_AC) ;
 
    Container myPane = this.getContentPane();
    myPane.setLayout(new BorderLayout()) ;
    myPane.add(Pane1,BorderLayout.NORTH) ;
    myPane.add(Pane2, BorderLayout.CENTER) ;
    myPane.add(myCounter, BorderLayout.SOUTH) ;
   
     
     setSize(400,500) ;

    show();
  }
   
   
    public void actionPerformed(ActionEvent e) {
        n++ ;    
       if (e.getSource() == b1){ myString += "1" ;}
       if (e.getSource() == b2){ myString += "2" ;}
       if (e.getSource() == b3){ myString += "3" ;}
       if (e.getSource() == b4){ myString += "4" ;}
       if (e.getSource() == b5){ myString += "5" ;}
       if (e.getSource() == b6){ myString += "6" ;}
       if (e.getSource() == b7){ myString += "7" ;}
       if (e.getSource() == b8){ myString += "8" ;}
       if (e.getSource() == b9){ myString += "9" ;}
       if (e.getSource() == b0){ myString += "0" ;}
       if (e.getSource() == b_plus){
           f1 = Float.parseFloat(myString);
           myString += "+" ;
           index_1 = myString.length(); }
       
       if (e.getSource() == b_minus){ myString += "-" ;}
       if (e.getSource() == b_multiply){ myString += "*" ;}
       if (e.getSource() == b_devide){ myString += "/" ;}
       if (e.getSource() == b_equal){
           myString += "=" ;
           index_2 = myString.length() ;
           f2 = Float.parseFloat(myString.substring(index_1-1,index_2-1)) ;
           f3 = f1+f2 ;
           myString = Float.toString(f3) ;
       }  
       
       if (e.getSource() == b_AC){ myString = "" ;}
       if (e.getSource() == b_dot){ myString += "." ;}  
       
        /* Look Here */
        //myCounter.setText(" You have pressed the button "+ n + " times ! " + myString ) ;  // line 02
        text1.setText(" You have pressed the button "+ n + " times ! " + myString ) ;
     }
    public static void main(String[] arg) {
        MeowTest MyCal = new MeowTest() ;
        MyCal.addWindowListener(
        new WindowAdapter()
        {
           public void windowClosing (WindowEvent e)
           {
               System.exit(0);
           }
          }
     );
 
     // MeowFrame.setSize(60,80) ;
         //MeowFrame.pack() ;
         //MeowFrame.show() ;
   }
}

/* End Here */
0
 
objectsCommented:
Pane1.setLayout(new BorderLayout()) ;
Pane1.add(myLabel, BorderLayout.NORTH);
Pane1.add(new JTextArea(1,5), BorderLayout.CENTER);
0
 
expertmbCommented:
set the layout to BoxLayout.
Pane1.setLayout(new BoxLayout(Pane1,BoxLayout.Y_AXIS)) ;
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now