Java #47: First and Last Name Swap App (Strings Only)[BorderLayout]

—————————-Sample Output—————————

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

//Programmer:Akinwale Owi
//Company: Dagba Computers
//Program Purpose: First and Last Name Swap App

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 sampleFrame = new JFrame (“Dagba Computers: Name Swap”);

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

//Setting the JFrame Color
sampleFrame.getContentPane().setBackground(Color.WHITE);

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

//Creating the in frame header
JLabel header = new JLabel (“Welcome to the Dagba Computer Name Swap! Please enter your name below!”);

//Creating the TextField that will hold the first name
JTextField first = new JTextField(“(Clear)”);

//Creating the TextField that will hold the last name
JTextField last = new JTextField(“(Clear)”);

//Creating the center JLabel
JLabel centerP = new JLabel(“Please enter your first(LEFT) and last(RIGHT)”);

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

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

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

public void actionPerformed(ActionEvent e)
{
//Reading in firstName text
String firstName = first.getText();
//Reading in lastName text
String lastName = last.getText();

//Printing out first and last to Test
System.out.printf(“First Name =: ” + firstName + “\n”);//First Name Test

System.out.printf(“Last Name =: ” + lastName + “\n”);//Last Name Test

//Creating the answer that we will print out on a new JFrame
String answer = lastName + ” ” + firstName;//Creating answer

//Another Test: Adding First and Last Name print
System.out.printf(“Swapped =” + answer + “\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 answer to fully test
JTextField answerField = new JTextField(answer + “\n”);

//adding answerField to answerFrame
answerFrame.add(answerField, 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 ‘submit’ Button ——————–

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

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

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

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

//——————————————–Adding Items to the JFrame————————————

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

//Adding first
sampleFrame.add(first, BorderLayout.LINE_START);

//adding last
sampleFrame.add(last, BorderLayout.LINE_END);

//Adding submit button
sampleFrame.add(submit, BorderLayout.PAGE_END);

//Adding centerP
sampleFrame.add(centerP, BorderLayout.CENTER);

//—————End of Item Add————————-

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

}
//End of myFunction

}
//End of DagbaComputers Class

——————————End of Program————————–

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: