* Purpose: to create a GUI that codes and decodes strings
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class CoderGui implements ActionListener{
private JTextField plntxt; //object representing a text field
private JTextField cphrtxt; //object representing a text field
private JButton encod; //object representing a button
private JButton decod; //object representing a button
private JButton reset; //object representing a button
private JFrame frame; //object representing the frame
private CoderDecoder cd; //object used to perform actions with the GUI
//default constructor to build the frame
public CoderGui(){
cd = new CoderDecoder();
cphrtxt = new JTextField(20);
plntxt = new JTextField(20);
encod = new JButton("Encode");
decod = new JButton("Decode");
reset = new JButton("Reset");
frame = new JFrame("Coder Decoder");
frame.setSize(340, 120);
frame.setLayout(new FlowLayout());
frame.setResizable(false);
cphrtxt.addActionListener(this);
plntxt.addActionListener(this);
encod.addActionListener(this);
decod.addActionListener(this);
reset.addActionListener(this);
frame.add(new JLabel("Plaintext: "));
frame.add(plntxt);
frame.add(new JLabel("Cyphertext: "));
frame.add(cphrtxt);
frame.add(encod);
frame.add(decod);
frame.add(reset);
frame.setVisible(true);
}//end CoderGui
//overwritten method that performs an action when an even occurs
public void actionPerformed(ActionEvent ae){
//else if ladder determining which event has occurred
if(ae.getActionCommand().equals("Encode"))
cphrtxt.setText(cd.getCoded(plntxt.getText()));
else if(ae.getActionCommand().equals("Decode"))
plntxt.setText(cd.getDecoded(cphrtxt.getText()));
else if(ae.getActionCommand().equals("Reset")){
cphrtxt.setText("");
plntxt.setText("");
}//end else if
}//end actionPerformed
//main to test code
public static void main(String[] args){
CoderGui cg = new CoderGui();
}//end main
}//end CoderGui
Published by Chris Chen
Chris is currently attending the University of California, Berkeley seeking an undergraduate's degree in Electrical Engineering Computer Science. He enjoys playing basketball, practicing kendo, hanging out w... View profile
- The Basics of Making a GUI in Java
- Introduction to Wireless Networking
- Photoshop Tutorial: Creating a Frame
- Country Kitchen Decor Craft Project: Lattice Recipe Box Frame
- SUN-SG8 Digital Photo Frame Review



