简体   繁体   中英

how to pass a textfield to a title of a frame?

I need to pass the Textfield text to a frame title. Please.

For example: if I write in the TextField "Hello", it should appear in the title of the Frame "Hello"

I have not found information in forums or support pages.

I will put the example code, what I need is on line 25 and 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();
   }
}

As I understand, your requirement is to get the text from text field and set it as second window's title when user clicks the button.

You can do this by getting rid of the variable nombre and directly using TStudent.getText() to set the title.

I have only changed below line and it works:

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

(I agree with the comments in the question. You should use correct Java coding conventions. Better to use Swing than AWT for Java GUIs if there isn't a particular reason for using AWT.)

Full code:

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();
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM