0

学校のプロジェクトに特別な機能を追加しようとしています。コードに 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);動作しません

4

2 に答える 2

3

あなたの見た目setBoundsでは、上部の中央に配置したいようです。

  1. 文字列の数を定義するコンストラクターを使用して、テキスト フィールドを作成します。

    new JTextField(20);
    
  2. アプレットのレイアウトをFlowLayout

    setLayout(new FlowLayout());
    
  3. テキストフィールドを追加するだけ

    add(text);
    
  4. null レイアウトで位置とサイズを設定するのは適切ではありません。コンテナを使用したコンポーネントのレイアウトを参照して、さまざまなレイアウト マネージャーの使用方法を学びます。

于 2014-03-25T09:24:56.110 に答える
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());

ノート

アンドリュー・トンプソンは次のように述べています。

Java GUI は、さまざまな画面解像度やさまざまな PLAF を使用するさまざまなプラットフォームで動作する必要がある場合があります。そのため、コンポーネントを正確に配置するのに役立ちません。堅牢な GUIのコンポーネントを整理するには、代わりにレイアウト マネージャー、またはそれらの組み合わせ1を、余白のレイアウト パディングと境界線と共に使用します2



于 2014-03-25T08:27:23.330 に答える