работая над презентацией для школы, я кодировал java-часть, кто-то еще делал питон, которого я не знаю.
У меня есть Java-приложение, которое использует JavaFX для открытия окна, запрашивающего информацию, которое затем должно собирать информацию и передавать ее для использования в программе на Python.
Основная идея заключается в том, что программа должна взять номер телефона, поставщика и тип сообщения, которое они хотят, и отправить его им.
Я не знаю, как запустить программу на Python через Java, и как передавать данные при этом.
Java:
package showcase.program;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
//import com.google.common.primitives.Ints;
public class ShowcaseProgram extends Application {
String PhoneNum;
String CarrierNumStr;
String ChoiceStr;
final Button button = new Button ("Submit");
final Label notification = new Label ();
final Label notification2 = new Label ();
@Override
public void start(Stage stage) {
Scene scene = new Scene(new Group(), 500, 500);
final ComboBox CarriersCombo = new ComboBox();
CarriersCombo.getItems().addAll(
"Verison",
"Sprint",
"T-Mobile",
"AT&T",
"MetroPCS",
"Boost Mobile",
"Cricket Wireless",
"Staight Talk",
"Virgin Mobile"
);
final ComboBox ChoiceCombo = new ComboBox();
ChoiceCombo.getItems().addAll(
"Insult",
"Compliment"
);
TextField PhoneNumIn = new TextField ();
PhoneNumIn.setText("Label");
PhoneNumIn.clear();
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
int count = 0;
if (ChoiceCombo.getValue() == null){
notification.setText("You have not selected : insult or compliment");
count++;
}
if (CarriersCombo.getValue() == null){
notification.setText("You have not selected a Carrier!");
count++;
}
if (PhoneNumIn.getText() == null){
notification.setText("You have not entered a Phone number");
count++;
}
if(PhoneNumIn.getText() == null){ //try to get google api imported properly for this to use Ints.tryParse()
notification.setText("Please make sure your phone number is numeric only.");
count++;
}
System.out.println(count);
if (count == 0){
PhoneNum = PhoneNumIn.getText();
CarrierNumStr = CarriersCombo.getValue().toString();
ChoiceStr = ChoiceCombo.getValue().toString();
PhoneNumIn.setText(null);
CarriersCombo.setValue(null);
ChoiceCombo.setValue(null);
notification.setText("Thank you");
} else {
notification2.setText("The information you inputed is incorrect, please try again.");
}
}
});
GridPane grid = new GridPane();
grid.setVgap(4);
grid.setHgap(10);
grid.setPadding(new Insets(5, 5, 5, 5));
grid.add(new Label("Phone Number :"), 0, 0);
grid.add(PhoneNumIn, 1, 0);
grid.add(new Label("Carrier :"),0 , 2);
grid.add(CarriersCombo, 1 , 2);
grid.add(new Label("Choice :"), 0, 3);
grid.add(ChoiceCombo, 1 , 3);
grid.add(button, 1, 4);
grid.add(notification, 1, 5);
grid.add(notification2, 1, 6);
Group root = (Group) scene.getRoot();
root.getChildren().add(grid);
stage.setScene(scene);
stage.show();
}
public static void launchPy(String PhoneNum, int Carrier, String Msg){
}
public static int CarrierToInt(String Carr){
int x;
if (Carr == "Verison"){
x = 7;
} else if (Carr == "Sprint"){
x = 5;
} else if(Carr == "T-Mobile") {
x = 6;
} else if(Carr == "AT&T") {
x = 0;
} else if(Carr == "MetroPCS") {
x = 3;
} else if(Carr == "Boost Mobile") {
x = 1;
} else if(Carr == "Cricket Wireless") {
x = 2;
} else if(Carr == "Straight Talk") {
x = 4;
} else if(Carr == "Virgin Mobile") {
x = 8;
}
else {
x = 0;
}
return x;
}
public static String Message(String ChoiceStr){
int RNG;
RNG = (int )(Math.random() * 1 + 3);
String msg = "";
String[] insults = new String[5];
String[] compliments = new String[5];
insults[1] = "Fart You";//preset insults and compliments go here
insults[2] = "";
insults[3] = "you stink!";
compliments[1] = "Nice Hair";
if (ChoiceStr == "Insult"){
msg = insults[RNG];
} else {
msg = compliments[RNG];
}
return msg;
}
public static void main(String[] args) {
while(true) {
launch(args);
}
}
}
Программа python настроена на прием трех входов, как указано в методе launchPy.
в идеале это было бы что-то вроде (псевдо)
public static void launchPy(String PhoneNum, int Carrier, String Msg){
string pythonLocation = "/home/...";
//run python(pythonLocation, PhoneNum, Carrier, Msg)
}
но я не знаю, как это сделать.
Спасибо!