Java #49: BorderLayout App, Multiplies Two Doubles


———————Full Source Code————————

//Programmer:Akinwale Owi
//Company: Dagba Computers
//Program Purpose: Multipy Two Doubles on a JFrame – Product on New JFrame

package dagbacomputers;

import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

//Beginning of DagbaComputers (Class containing the Main function)
public class DagbaComputers extends JFrame

//Main Function
public static void main(String[] args)
//define later
javax.swing.SwingUtilities.invokeLater(new Runnable ()

public void run()
//Calling myFrame Function

//End of Main Function

//Function that I build the JFrame in
private static void myframe()
//Creating the JFrame
JFrame mathFrame = new JFrame (“Dagba Computers:Multipy two Doubles App”);

//What happens when we close the JFrame
mathFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//Just closes like normal

//Setting the JFrame Color

//Frame Dimensions
mathFrame.setPreferredSize(new Dimension (500,500));

//Creating the in frame header
JLabel header = new JLabel (” Welcome! Please enter two numbers in the spaces provided!”);

//Creating the TextField that will hold the First Double
JTextField firstNum = new JTextField(“”);

//Creating the TextField that will hold the Second Double
JTextField secondNum = new JTextField(“”);

//Creating the center JLabel, which will be a ‘x’, so the user knows to put the numbers on either side
JLabel x = new JLabel(” x”);


//Setting the size of header
header.setPreferredSize(new Dimension(100, 100));

//Setting the size of firstNum
firstNum.setPreferredSize(new Dimension(100,100));

//Setting the size of secondNum
secondNum.setPreferredSize(new Dimension(100,100));

//Setting the size of x
x.setPreferredSize(new Dimension(100,100));

//——————————End of Sizes—————————————-

//——————–Creating the Multiply Button——————————–
JButton Multiply = new JButton(“Multiply”);

//Setting the size of the Multiply button
Multiply.setPreferredSize(new Dimension(100, 100));

//——————————Starting the action listener for the ‘Multiply’ Button——————————–
Multiply.addActionListener(new ActionListener()

public void actionPerformed(ActionEvent e)

//Reading in integer of first box
double num1 = Double.parseDouble(firstNum.getText());

//Reading in integer of second box
double num2 = Double.parseDouble(secondNum.getText());

//Multiplying the two doubles
double product = num1 * num2;


//Printing out num1 to test
System.out.printf(“Num1 Value: ” + num1 + “\n”);

//Printing out num2 to test
System.out.printf(“Num2 Value:” + num2 +”\n”);

//Printing out sum to Test
System.out.printf(“Product =:” + product + “\n”);

//———————————Creating the answer JFrame—————————————————
//Creating the answer’s JFrame
JFrame answerFrame = new JFrame(“Answer Frame”);

//Setting Answers Close Function

//Setting the size of answer
answerFrame.setPreferredSize(new Dimension (300,300));

//Putting a text box inside of answerFrame to fully test
JTextField sumField = new JTextField(“Product = ” + product + “\n”);

//adding sumField to answerFrame
answerFrame.add(sumField, BorderLayout.CENTER); //No size setting so it will take up the whole frame

//Pack & Set Visible for Answer

//———————————End of answer JFrame Creation———————————————————
//—————————————End of action listener for ‘calculate’ Button ——————–

//——————————————–Adding Items to the mathFrame————————————

//Adding header
mathFrame.add(header, BorderLayout.PAGE_START);

//Adding firstNum
mathFrame.add(firstNum, BorderLayout.LINE_START);

//Adding secondNum
mathFrame.add(secondNum, BorderLayout.LINE_END);

//Adding calculate button
mathFrame.add(Multiply, BorderLayout.PAGE_END);

//Adding plusSign
mathFrame.add(x, BorderLayout.CENTER);

//————————End of Adding Items to mathFrame————————————–

//pack and visibility

//End of myFunction

//End of DagbaComputers Class

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s