I have made a textfield applet, and would like it to do the following:
If the string typed in contains the letter 'l', then i would like it to always be displayed in red, otherwise I would like the text to be displayed in green.
Here is the code I have so far:
import java.util.*; import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class Sarah extends Applet implements ActionListener{ //public static void main(String[] args) // TODO Auto-generated method stub String name; TextField input; Color col; Font f; Scanner sc=new Scanner (System.in); char firstname; public void init() { f = new Font("Helvetica", Font.BOLD,19); setBackground (Color.white); input = new TextField(20); do { firstname = sc.next().charAt(0); } while (firstname <'A' || firstname > 'Z'); if (firstname == 'l') { col=Color.red; } else { col=Color.green; } add(input); input.addActionListener(this); } public void actionPerformed (ActionEvent e) { name = e.getActionCommand(); repaint(); } public void paint (Graphics g) { g.setFont(f); g.setColor(col); g.drawString(name, 75, 75); } }
Any help asap would really be appreciated! Thank you