我在这里失去了。
我需要从JText字段获取文本?将其保存为字符串,然后在同一类的main方法中使用它。我犯了错误
无法从静态上下文引用非静态变量文件
但是方法main必须保持静态,我不能对该字段做任何事情,因为我使用标准的NetBeans工具来制作JFrame应用程序。
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String name = jTextField1.getText();
file = new File( path+"\\"+name+".txt" );
}
请帮忙!
P.S。这是整个班级
package holtwinters;
import static holtwinters.HoltWinters.sum;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Scanner;
public class NewJFrame extends javax.swing.JFrame {
/**
* Creates new form NewJFrame
*/
public NewJFrame() {
initComponents();
}
String path = "C:\\Users\\Jane\\Desktop";
File file;
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTextField1.setText("Введите номер банкомата");
jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(evt);
}
});
jButton1.setText("Ввести");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(25, 25, 25)
.addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 280, Short.MAX_VALUE)
.addGap(18, 18, 18)
.addComponent(jButton1)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(136, 136, 136)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1))
.addContainerGap(141, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String name = jTextField1.getText();
file = new File( path+"\\"+name+".txt" );
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
/* public void run(){
String name = jTextField1.getText();
}
*/
public static void main(String args[])
throws FileNotFoundException, IOException{
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
new NewJFrame();
// new NewJFrame().run();
new NewJFrame().jTextField1ActionPerformed(null);
BufferedReader br = new BufferedReader (
new InputStreamReader(
new FileInputStream( file ), "UTF-8"
)
);
String line = null;
while ((line = br.readLine()) != null) {
try {
Long y = Long.valueOf(line);
// System.out.println(y);
} catch (NumberFormatException e) {
System.err.println("Неверный формат строки!");
}
}
// long data = Long.valueOf(line);
// int change = (int) data;
// long [] y = new long [change];
int period = 24;
int m = 5;
long[] y = new long[144];
try {
Scanner scanner = new Scanner(new File(path+"\\"+name+".txt"));
int i = 0;
while (scanner.hasNextLong()) {
y[i] = scanner.nextLong();
i++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
double sum_origin = 0;
int k=0;
do {
sum_origin = sum_origin + y[k];
k++;
} while (k<24);
//searching for alpha
double alpha = 0.01;
double a = 0.01;
double x=sum_origin;
double q;
do {
double beta = 0.3;
double gamma = 0.3;
double[] prediction = HoltWinters.forecast(y, a, beta, gamma,
period, m);
double sum_pre = sum(prediction);
q = sum_origin - sum_pre;
if (q<=x) {
x=q;
alpha = a;
}
a = a +0.01;
} while (a<0.99);
//searching for beta
double beta = 0.01;
double b = 0.01;
double x1=1000000;
double q1;
do {
double gamma = 0.3;
double[] prediction = HoltWinters.forecast(y, alpha, b, gamma,
period, m);
double sum_pre = sum(prediction);
q1 = sum_origin - sum_pre;
if (q1<=x1) {
x1=q1;
beta = b;
}
b = b +0.01;
} while (b<0.99);
//searching for gamma
double gamma = 0.01;
double g = 0.01;
double x2=1000000;
double q2;
do {
double[] prediction = HoltWinters.forecast(y, alpha, beta, g,
period, m);
double sum_pre = sum(prediction);
q2 = sum_origin - sum_pre;
if (q2<=x2) {
x2=q2;
gamma = g;
}
g = g +0.01;
} while (g<0.99);
// System.out.println(alpha);
// System.out.println(beta);
// System.out.println(gamma);
double[] prediction = HoltWinters.forecast(y, alpha, beta, gamma,
period, m);
for(int i = period; i <= prediction.length - 1; i++) {
System.out.println(prediction[i] + " ");
}
br.close();
File flt = new File("C:\\Users\\Jane\\Desktop\\"+name+"_prediction.txt");
PrintWriter out = new PrintWriter(new BufferedWriter(
new FileWriter(flt)));
for(int i = period; i <= prediction.length - 1; i++) {
out.println(prediction[i] + " ");
}
out.flush();
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}
答案 0 :(得分:1)
以下是我认为您尝试从该计划中做的事情:
JTextField
)获取一些输入,并且该输入应该是文件名如果我认为您想要的是正确的,那么您也可以通过使用按钮的点击事件来实现
您可以在类NewJFrame
中实现该接口,并在JTextField
中验证actionPerformed()
值,并在其他方法中委托实际逻辑,而不是使用匿名内部类作为按钮操作侦听器。
仅将main方法用作程序的入口点。与initComponents()
类似,您可以创建执行业务逻辑,读取文件和创建对用户操作的响应的方法。这样程序将易于阅读和维护。
此外,您在new NewJFrame().jTextField1ActionPerformed(null);
中使用的main()
没有意义。您应该只在程序中初始化JFrame一次。我认为你应该在你的main()
方法中只写一个语句
new JFrame();
这将启动您的程序,然后您的程序必须由用户操作驱动。
我希望这会有所帮助
答案 1 :(得分:-1)
错误在于:
BufferedReader br = new BufferedReader (
new InputStreamReader(
new FileInputStream( file ), "UTF-8"
)
);
file是无法从静态方法访问的实例变量。您必须在文件变量之前加上static关键字。
static File file;
而不是
File file;