gpt4 book ai didi

java - 从按钮调用java类

转载 作者:行者123 更新时间:2023-12-01 11:39:38 25 4
gpt4 key购买 nike

我有一个 java 类,可以将 csv 转换为 shapefile,它工作得很好...现在我想创建一个带有可以执行此操作的按钮的 java 应用程序...所以我必须将按钮与 csv2shape 类相关联.. .ana我真的不知道该怎么做..这里是我的代码的一部分,解释了我尝试做的事情,它不起作用

               public class GestionTournee {

private JFrame frame;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GestionTournee window = new GestionTournee();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the application.
*/
public GestionTournee() {
initialize();
}

/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Button button = new Button("New button");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new Thread(new Runnable() {
public void run() {
new Csv2shape();
}
}).start();
}
});
frame.getContentPane().add(button, BorderLayout.NORTH);
}

}

还有 csv 转换器的代码

        public class Csv2shape {


public static void main(String[] args) throws Exception {
// Set cross-platform look & feel for compatability
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());

File file = JFileDataStoreChooser.showOpenFile("csv", null);
if (file == null) {
return;
}
/*
* We use the DataUtilities class to create a FeatureType that will describe the data in our
* shapefile.
*
* See also the createFeatureType method below for another, more flexible approach.
*/
final SimpleFeatureType TYPE = DataUtilities.createType( "Location",
"the_geom:Point:srid=4326," + // <- the geometry attribute: Point type
"Date:String," + // <- a String attribute
"NombreAr:Double," + // <- a String attribute
"vitesse:String," + // <- a String attribute
"distance:Double" // a number attribute


);
System.out.println("TYPE:"+TYPE);

/* A list to collect features as we create them.
*/
List<SimpleFeature> features = new ArrayList<SimpleFeature>();

/*
* GeometryFactory will be used to create the geometry attribute of each feature,
* using a Point object for the location.
*/
GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();

SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE);

BufferedReader reader = new BufferedReader(new FileReader(file));
try {
/* First line of the data file is the header */
String line = reader.readLine();
System.out.println("Header: " + line);
boolean foundAnyRowHigherThan4 = false;
double s =0;
boolean foundAnyRowHigherThan5 = false;
boolean checker = true;
double Longitude2 = 0;
double Latitude2 = 0;
double Dis = 0;
double dis = 0;
for (line = reader.readLine(); line != null; line = reader.readLine()) {
if (line.trim().length() > 0) { // skip blank lines
{
if(checker){
String currentTokens[] = line.split("\\,");
String currentName1 = currentTokens[0].trim();
String currentName2 = currentTokens[1].trim();
Longitude2 = Double.parseDouble(currentTokens[2]);
Latitude2 = Double.parseDouble(currentTokens[3]);
checker = false ;
continue;
}




String tokens[] = line.split("\\,");
String name1 = tokens[0].trim();
String name2 = tokens[1].trim();
double longitude = Double.parseDouble(tokens[2]);
double latitude = Double.parseDouble(tokens[3]);



String speedString = tokens[5].trim();
double dist = Double.parseDouble(tokens[4]);
float speedFloat = Float.parseFloat(speedString);



if(foundAnyRowHigherThan5 || speedFloat > 5.0) {
// a partir de ce point on ajoutera touts les points ,
if(!foundAnyRowHigherThan5) {
foundAnyRowHigherThan5 = true;
}


if(foundAnyRowHigherThan4 || speedFloat < 1.0) {
// a partir de ce point on ajoutera touts les points d'arrets,
if(speedFloat <0.1)
s = s+1 ;
else
foundAnyRowHigherThan4 = true;
}


/* Longitude (= x coord) first ! */
Point point = geometryFactory.createPoint(new Coordinate(longitude, latitude));
featureBuilder.add(point);

double earthRadius = 6371; //km pour la changer en metres faut ajouter 000 a la fin
double dLat = Math.toRadians(latitude-Latitude2);
double dLng = Math.toRadians(longitude-Longitude2);
double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(Math.toRadians(Latitude2)) * Math.cos(Math.toRadians(latitude)) *
Math.sin(dLng/2) * Math.sin(dLng/2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
double d = (float) (earthRadius * c);

dis = d+dis;




featureBuilder.add(name1);
featureBuilder.add(s);

featureBuilder.add(speedString);
featureBuilder.add(dis);
SimpleFeature feature = featureBuilder.buildFeature(null);

features.add(feature);

Longitude2 = longitude;
Latitude2 = latitude;

}
}
}

}
}
finally {
reader.close();
}

如有任何帮助,我将不胜感激......谢谢

最佳答案

基本上正确连接了事件处理程序(稍后会详细介绍)。

主要问题是您的 CSV 逻辑位于 Csv2shapemain 方法中。您没有在事件处理程序中调用此方法,因为您只是在执行 new Csv2shape();

要解决这个问题,您只需执行以下操作:Csv2shape.main(null);,这将调用您的 main 方法,该方法具有但是,从逻辑上来说,不建议这样做(您的代码中最多应该有 1 个 main 方法,并且您应该避免自己调用它)。因此,解决您的问题,我建议您将 main 中的逻辑移动到其他方法中,例如 private void createCSV()。在 Csv2shape 构造函数中,您调用此方法:public Csv2shape() { this.createCSV();}

上述应该触发您需要的调用。

正如我在一开始所说的,事件处理程序的创建大部分是正确的。问题是您正在事件调度线程 (EDT) 上处理文件,该线程是处理所有 UI 操作的线程,因此,在此线程上执行繁重的非 UI 操作将对您的用户体验产生不利影响。

要解决此问题,只需在新线程中启动 Csv2shape 构造函数即可。这将减轻 EDT 的负担:

因此,将 new Csv2shape(); 替换为:

new Thread(new Runnable()  {
@Override
public void run() {
new Csv2shape();
}
}).start();

编辑:

如果您遵循我的示例,这就是 Csv2shape 类的外观:

public class Csv2shape {

public Csv2shape() {
try {
this.createCSV();
}
catch(Exception e) {
e.printStackTrace(); //Or someother means of logging.
}

}
private void createCSV() throws Exception {
// Set cross-platform look & feel for compatability
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());

File file = JFileDataStoreChooser.showOpenFile("csv", null);
if (file == null) {
return;
}
/*
* We use the DataUtilities class to create a FeatureType that will describe the data in our
* shapefile.
*
* See also the createFeatureType method below for another, more flexible approach.
*/
final SimpleFeatureType TYPE = DataUtilities.createType( "Location",
"the_geom:Point:srid=4326," + // <- the geometry attribute: Point type
"Date:String," + // <- a String attribute
"NombreAr:Double," + // <- a String attribute
"vitesse:String," + // <- a String attribute
"distance:Double" // a number attribute


);
System.out.println("TYPE:"+TYPE);

/* A list to collect features as we create them.
*/
List<SimpleFeature> features = new ArrayList<SimpleFeature>();

/*
* GeometryFactory will be used to create the geometry attribute of each feature,
* using a Point object for the location.
*/
GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();

SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE);

BufferedReader reader = new BufferedReader(new FileReader(file));
try {
/* First line of the data file is the header */
String line = reader.readLine();
System.out.println("Header: " + line);
boolean foundAnyRowHigherThan4 = false;
double s =0;
boolean foundAnyRowHigherThan5 = false;
boolean checker = true;
double Longitude2 = 0;
double Latitude2 = 0;
double Dis = 0;
double dis = 0;
for (line = reader.readLine(); line != null; line = reader.readLine()) {
if (line.trim().length() > 0) { // skip blank lines
{
if(checker){
String currentTokens[] = line.split("\\,");
String currentName1 = currentTokens[0].trim();
String currentName2 = currentTokens[1].trim();
Longitude2 = Double.parseDouble(currentTokens[2]);
Latitude2 = Double.parseDouble(currentTokens[3]);
checker = false ;
continue;
}




String tokens[] = line.split("\\,");
String name1 = tokens[0].trim();
String name2 = tokens[1].trim();
double longitude = Double.parseDouble(tokens[2]);
double latitude = Double.parseDouble(tokens[3]);



String speedString = tokens[5].trim();
double dist = Double.parseDouble(tokens[4]);
float speedFloat = Float.parseFloat(speedString);



if(foundAnyRowHigherThan5 || speedFloat > 5.0) {
// a partir de ce point on ajoutera touts les points ,
if(!foundAnyRowHigherThan5) {
foundAnyRowHigherThan5 = true;
}


if(foundAnyRowHigherThan4 || speedFloat < 1.0) {
// a partir de ce point on ajoutera touts les points d'arrets,
if(speedFloat <0.1)
s = s+1 ;
else
foundAnyRowHigherThan4 = true;
}


/* Longitude (= x coord) first ! */
Point point = geometryFactory.createPoint(new Coordinate(longitude, latitude));
featureBuilder.add(point);

double earthRadius = 6371; //km pour la changer en metres faut ajouter 000 a la fin
double dLat = Math.toRadians(latitude-Latitude2);
double dLng = Math.toRadians(longitude-Longitude2);
double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(Math.toRadians(Latitude2)) * Math.cos(Math.toRadians(latitude)) *
Math.sin(dLng/2) * Math.sin(dLng/2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
double d = (float) (earthRadius * c);

dis = d+dis;




featureBuilder.add(name1);
featureBuilder.add(s);

featureBuilder.add(speedString);
featureBuilder.add(dis);
SimpleFeature feature = featureBuilder.buildFeature(null);

features.add(feature);

Longitude2 = longitude;
Latitude2 = latitude;

}
}
}

}
}
finally {
reader.close();
}
}

关于java - 从按钮调用java类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29647633/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com