0

I am trying to add an extra feature to a school project. When I add a TextField to the code it appears at the top in the middle. I want to be able to change the location of the TextField

My code looks like:

import java.applet.*;
import java.awt.*;
import java.io.*;

public class Prac4 extends Applet {
char ch;
char letter = 'X';
char other = '#';
char c1;
char c2;
char c3;
char c4;
char c5;
char c6;

String s1;
String s2;
String s3;
String s4;
String s5 = "How are you?";
String s6 = "What are we testing here?";
String s7 = "  How many spaces are in this text?   ";
String s8;
String s9;
String s10;
String s11;

TextField text1;

int num = 6;
int x;

boolean answer;

public void init() {
    ch = '%';

    s1 = new String();
    s2 = new String("Hello");
    s3 = new String(s2);
    s4 = "  Hello   ";

    text1 = new TextField(20);
    text1.setText("JOHN DOE");
    add(text1);
    text1.setBounds(10,10,50,50);
}

public void paint (Graphics g) {
    g.drawString(s2 + " What a nice day.", 25, 25);
    g.drawString("s2 equals " +s2, 25, 50);

    g.drawString("Length of s2 is " + s2.length() + " characters", 25, 75);

    g.drawString("s2 to UpperCase is " + s2.toUpperCase(), 25, 100);
    g.drawString("s2 to LowerCase is " + s2.toLowerCase(), 25, 125);

    s1 = s4.trim();
    g.drawString("The origional s4 is:" + s4, 25, 150);
    g.drawString("After trimmming s4 is:" + s1, 25, 175); 

    s1 = s2.replace('H', 'h');
    g.drawString("s2 after a replace is " + s1, 25, 200);
    ch = s4.charAt(num);

    s1 = s2.substring(1, 4);
    g.drawString("s1 = s2.substring(1,4) returns " + s1, 25, 225);

    s1 = s2.substring(2,2);
    g.drawString("s1 = s2.substring(2,2) returns " + s1, 25, 250);

    g.drawString("With the string s6 = \"" + s6 + "\"", 25, 275);

    answer = s6.startsWith("Whi");
    g.drawString("answer = s6.startsWith(\"Whi\") reurns " + answer, 25, 300);

    answer = s6.endsWith("?");
    g.drawString("answer = s6.endsWith(\"?\") returns " + answer, 25, 325);

    x = "Wooloomooloo".indexOf("loo", 8);
    g.drawString("x = \"Woolloomooloo\".indexOf(\"loo\", 8) returns " + x + " but", 25, 350);

    x = "Wooloomooloo".indexOf("loo", 12);
    g.drawString("x = \"Woolloomooloo\".indexOf(\"loo\", 12) returns " + x, 25, 375);
    g.drawString("which says the substring \"loo\" was not found after  position 12", 25, 400);

    /*
     * Turn s7 into upper case
     *      display the result
     * 
     * Trim s7
     *      display the result
     */

    g.drawString("s7 in uppercase = \"" + s7.toUpperCase() + "\"", 550, 25);
    g.drawString("s7 without leading or trailing spaces = \"" + s7.trim() + "\"", 550, 50);

    /*
     * Define s8 as "alk on the wild sid"
     * Define c1 as "W"
     * Define c2 as "E"
     * Display c1 then s8 then c2
     */

    s8 = "alk on the wild sid";
    c1 = 'W';
    c2 = 'E';
    g.drawString(c1 + s8 + c2, 550, 100);

    /*
     * Define s9 as TRAVIS WESLEY
     * change s9 to lower case
     * Split string at " " using s9.split(" "); into s10 and s11
     * Define c3 as s10.charAt(0);
     * Define c4 as s11.charAt(0);
     * Define c5 as c3 upper case
     * Define c6 as c4 uppercase
     * Display s11 with replaced letter the s10 with replaced letter
     * 
     * Error occurs when the fist letter is repeated in the word 
     */

    s9 = text1.getText();
    s9 = s9.toLowerCase();
    String[] parts = s9.split(" ");
    s10 = parts[0];
    s11 = parts[1];

    c3 = s10.charAt(0);
    String s12 = String.valueOf(c3);
    c4 = s11.charAt(0);
    String s13 = String.valueOf(c4);
    c5 = Character.toUpperCase(c3);
    String s14 = String.valueOf(c5);
    c6 = Character.toUpperCase(c4);
    String s15 = String.valueOf(c6);
    g.drawString(s11.replaceFirst(s13, s15) + ", " + s10.replaceFirst(s12, s14), 550, 150);
}

public boolean action(Event e, Object o) {
    repaint();
    return true;
}
}

