Member Avatar for loserspearl

My Applet is working but I have 2 issues, I cannot get away from the 'metal' plaf background (trying to make it all solid blue) and when the user clicks on the 1st text box I want the prompting text to clear for the user to type their own.

No frame using an applet but I do have setBackground(color.blue) but that doesnt seem to work.
I could use a mouselistener but Im already implementing actionlistener for my buttons. And I cannot use actionlistener on textarea

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, spaceLbl;
    String inStr, outStr;

    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 2011");
        infoLbl.setFont(new Font("SansSeriff", Font.PLAIN, 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);
        
        spaceLbl = new JLabel("        ");

        encBtn = new JButton("Encrypt");
        encBtn.setActionCommand("encAct");
        encBtn.setToolTipText("Enter and ecrypt text.");
        encBtn.addActionListener(this);

        clrBtn = new JButton("Clear");
        clrBtn.setActionCommand("clrAct");
        clrBtn.setToolTipText("Clear the plain and ecrypted text.");
        clrBtn.addActionListener(this);

        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);
            //spacer between the buttons
            mid.add(spaceLbl, BorderLayout.CENTER);
            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);

    }
     
    public void actionPerformed (ActionEvent e) {
            
        if ("encAct".equals(e.getActionCommand())) {
            inStr = preCrypTxt.getText();
	    outStr = PigLatin.encrypt(inStr);         
            postCrypTxt.setText(outStr);
        }
        else if("clrAct".equals(e.getActionCommand())) {
            preCrypTxt.setText("");    
            postCrypTxt.setText("");
        }
        else {
            //for use in my browsers java console
            System.out.println("error here");
        }
    }    
}

Recommended Answers

All 6 Replies

Try getContentPane().setBackground(Color.BLUE);

Member Avatar for loserspearl

awesome! all blue now!

Still trying to figure out how to use an action event to clear a textfield prompt when clicked.

You would use a mouse listener for that.

Member Avatar for loserspearl

But im already implementing actionlistener, how can i use both in the same class?

You can implement as many interfaces as you like. You could also write a separate inner class for your mouse listener, or you could use an anonymous inner class declaration

preCrypTxt.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseEntered(MouseEvent e) {
        ...
    }
});
Member Avatar for loserspearl

never knew that thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.