如何将文本字段传递给框架的标题?

问题描述 投票:0回答:1

我需要将Textfield文本传递给帧标题。请。

例如:如果我在TextField中写“Hello”,它应该出现在Frame“Hello”的标题中

我没有在论坛或支持页面中找到信息。

我将把示例代码放在第25行和第40行。

import java.awt.*;
import java.awt.event.*;
public class HELPME extends Frame{
    Frame frame2,frame3;
    Button Close,Close1;
    Label Student;
    TextField TStudent;
    String nombre;
    Button Result;


    public HELPME(){
        frame2=new Frame();
        frame2.setSize(300,150);
        frame2.setVisible(true);
        frame2.setLocationRelativeTo(null);
        frame2.setLayout(null);


        Student = new Label("Student: ");
        Student.setBounds(20, 50, 50, 30);
        frame2.add(Student);

        TStudent = new TextField();
        nombre=TStudent.getText();  // ¡¡HEREE!!  <--
        TStudent.setBounds(80, 50, 100, 30);
        frame2.add(TStudent);

        Result=new Button("Result");
        Result.setBounds(80, 100, 57, 30);
        frame2.add(Result);

        Result.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                            frame3=new Frame();
                            frame3.setSize(500,150);
                            frame3.setVisible(true);
                            frame3.setLocationRelativeTo(null);
                            frame3.setLayout(null);
                            frame3.setTitle(nombre); // AND HERE <-- 

                    }
                  });
        //close the window
        frame2.addWindowListener(new WindowAdapter(){
        public void windowClosing(WindowEvent we){
        System.exit(0);
         } 
      });
    }

    public static void main(String args[]){
    HELPME prog = new HELPME();
   }
}
java swing awt
1个回答
0
投票

据我了解,您的要求是从文本字段中获取文本,并在用户单击按钮时将其设置为第二个窗口的标题。

你可以通过摆脱变量nombre并直接使用TStudent.getText()设置标题来做到这一点。

我只改变了以下线,它的工作原理:

frame3.setTitle(TStudent.getText()); // AND HERE <--

(我同意问题中的评论。你应该使用正确的Java编码约定。如果没有使用AWT的特殊原因,最好使用Swing而不是AWT for Java GUI。)

完整代码:

import java.awt.*;
import java.awt.event.*;
public class HELPME extends Frame{
  Frame frame2,frame3;
  Button Close,Close1;
  Label Student;
  TextField TStudent;
  String nombre;
  Button Result;


  public HELPME(){
    frame2=new Frame();
    frame2.setSize(300,150);
    frame2.setVisible(true);
    frame2.setLocationRelativeTo(null);
    frame2.setLayout(null);


    Student = new Label("Student: ");
    Student.setBounds(20, 50, 50, 30);
    frame2.add(Student);

    TStudent = new TextField();
    nombre=TStudent.getText();  // ¡¡HEREE!!  <--
    TStudent.setBounds(80, 50, 100, 30);
    frame2.add(TStudent);

    Result=new Button("Result");
    Result.setBounds(80, 100, 57, 30);
    frame2.add(Result);

    Result.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e){
        frame3=new Frame();
        frame3.setSize(500,150);
        frame3.setVisible(true);
        frame3.setLocationRelativeTo(null);
        frame3.setLayout(null);
        frame3.setTitle(TStudent.getText()); // AND HERE <--

      }
    });
    //close the window
    frame2.addWindowListener(new WindowAdapter(){
      public void windowClosing(WindowEvent we){
        System.exit(0);
      }
    });
  }

  public static void main(String args[]){
    HELPME prog = new HELPME();
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.