How could I set width of JText in panel1 equal with width in panel2

127 views Asked by At

How could I set width of JText in panel1 equal with width in panel2?

/* Calculator */
import java.awt.*;
import java.awt.event.*;
import java.lang.Math;
import javax.swing.*;
class Calculator extends Frame implements ActionListener  {
    Font font1 = new Font( "Times New Roman",  Font.PLAIN,  20 );
    JLabel  Label1,  Label2,  Label3;
    JTextField  txt1,  txt2,  txt3;
    // 4 JButton
    JButton  Add = new JButton( "  +  " );
    JButton  Sub = new JButton( "  -  " );
    JButton  Mul = new JButton( "  x  " );
    JButton  Div = new JButton( "  /  " );
    JButton  Power = new JButton( "  ^  " );
    JButton  Sin = new JButton( "  Sin  " );
    JButton  Cos = new JButton( "  Cos  " );
    JButton  Tan = new JButton( "  Tan  " );
    JButton  Arcsin = new JButton( "  Arcsin  " );
    JButton  Arccos = new JButton( "  Arccos  " );
    JButton  Arctan = new JButton( "  Arctan  " );
    JButton  Factorial = new JButton( "  Factorial  " );
    JButton  Combinatory = new JButton( "  Combinatory  " );

    // 2 Panels will contain components
    Panel  p1 = new Panel( new GridLayout( 3, 2 ) );
    Panel  p2 = new Panel( new GridLayout( 4, 4 ) );

/* Calculator( )  */
    Calculator( )  {
        super ( "Calculator" );
        Label1 = new JLabel ( "First number: ", Label.LEFT );
        Label2 = new JLabel ( "Second number: ", Label.LEFT );
        Label3 = new JLabel ( "Result: ", Label.LEFT );
        txt1 = new JTextField( );  txt2 = new JTextField( );   txt3 = new JTextField( ); 
        txt1.setFont( font1 );  txt2.setFont( font1 );   txt3.setFont( font1 );
        txt1.setPreferredSize( new Dimension ( 100, 20 ) );
        txt2.setPreferredSize( new Dimension ( 100, 20 ) );
        txt3.setPreferredSize( new Dimension ( 100, 20 ) );

        Label1.setFont( font1 );  Label2.setFont( font1 );  
        Label3.setFont( font1 );  
        // Adding lables and textbox to panel p1
        p1.setMaximumSize( new Dimension( 3*100,  2*200 ) );
        p2.setMaximumSize( new Dimension( 4*100,  4*100 ) );
        p1.add( Label1 );  p1.add( txt1 );
        p1.add( Label2 );  p1.add( txt2 );
        p1.add( Label3 );  p1.add( txt3 );
        // Adding 4 JButtons to panel p2
        p2.add( Add );  p2.add( Sub );  p2.add( Mul );  p2.add( Div );
        p2.add( Power );  p2.add( Factorial );  p2.add( Combinatory ); 
        p2.add( Sin );  p2.add( Cos );  p2.add( Tan );  
        p2.add( Arcsin );  p2.add( Arccos );  p2.add( Arctan );  

        // set layout of this frame is FlowLayout
        this.setLayout( new FlowLayout( ) );
        // Adding 2 panels to this frame
        this.add( p1 );  this.add( p2 );
        Add.addActionListener( this );  Sub.addActionListener( this );
        Mul.addActionListener( this );  Div.addActionListener( this );
        Power.addActionListener( this );  Factorial.addActionListener( this );
        Sin.addActionListener( this );  Cos.addActionListener( this );
        Tan.addActionListener( this );  Arcsin.addActionListener( this );
        Arccos.addActionListener( this );  Arctan.addActionListener( this );
        Combinatory.addActionListener( this );

      // Managing window closing event
        addWindowListener ( new WindowAdapter( )  {
            public void windowClosing( WindowEvent event )  { System.exit( 0 ); }
        } );  
    /* Add close JButton or we can use:         
        setDefaultCloseOperation ( JFrame.DISPOSE_ON_CLOSE ); in constructor Calculator( )  */
    }
    public long Factorial ( long n )  {
        int i, n1;  n1 = 1;
        for ( i = 1; i <= n; i++ )
            n1 = n1*i;
        return n1; 
    }


/* public void actionPerformed ( ActionEvent e ) */
    public void actionPerformed ( ActionEvent e ) {
        /* Method will be automatic called when ActionListener receive action
            from the listened objects */
        double k3;  double PI = 3.141592654;
        //  Convert inputted content into number data
        double k1 = Double.parseDouble ( txt1.getText( ) );  
        double k2 = Double.parseDouble ( txt2.getText( ) );  
        String s1, s2, s3, s4;  s1 = txt1.getText( );  s2 = txt2.getText( );  
        if ( e.getSource( ) == Add )  {  
            // If event source is JButton Add
            k3 = k1 + k2;  s3 = Double.toString( k3 );
            s4 = s1 + " + " + s2 + " = " + s3;
            txt3.setText( s4  ); 
            /* txt3, s4 = Result */
        } 
        if ( e.getSource( ) == Sub )  {  
            k3 = k1 - k2;  s3 = Double.toString( k3 );
            s4 = s1 + " - " + s2 + " = " + s3;
            txt3.setText( s4  ); 
        } 
        if ( e.getSource(   ) == Mul )  {  
            k3 = k1 * k2;  s3 = Double.toString( k3 );
            s4 = s1 + " * " + s2 + " = " + s3;
            txt3.setText( s4  ); 
        } 
        if ( e.getSource( ) == Div )  {  
            k3 = k1 / k2;  s3 = Double.toString( k3 );
            s4 = s1 + " / " + s2 + " = " + s3;
            txt3.setText( s4  ); 
        } 
        if ( e.getSource( ) == Power )  {  
            k3 = Math.exp( Math.log( k1 ) * k2 );  s3 = Double.toString( k3 );
            s4 = s1 + " ^ " + s2 + " = " + s3;
            txt3.setText( s4  ); 
        } 
        if ( e.getSource( ) == Factorial )  {
            long  n, n1;  n = ( long ) k1;  
            n1 = Factorial( n );
            s3 = Long.toString( n1 );  s1 = Long.toString( n );
            s4 = s1 + "! = " + s3;
            txt3.setText( s4  ); 
        } 
        if ( e.getSource( ) == Sin )  {
            k1 = k1*PI/180;  
            k3 = Math.sin( k1 );  s3 = Double.toString( k3 );
            s4 = "Sin( " + s1 + " ) = " + s3;
            txt3.setText( s4  );  
        } 
        if ( e.getSource( ) == Cos )  { 
            k1 = k1*PI/180;   
            k3 = Math.cos( k1 );  s3 = Double.toString( k3 );
            s4 = "Cos( " + s1 + " ) = " + s3;
            txt3.setText( s4  );  
        } 
        if ( e.getSource( ) == Tan )  { 
            k1 = k1*PI/180;   
            k3 = Math.tan( k1 );  s3 = Double.toString( k3 );
            s4 = "Tan( " + s1 + " ) = " + s3;
            txt3.setText( s4  );  
        } 
        if ( e.getSource( ) == Arcsin )  { 
            k3 = Math.asin( k1 );  k3 = k3*180/PI; 
            s3 = Double.toString( k3 );
            s4 = "Arcsin( " + s1 + " ) = " + s3;
            txt3.setText( s4  );  
        } 
        if ( e.getSource( ) == Arccos )  {  
            k3 = Math.acos( k1 );  k3 = k3*180/PI; 
            s3 = Double.toString( k3 );
            s4 = "Arccos( " + s1 + " ) = " + s3;
            txt3.setText( s4  );  
        } 
        if ( e.getSource( ) == Arctan )  {   
            k3 = Math.atan( k1 );  k3 = k3*180/PI; 
            s3 = Double.toString( k3 );
            s4 = "Arctan( " + s1 + " ) = " + s3;
            txt3.setText( s4  );  
        } 
        if ( e.getSource( ) == Combinatory )  {   
            long n1, n2, n;  n1 = ( long ) k1;  n2 = ( long ) k2; 
            n = Factorial( n2 ) / ( Factorial( n1 ) * Factorial( n2 - n1 )  );
            s1 = Long.toString( n1 );  s2 = Long.toString( n2 );  
            s3 = Long.toString( n ); 
            s4 = "C( " + s1 + ", " + s2 + " ) = " + s3;
            txt3.setText( s4  );  
        } 
    }
    public static void main( String args[] )  {
        Calculator f = new Calculator( );
        f.setSize( 500, 500 );
        f.setVisible( true );
    }
}

