Java 获取我之前设置的值
我的程序有点问题,包括两个类。 我想在用户单击程序的“新建”按钮时使用jTextField1的文本。然后我想在第二个类中使用这个值来编写一个简单的文件 这是1类的代码:Java 获取我之前设置的值,java,class,setter,getter,Java,Class,Setter,Getter,我的程序有点问题,包括两个类。 我想在用户单击程序的“新建”按钮时使用jTextField1的文本。然后我想在第二个类中使用这个值来编写一个简单的文件 这是1类的代码: public class Class1 extends javax.swing.JFrame { Class2 network = new Class2(); private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
public class Class1 extends javax.swing.JFrame {
Class2 network = new Class2();
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
network.setID(jTextField1.getText());
Class2.New();
}
}
public class Class1 extends javax.swing.JFrame {
Class2 network = new Class2();
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
network.setID(jTextField1.getText());
network.New();
}
}
第二类:
public class Class2 {
String tempID = "";
public void setID(String ID){
ID = tempID;
}
public static void New(){
Class1 ID = new Class1();
/* Here i want to save the value in a string */
String f = string + ".dat";
try{
FileOutputStream outx = new FileOutputStream("C:\\temp\\NetworkAdmin\\data\\" + f);
PrintStream out = new PrintStream(outx);
out.println("test");
out.close();
}catch(Exception e){
System.err.println(e.toString());
}
}
}
public class Class2 {
String tempID = "";
public void setID(String ID){
tempID = ID;
}
public void New(){
Class1 ID = new Class1();
/* Here i want to save the value in a string */
//String f = string + ".dat";
String f = tempID + ".dat"; // this should work - use tempID which you set before
try{
FileOutputStream outx = new FileOutputStream("C:\\temp\\NetworkAdmin\\data\\" + f);
PrintStream out = new PrintStream(outx);
out.println("test");
out.close();
}catch(Exception e){
System.err.println(e.toString());
}
}
}
我希望这足够了;感谢您的帮助您在Class2中有一个实例变量,但您没有设置它。我猜你真正想做什么而不是
public void setID(String ID){
ID = tempID;
}
改为
public void setID(String ID){
tempID = ID;
}
然后使用
String f = tempID + ".dat";
注意:然后您必须将tempID声明为static,或者将方法更改为non static并使用对象(即network)调用。对于每次调用Neu,您都要保存一个新值。这就是为什么要使用类
Neu item = new Neu();
String item = item.getValue();
除非有真正令人信服的理由使用static,否则请避免使用它。它只允许糟糕的编码实践溜进面向对象的代码中 您无法访问静态方法中的非静态字段。因此,要么使方法和字段都是静态的,要么是非静态的。同样在类2的setID中,您设置ID=tempID,它应该是tempID=ID 这是1类的代码:
public class Class1 extends javax.swing.JFrame {
Class2 network = new Class2();
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
network.setID(jTextField1.getText());
Class2.New();
}
}
public class Class1 extends javax.swing.JFrame {
Class2 network = new Class2();
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
network.setID(jTextField1.getText());
network.New();
}
}
第二类:
public class Class2 {
String tempID = "";
public void setID(String ID){
ID = tempID;
}
public static void New(){
Class1 ID = new Class1();
/* Here i want to save the value in a string */
String f = string + ".dat";
try{
FileOutputStream outx = new FileOutputStream("C:\\temp\\NetworkAdmin\\data\\" + f);
PrintStream out = new PrintStream(outx);
out.println("test");
out.close();
}catch(Exception e){
System.err.println(e.toString());
}
}
}
public class Class2 {
String tempID = "";
public void setID(String ID){
tempID = ID;
}
public void New(){
Class1 ID = new Class1();
/* Here i want to save the value in a string */
//String f = string + ".dat";
String f = tempID + ".dat"; // this should work - use tempID which you set before
try{
FileOutputStream outx = new FileOutputStream("C:\\temp\\NetworkAdmin\\data\\" + f);
PrintStream out = new PrintStream(outx);
out.println("test");
out.close();
}catch(Exception e){
System.err.println(e.toString());
}
}
}
您可以将文本作为参数传递给New。Class1中缺少代码。是否可以使其可编译?我仍然不确定你到底想说什么?你认为这个答案真的解决了问题吗?@Plexus我相信是的,但我可能错了。你认为这个解决方案会有什么问题?