问题
I want to be able to get the string from the JFrame
. This is because the title of the JFrame
is not constant and is dependent on variables.
Is there a way to assign to a string variable the text of the title?
private void submitButtonActionPerformed(java.awt.event.ActionEvent evt) {
if(super.checkifEmptyFields(jPanel3)==false){
String LeaseName = jTextField2.getText().toLowerCase();
String sld[] =jDateChooser1.getDate().toString().split(" ");
String startLeasingDate = sld[0]+","+sld[1]+","+sld[5].toLowerCase();
String eld[] =jDateChooser1.getDate().toString().split(" ");
String EndLeasingDate = eld[0]+","+eld[1]+","+eld[5].toLowerCase();
String leaseAmount = jTextField3.getText();
String text = jTextField1.getText();
int floor = Integer.parseInt(text.substring(6, 7));
int flat = Integer.parseInt(text.substring(13));
//String name NewLease2.getTitle;
}
}
The class name is called NewLease2 and this is the function I want to perform when the user presses a button.
回答1:
Why dont you try:
JFrame j = new JFrame("My Title here");
String whatTitle = j.getTitle();
回答2:
In the constructor of the JFrame
, you could set a private instance variable and assign it the value of the title.
This will store the original value for the title of your instance object.
public class MyJFrame extends JFrame {
private String originalTitle;
public String getOriginalTitle() {
return this.originalTitle;
}
public MyJFrame() {
super();
this.originalTitle = this.getTitle(); // Store a snapshot of the title.
}
}
回答3:
I got it. Thanks so much for your help Xoce 웃 Пepeúpa. I just said this in my function.
private void submitButtonActionPerformed(java.awt.event.ActionEvent evt) {
if(super.checkifEmptyFields(jPanel3)==false){
String LeaseName = jTextField2.getText().toLowerCase();
String sld[] =jDateChooser1.getDate().toString().split(" ");
String startLeasingDate = sld[0]+","+sld[1]+","+sld[5].toLowerCase();
String eld[] =jDateChooser1.getDate().toString().split(" ");
String EndLeasingDate = eld[0]+","+eld[1]+","+eld[5].toLowerCase();
String leaseAmount = jTextField3.getText();
String text = jTextField1.getText();
int floor = Integer.parseInt(text.substring(6, 7));
int flat = Integer.parseInt(text.substring(13));
JFrame j = this;
String m =j.getTitle(); // << This is the bit that gets the title.
}
And now it works :)
来源:https://stackoverflow.com/questions/36041702/how-do-you-get-the-value-of-the-jframe-title