Tag: java awt program

  • Java Program to Create a Calculator Using AWT Controls

    Today in this java programming we will learn a Java Program to Create a Calculator Using AWT Controls(Abstract Window Toolkit). This will be a basic calculator with a user interface. The AWT components we will be using are Textfields, Buttons, and labels.

    As we know a simple calculator requires two fields for input and one field to show the result of the operation performed on those two operands.


    Program to create Calculator in Java using AWT

    Source Code:

    import java.awt.*;
    import java.awt.event.*;
     
    class Calculator implements ActionListener
    {
    	//Declaring Objects
    	Frame f = new Frame("Calculator AWT");
    	Label l1 = new Label("First Number:");
    	Label l2 = new Label("Second Number:");
    	Label l3 = new Label("Result:");
    	
    	TextField t1 = new TextField();
    	TextField t2 = new TextField();
    	TextField t3 = new TextField();
    	
    	Button b1 = new Button("Add");
    	Button b2 = new Button("Sub");
    	Button b3 = new Button("Mul");
    	Button b4 = new Button("Div");
    	Button b5 = new Button("Quit");
    	
    	Calculator()
        {      
           //Background Color
           f.setBackground(Color.GRAY) ;
    
    		//Giving Bounds for Labels
    		l1.setBounds(50,100,100,20);
    		l2.setBounds(50,140,100,20);
    		l3.setBounds(50,180,100,20);
    		
            //Giving Bounds for Labels
    		t1.setBounds(200,100,100,20);
    		t2.setBounds(200,140,100,20);
    		t3.setBounds(200,180,100,20);
    		
            //Giving Bounds for Labels
    		b1.setBounds(50,230,50,20);
    		b2.setBounds(110,230,50,20);
    		b3.setBounds(170,230,50,20);
    		b4.setBounds(230,230,50,20);
    		b5.setBounds(290,230,50,20);
    		
    		//Add components to the frame
    		f.add(l1);
    		f.add(l2);
    		f.add(l3);
    		
    		f.add(t1);
    		f.add(t2);
    		f.add(t3);
    		
    		f.add(b1);
    		f.add(b2);
    		f.add(b3);
    		f.add(b4);
    		f.add(b5);
    		
            //Action Buttons
    		b1.addActionListener(this);
    		b2.addActionListener(this);
    		b3.addActionListener(this);
    		b4.addActionListener(this);
    		b5.addActionListener(this);
    		
    		f.setLayout(null);
    		f.setVisible(true);
    		f.setSize(400,350);
    	}
    	
    	public void actionPerformed(ActionEvent e)
    	{
    		int n1=Integer.parseInt(t1.getText());
    		int n2=Integer.parseInt(t2.getText());
    		
    		//Buttons Operations
    		if(e.getSource() == b1)
    		{
    		  t3.setText(String.valueOf(n1+n2));
    		}
    			
    		if(e.getSource() == b2)
    		{
    		  t3.setText(String.valueOf(n1-n2));
    		}
    		
    		if(e.getSource() == b3)
    		{
    		  t3.setText(String.valueOf(n1*n2));
    		}
    		
    		if(e.getSource() == b4)
    		{
    		  t3.setText(String.valueOf(n1/n2));
    		}
    		
    		if(e.getSource() == b5)
    		{
    		  System.exit(0);
    		}
    	}
    	
    	public static void main(String args[])
    	{
                new Calculator();
    	}
    }

    Output: Save the file with .java extension and open the command prompt on that directory and run the following code.

    C:\java>javac Calculator.java
    C:\java>java Calculator

    You will see the following result:

    This is the end of the article Simple calculator program in java using AWT with output.
    Learn more about Java Programs.


  • Java Program to Create Login Window using AWT Controls

    This article is based on the Java Program to create Login Window using AWT Controls. Here we will use the AWT components Label, Button and Textfield. We will see the code to create a user_name and password text field with login and signup buttons.

    AWT program in Java with output:


    Awt Program in java with Output to Create Login Window

    import java.awt.*;
    import java.awt.event.*;
    
    class LoginWindowAWT extends Frame
    {
        //Declaring text fields and Buttons
        TextField userName, password;
        Button btn1, btn2;
        
        LoginWindowAWT()
        {
            setLayout(new FlowLayout());
            this.setLayout(null);
            
            //Setting the name and password that will be displayed
            Label u_name = new Label("User Name:",Label.CENTER);
            Label pass = new Label("Password:",Label.CENTER);
            
            //username declaration
            userName = new TextField(20);
            this.add(u_name);
            this.add(userName);
    
            //Password declaration
            password = new TextField(20);
            this.add(pass);
            this.add(password);
            
            password.setEchoChar('#');
            
            //Button Declaration
            btn1 =new Button("LogIn");
            btn2 =new Button("SignUp");
            this.add(btn1);
            this.add(btn2);
            
            //Setting the display Bounds
            userName.setBounds(200,100,90,20);
            u_name.setBounds(70,90,90,40);
            
    
            password.setBounds(200,140,90,20);
            pass.setBounds(70,130,90,40);
    
            btn1.setBounds(100,260,70,40);
            btn2.setBounds(180,260,70,40);
     
        }
        public static void main(String args[])
        {
            //creating an object
            LoginWindowAWT ml=new LoginWindowAWT();
            
            //background Color
            ml.setBackground(Color.RED) ;
    
            ml.setTitle("Login Window AWT");
            ml.setVisible(true);
            ml.setSize(400,400);
            
        }
    }

    Output: Save the file with .java extension and open the command prompt on that directory and run the following code.

    C:\java>javac LoginWindowAWT.java
    C:\java>java LoginWindowAWT

    You will see the following result:


    This is the end of the article to create a Login Window with an AWT program in Java with output.
    Learn more about Java Programs.