text1.setLocation(x, y); doesn't work

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Zephranoid
  • 3
  • 1
  • 7
  • Where do want it positioned? – Paul Samsotha Mar 25 '14 at 09:19
  • 1) Why code an applet? If it is due to spec. by teacher, please refer them to [Why CS teachers should stop teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why AWT rather than Swing? See my answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. – Andrew Thompson Mar 26 '14 at 04:55

2 Answers2

3

By the looks of your setBounds it looks like you want it centered at the top.

  1. Create your text field by using the constructor that defines the number of character columns

    new JTextField(20);
    
  2. Set the layout of your applet to FlowLayout

    setLayout(new FlowLayout());
    
  3. Just add the text field

    add(text);
    
  4. Setting location and size with null layouts is not the way. Learn to use the different layout managers by looking at Laying out Components Withing a Container

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
0

you have to use setbounds method like below for this if you do not want use any layouts.

    text1.setBounds(10,10,50,50);
    setlayout(null)

But this is not the correct way though.You have to use like below only.

    TextField text1 = new TextField(20);
    text1.setText("JOHN DOE");
    add(text1);
    text1.setBounds(10, 10, 100, 20);
    setLayout(new BorderLayout());

Note

Andrew Thompson notes:

Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or combinations of them1, along with layout padding & borders for white space2.



Community
  • 1
  • 1
Prakash
  • 693
  • 5
  • 17
  • This doesn't change where the TextField is located, it is still at the top in the middle. – Zephranoid Mar 25 '14 at 08:38
  • put your full code.because you also have to set the layout to null if you don't want any layout.like **frame.setLayout(null);** – Prakash Mar 25 '14 at 08:40
  • Its not letting me upload a past that is mostly code. Any ideas? – Zephranoid Mar 25 '14 at 08:54
  • what do you said.i didn't get it.say it clearly...try setting the layout first... – Prakash Mar 25 '14 at 08:56
  • Stack Overflow wont let me edit my post with all my code as the post can't contain mostly code. – Zephranoid Mar 25 '14 at 08:58
  • @X-Eon Ooops!!! then add some contents(a bit more about your problem) with it... **OR** maybe add it as an answer(not the way though) – Prakash Mar 25 '14 at 08:59
  • add **setLayout(null);** at last in your init method and change the setbounds value to fix textfield where you wanted it. – Prakash Mar 25 '14 at 09:27
  • What's wrong with you and you're going to down-vote me for no reason, besides the fact that someone down-voted you for legitimate reasons. I didn't even down vote you. You have a serious problem. Now I will though. – Paul Samsotha Mar 26 '14 at 05:02
  • 1
    @peeskillet k i am really sorry for that now i changed. you? – Prakash Mar 26 '14 at 05:04
  • *"now i changed."* Now I wish I could cancel that -1. Could you make an [edit](http://stackoverflow.com/posts/22628963/edit) (addition) to your answer with my comment (without the *last sentence*)? Then I can take away 1 down-vote. – Andrew Thompson Mar 26 '14 at 05:09
  • 1
    @AndrewThompson Yeah now i got how to roll it back to any version.But i'm not rolling back this one anymore though. And Thanks for your suggestions AndrewThompson.. – Prakash Mar 26 '14 at 05:27