Im writing the Applet GUI code that uses another class to translate a string into pig latin (dubbed PigLatin.class) My gui has two text areas, a few labels, and two buttons, but I'm having some trouble with event handling.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TxtCrypt extends JApplet implements ActionListener {
LayoutManager lo;
JButton encBtn, clrBtn;
JTextArea preCrypTxt, postCrypTxt;
JLabel titleLbl, infoLbl, plainLbl, ciphLbl;
String inStr, outStr;
// public TxtCrypt() {
// }
public void init () {
lo = new FlowLayout();
setLayout(lo);
setBackground(Color.BLUE);
titleLbl = new JLabel("Text Encryption Program");
titleLbl.setFont(new Font("SansSeriff", Font.BOLD, 20));
titleLbl.setForeground(Color.white);
infoLbl = new JLabel("\u00A9 McGalliger Manufacturing");
infoLbl.setFont(new Font("SansSeriff", Font.BOLD, 16));
infoLbl.setForeground(Color.white);
plainLbl = new JLabel("Plaintext: ");
plainLbl.setFont(new Font("SansSeriff", Font.BOLD, 12));
plainLbl.setForeground(Color.white);
ciphLbl = new JLabel("Ciphertext: ");
ciphLbl.setFont(new Font("SansSeriff", Font.BOLD, 12));
ciphLbl.setForeground(Color.white);
encBtn = new JButton("Encrypt");
encBtn.setActionCommand("encAct");
encBtn.setToolTipText("Enter and ecrypt text.");
clrBtn = new JButton("Clear");
clrBtn.setActionCommand("clrAct");
clrBtn.setToolTipText("Clear the plain and ecrypted text.");
preCrypTxt = new JTextArea("Enter your text to encrypt", 5, 35);
preCrypTxt.setLineWrap(true);
preCrypTxt.setWrapStyleWord(true);
postCrypTxt = new JTextArea(5, 35);
postCrypTxt.setLineWrap(true);
postCrypTxt.setWrapStyleWord(true);
JPanel title = new JPanel();
title.setLayout(new BorderLayout());
title.setBackground(Color.BLUE);
title.add(titleLbl, BorderLayout.CENTER);
JPanel top = new JPanel();
top.setLayout(new BorderLayout());
top.setBackground(Color.BLUE);
top.add(plainLbl, BorderLayout.NORTH);
top.add(preCrypTxt, BorderLayout.CENTER);
JPanel mid = new JPanel();
mid.setLayout(new BorderLayout());
mid.setBackground(Color.BLUE);
mid.add(encBtn, BorderLayout.WEST);
mid.add(clrBtn, BorderLayout.EAST);
JPanel bot = new JPanel();
bot.setLayout(new BorderLayout());
bot.setBackground(Color.BLUE);
bot.add(ciphLbl, BorderLayout.NORTH);
bot.add(postCrypTxt, BorderLayout.CENTER);
bot.add(infoLbl, BorderLayout.SOUTH);
add(title);
add(top);
add(mid);
add(bot);
encBtn.addActionListener(this);
clrBtn.addActionListener(this);
}
public void actionPerformed (ActionEvent e) {
if ("encAct".equals(e.getActionCommand())) {
String inStr = preCrypTxt.getText();
//use PigLatin.encrypt
postCrypTxt.setText(outStr);
}
else if("clrAct".equals(e.getActionCommand())) {
preCrypTxt.setText("");
postCrypTxt.setText("");
}
else {
//error
}
}
}
I'm need to take the text from the first textarea pass it to the PigLatin class into the encrypt method which will return a string and add that new string to the second text area. Thats the basis of the project, not sure why the applet frames background isn't blue(setBackground) nor clear button isnt working. I would also like if when users click and change focus to the textarea it would remove the starting text, but Im new to event handling.