Results 1 to 4 of 4
Thread: text editor problem
- 03-04-2011, 02:46 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 2
- Rep Power
- 0
text editor problem
Hi I need to create a small applet that reads in a script and then displays it on the applet window. I also need every "l" to be red and every "y" to be green. So far I have the following code (below) but when I run this code only a button appears and no text entry box. I understand that I have to use loops to be able to create the specific coloured letters but I have never done this before so any help would be greatly appreciated.
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.awt.Graphics;
import javax.swing.JTextField;
public class textfield extends Applet implements ActionListener
{
Button okButton;
String text1;
JTextField textField = new JTextField(30);
public void paint(Graphics g)
{
g.setFont (new Font ("Helvetica", Font.BOLD, 20));
g.setColor (Color.black);
g.drawString(text1,20,100);
}
public void init()
{
setLayout(new FlowLayout());
okButton = new Button("Enter Text");
text1 = textField.getText();
add(okButton);
okButton.addActionListener(this);
}
public void actionPerformed(ActionEvent evt)
{
if (evt.getSource() == okButton)
repaint();
}
}
- 03-04-2011, 02:56 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Use str.substring or charat to loop through the input and change to font color of the correct itema, are wanting the colors to switch after the ok button is hit? If so have the button listener set a string object equal to the field, then loop through the string and change the I and y's then set the textfield with the modified string.
- 03-04-2011, 03:10 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 2
- Rep Power
- 0
Hi, yes I need to type into the text editor box, which currently wont display as there is some sort of error which I dont know? Thank you for that answer but I am new to java and dont know how to do loops. Dont suppose you could give me a short example of some kind if thats not too much hassle? Sorry to be a pain.
- 03-04-2011, 03:26 PM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
I can certainly help you out, i will give you an example program to run which does extremely simple string manipulation.
this just shows you how to split a string. Your program will be more complex as it will have to perform some tests and actually change values. Look up stringbuilder as wellJava Code:public class MyClass{ String s; MyClass(){ s = "hello"; } public static void main(String[] args){ MyClass mc = new MyClass(); for(int j = 0; j < mc.s.length(); j++){ System.out.println(mc.s.substring(j, j + 1)); } } }
Similar Threads
-
text editor does not open file
By globo in forum Advanced JavaReplies: 2Last Post: 11-21-2010, 07:39 PM -
Help for a Text Editor.
By necroninja in forum Advanced JavaReplies: 0Last Post: 08-01-2009, 05:21 PM -
SWT Text Editor Demo
By Java Tip in forum SWTReplies: 0Last Post: 07-11-2008, 04:35 PM -
SWT Text Editor
By Java Tip in forum SWTReplies: 0Last Post: 07-07-2008, 04:43 PM -
Problem in Text Editor
By mustahsan4u in forum New To JavaReplies: 2Last Post: 03-26-2008, 02:34 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks