如何将文本字段传递给框架的标题?
how to pass a textfield to a title of a frame?
我需要将文本字段文本传递给框架标题。请。
例如:如果我在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();
}
}
据我了解,您的要求是从文本字段中获取文本,并在用户单击按钮时将其设置为第二个 window 的标题。
您可以通过删除变量 nombre
并直接使用 TStudent.getText()
设置标题来做到这一点。
我只更改了以下行并且有效:
frame3.setTitle(TStudent.getText()); // AND HERE <--
(我同意问题中的评论。您应该使用正确的 Java 编码约定。如果没有特定的使用原因,对于 Java GUI,最好使用 Swing 而不是 AWT AWT.)
完整代码:
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();
}
}
我需要将文本字段文本传递给框架标题。请。
例如:如果我在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();
}
}
据我了解,您的要求是从文本字段中获取文本,并在用户单击按钮时将其设置为第二个 window 的标题。
您可以通过删除变量 nombre
并直接使用 TStudent.getText()
设置标题来做到这一点。
我只更改了以下行并且有效:
frame3.setTitle(TStudent.getText()); // AND HERE <--
(我同意问题中的评论。您应该使用正确的 Java 编码约定。如果没有特定的使用原因,对于 Java GUI,最好使用 Swing 而不是 AWT AWT.)
完整代码:
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();
}
}