Could you please tell me How could I set width of JText in panel1 equal with width in panel2? I have set

p1.setMaximumSize( new Dimension( 3*100,  2*200 ) );
p2.setMaximumSize( new Dimension( 4*100,  4*100 ) );

but it doesnt work.

/* Random text, It looks like your code is mostly code and I think I give enough detail in English language. I need this line to post your code and edit later :D */

1

There are 1 answers

2
Gilbert Le Blanc On

Basically, you need to use the GridBagLayout on the JFrame and the top JPanel, to get the spacing you want.

You also need to start a Swing application on the Event Dispatch thread (EDT).

Here's the GUI:

Calculator

Here's the modified code:

/* Calculator */
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

class Calculator extends JFrame implements ActionListener {
    private static final long   serialVersionUID    = 4472395095500512410L;
    Font                        font1               = 
            new Font("Times New Roman", Font.PLAIN, 20);
    JLabel                      Label1, Label2, Label3;
    JTextField                  txt1, txt2, txt3;
    // 4 JButton
    JButton                     Add                 = new JButton("  +  ");
    JButton                     Sub                 = new JButton("  -  ");
    JButton                     Mul                 = new JButton("  x  ");
    JButton                     Div                 = new JButton("  /  ");
    JButton                     Power               = new JButton("  ^  ");
    JButton                     Sin                 = new JButton("  Sin  ");
    JButton                     Cos                 = new JButton("  Cos  ");
    JButton                     Tan                 = new JButton("  Tan  ");
    JButton                     Arcsin              = new JButton("  Arcsin  ");
    JButton                     Arccos              = new JButton("  Arccos  ");
    JButton                     Arctan              = new JButton("  Arctan  ");
    JButton                     Factorial           = new JButton(
                                                            "  Factorial  ");
    JButton                     Combinatory         = new JButton(
                                                            "  Combinatory  ");

