我正在尝试为学校项目添加额外功能。
当我向代码添加TextField时,它会出现在中间的顶部。我希望能够更改TextField
我的代码如下:
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);
无法正常工作
答案 0 :(得分:3)
根据setBounds
的外观,您希望它位于顶部的中心位置。
使用定义字符列数
的构造函数创建文本字段new JTextField(20);
将小程序的布局设置为FlowLayout
setLayout(new FlowLayout());
只需添加文字字段
即可add(text);
使用null布局设置位置和大小不是方法。通过查看Laying out Components Withing a Container
答案 1 :(得分:0)
如果您不想使用任何布局,则必须使用如下所示的setbounds方法。
text1.setBounds(10,10,50,50);
setlayout(null)
但这不是正确的方法。你只能使用以下方式。
TextField text1 = new TextField(20);
text1.setText("JOHN DOE");
add(text1);
text1.setBounds(10, 10, 100, 20);
setLayout(new BorderLayout());
Andrew Thompson指出:
Java GUI可能必须在许多平台上工作,在不同的屏幕分辨率和使用不同的PLAF。因此,它们不有助于精确放置组件。要组织 健壮的GUI 的组件,而是使用布局管理器,或combinations of them 1 ,以及布局填充& white space 2 的边框。
- 醇>