Solved

the position of the textfield

Posted on 2004-09-12
4
364 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
web application structure 18 135
jsp login check 12 67
ejb on wildfly 5 64
Eclipse with various Java releases 7 90
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

759 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