Java #48: BorderLayout App that Adds two Ints

———————Output———————–

———————Full Source Code———————–
//Programmer:Akinwale Owi
//Company: Dagba Computers
//Program Purpose: Add two Integers on a JFrame – Sum on New JFrame

package dagbacomputers;

//Imports
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
myframe();
}
});

}
//End of Main Function

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

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

//Setting the JFrame Color
mathFrame.getContentPane().setBackground(Color.GREEN);

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

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

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

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

//Creating the center JLabel, which will be a plus sign +, so the user knows to put the numbers on either side
JLabel plusSign = new JLabel(” +”);

//——————————-Sizes—————————————-

//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 plusSign
plusSign.setPreferredSize(new Dimension(100,100));

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

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

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

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

public void actionPerformed(ActionEvent e)
{

//Reading in integer of first box
int num1 = Integer.parseInt(firstNum.getText());

//Reading in integer of second box
int num2 = Integer.parseInt(secondNum.getText());

//Adding the two integers
int sum = num1 + num2;

//——————————Tests——————————————–

//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(“Sum =:” + sum + “\n”);

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

//Setting Answers Close Function
answerFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);//

//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(“Sum = ” + sum + “\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
answerFrame.pack();
answerFrame.setVisible(true);

//———————————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(calculate, BorderLayout.PAGE_END);

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

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

//pack and visibility
mathFrame.pack();
mathFrame.setVisible(true);

}
//End of myFunction

}
//End of DagbaComputers Class

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

Blog at WordPress.com.

Up ↑

%d bloggers like this: