我想在 Java 中创建一个小应用程序——一个按钮和一个 JTextField。此外,我使用从 J48 算法生成“树”的代码(我在寻找 WEKA totorial 时在网上找到代码)。可变表单文本字段应该在此代码中使用(实现 J48)。我创建了这样的东西,但它不起作用:
package visio;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.FileReader;
import javax.swing.JFrame;
import weka.classifiers.trees.J48;
import weka.core.Instances;
import weka.gui.treevisualizer.PlaceNode2;
import weka.gui.treevisualizer.TreeVisualizer;
public class Visio extends JFrame implements ActionListener {
int temp;
JTextField pole;
JButton button = new JButton("bb");
public Visio() {
setSize(400, 400);
setTitle("okno");
setLayout(null);
button.setBounds(50, 50, 100, 20);
add(button);
button.addActionListener(this);
pole = new JTextField("");
pole.setBounds(20, 70, 150, 20);
add(pole);
}
public static void main(String[] args) throws Exception {
Visio okienko = new Visio();
okienko.setDefaultCloseOperation(EXIT_ON_CLOSE);
okienko.setVisible(true);
// train classifier
J48 cls = new J48();
BufferedReader reader;
reader = new BufferedReader(new FileReader("E:/Desktop/weka.arff"));
Instances data = new Instances(reader);
reader.close();
/* Instances data = new Instances(new BufferedReader(new FileReader(args[0])));*/
data.setClassIndex(data.numAttributes() - temp);
cls.buildClassifier(data);
// display classifier
final javax.swing.JFrame jf =
new javax.swing.JFrame("Weka Classifier Tree Visualizer: J48");
jf.setSize(500, 400);
jf.getContentPane().setLayout(new BorderLayout());
TreeVisualizer tv = new TreeVisualizer(null,
cls.graph(),
new PlaceNode2());
jf.getContentPane().add(tv, BorderLayout.CENTER);
jf.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent e) {
jf.dispose();
}
});
jf.setVisible(true);
tv.fitToScreen();
}
@Override
public void actionPerformed(ActionEvent e) {
temp = Integer.parseInt(pole.getText());
}
}
帮帮我。我有一个错误:不能从静态上下文中引用非静态变量