    // 2 Panels will contain components
    Panel                       p1                  = new Panel();
    Panel                       p2                  = new Panel(new GridLayout(
                                                            4, 4));

    /* Calculator( ) */
    Calculator() {
        super("Calculator");
        Label1 = new JLabel("First number: ", Label.LEFT);
        Label2 = new JLabel("Second number: ", Label.LEFT);
        Label3 = new JLabel("Result: ", Label.LEFT);
        txt1 = new JTextField(25);
        txt2 = new JTextField(25);
        txt3 = new JTextField(25);
        txt1.setFont(font1);
        txt2.setFont(font1);
        txt3.setFont(font1);

        Label1.setFont(font1);
        Label2.setFont(font1);
        Label3.setFont(font1);

        p1.setLayout(new GridBagLayout());
        GridBagConstraints pc = new GridBagConstraints();

        // Adding lables and textbox to panel p1
        pc.gridx = 0;
        pc.gridy = 0;
        pc.anchor = GridBagConstraints.LINE_START;
        pc.fill = GridBagConstraints.NONE;

        p1.add(Label1, pc);

        pc.gridx++;
        pc.fill = GridBagConstraints.HORIZONTAL;

        p1.add(txt1, pc);

        pc.gridx = 0;
        pc.gridy++;
        pc.fill = GridBagConstraints.NONE;

        p1.add(Label2, pc);

        pc.gridx++;
        pc.fill = GridBagConstraints.HORIZONTAL;

        p1.add(txt2, pc);

        pc.gridx = 0;
        pc.gridy++;
        pc.fill = GridBagConstraints.NONE;

        p1.add(Label3, pc);

        pc.gridx++;
        pc.fill = GridBagConstraints.HORIZONTAL;

        p1.add(txt3, pc);
        // Adding 4 JButtons to panel p2
        p2.add(Add);
        p2.add(Sub);
        p2.add(Mul);
        p2.add(Div);
        p2.add(Power);
        p2.add(Factorial);
        p2.add(Combinatory);
        p2.add(Sin);
        p2.add(Cos);
        p2.add(Tan);
        p2.add(Arcsin);
        p2.add(Arccos);
        p2.add(Arctan);

        // set layout of this frame is FlowLayout
        this.setLayout(new GridBagLayout());
        // Adding 2 panels to this frame
        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 0;
        c.anchor = GridBagConstraints.LINE_START;
        c.fill = GridBagConstraints.HORIZONTAL;
        this.add(p1, c);
        c.gridy++;
        this.add(p2, c);

        Add.addActionListener(this);
        Sub.addActionListener(this);
        Mul.addActionListener(this);
        Div.addActionListener(this);
        Power.addActionListener(this);
        Factorial.addActionListener(this);
        Sin.addActionListener(this);
        Cos.addActionListener(this);
        Tan.addActionListener(this);
        Arcsin.addActionListener(this);
        Arccos.addActionListener(this);
        Arctan.addActionListener(this);
        Combinatory.addActionListener(this);

        // Managing window closing event
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent event) {
                System.exit(0);
            }
        });
        /*
         * Add close JButton or we can use: setDefaultCloseOperation (
         * JFrame.DISPOSE_ON_CLOSE ); in constructor Calculator( )
         */
    }

    public long Factorial(long n) {
        int i, n1;
        n1 = 1;
        for (i = 1; i <= n; i++)
            n1 = n1 * i;
        return n1;
    }

    /* public void actionPerformed ( ActionEvent e ) */
    public void actionPerformed(ActionEvent e) {
        /*
         * Method will be automatic called when ActionListener receive action
         * from the listened objects
         */
        double k3;
        double PI = 3.141592654;
        // Convert inputted content into number data
        double k1 = Double.parseDouble(txt1.getText());
        double k2 = Double.parseDouble(txt2.getText());
        String s1, s2, s3, s4;
        s1 = txt1.getText();
        s2 = txt2.getText();
        if (e.getSource() == Add) {
            // If event source is JButton Add
            k3 = k1 + k2;
            s3 = Double.toString(k3);
            s4 = s1 + " + " + s2 + " = " + s3;
            txt3.setText(s4);
            /* txt3, s4 = Result */
        }
        if (e.getSource() == Sub) {
            k3 = k1 - k2;
            s3 = Double.toString(k3);
            s4 = s1 + " - " + s2 + " = " + s3;
            txt3.setText(s4);
        }
        if (e.getSource() == Mul) {
            k3 = k1 * k2;
            s3 = Double.toString(k3);
            s4 = s1 + " * " + s2 + " = " + s3;
            txt3.setText(s4);
        }
        if (e.getSource() == Div) {
            k3 = k1 / k2;
            s3 = Double.toString(k3);
            s4 = s1 + " / " + s2 + " = " + s3;
            txt3.setText(s4);
        }
        if (e.getSource() == Power) {
            k3 = Math.exp(Math.log(k1) * k2);
            s3 = Double.toString(k3);
            s4 = s1 + " ^ " + s2 + " = " + s3;
            txt3.setText(s4);
        }
        if (e.getSource() == Factorial) {
            long n, n1;
            n = (long) k1;
            n1 = Factorial(n);
            s3 = Long.toString(n1);
            s1 = Long.toString(n);
            s4 = s1 + "! = " + s3;
            txt3.setText(s4);
        }
        if (e.getSource() == Sin) {
            k1 = k1 * PI / 180;
            k3 = Math.sin(k1);
            s3 = Double.toString(k3);
            s4 = "Sin( " + s1 + " ) = " + s3;
            txt3.setText(s4);
        }
        if (e.getSource() == Cos) {
            k1 = k1 * PI / 180;
            k3 = Math.cos(k1);
            s3 = Double.toString(k3);
            s4 = "Cos( " + s1 + " ) = " + s3;
            txt3.setText(s4);
        }
        if (e.getSource() == Tan) {
            k1 = k1 * PI / 180;
            k3 = Math.tan(k1);
            s3 = Double.toString(k3);
            s4 = "Tan( " + s1 + " ) = " + s3;
            txt3.setText(s4);
        }
        if (e.getSource() == Arcsin) {
            k3 = Math.asin(k1);
            k3 = k3 * 180 / PI;
            s3 = Double.toString(k3);
            s4 = "Arcsin( " + s1 + " ) = " + s3;
            txt3.setText(s4);
        }
        if (e.getSource() == Arccos) {
            k3 = Math.acos(k1);
            k3 = k3 * 180 / PI;
            s3 = Double.toString(k3);
            s4 = "Arccos( " + s1 + " ) = " + s3;
            txt3.setText(s4);
        }
        if (e.getSource() == Arctan) {
            k3 = Math.atan(k1);
            k3 = k3 * 180 / PI;
            s3 = Double.toString(k3);
            s4 = "Arctan( " + s1 + " ) = " + s3;
            txt3.setText(s4);
        }
        if (e.getSource() == Combinatory) {
            long n1, n2, n;
            n1 = (long) k1;
            n2 = (long) k2;
            n = Factorial(n2) / (Factorial(n1) * Factorial(n2 - n1));
            s1 = Long.toString(n1);
            s2 = Long.toString(n2);
            s3 = Long.toString(n);
            s4 = "C( " + s1 + ", " + s2 + " ) = " + s3;
            txt3.setText(s4);
        }
    }

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                Calculator f = new Calculator();
//              f.setSize(500, 500);
                f.pack();
                f.setVisible(true);
            }           
        });

    }
}