[英]how to pass a textfield to a title of a frame?
我需要將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();
}
}
據我了解,您的要求是從文本字段中獲取文本,並在用戶單擊按鈕時將其設置為第二個窗口的標題。
您可以通過刪除變量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();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.