Solved

the position of the textfield

Posted on 2004-09-12
4
361 Views
Last Modified: 2012-08-13
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
Comment
Question by:meow00
  • 2
4 Comments
 
LVL 16

Expert Comment

by:suprapto45
ID: 12041094
Hi again,

It is quite easy. Let me write you a code
0
 
LVL 16

Accepted Solution

by:
suprapto45 earned 250 total points
ID: 12041124
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
 
LVL 92

Assisted Solution

by:objects
objects earned 50 total points
ID: 12041191
Pane1.setLayout(new BorderLayout()) ;
Pane1.add(myLabel, BorderLayout.NORTH);
Pane1.add(new JTextArea(1,5), BorderLayout.CENTER);
0
 
LVL 6

Assisted Solution

by:expertmb
expertmb earned 50 total points
ID: 12041198
set the layout to BoxLayout.
Pane1.setLayout(new BoxLayout(Pane1,BoxLayout.Y_AXIS)) ;
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Systems talking to each other 5 126
Non-recursive backtracking, using a stack 1 103
Cipher Configuration on Apache HTTPD 4 53
windows explorer path to command prompt 5 33
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now