我正在使用netbeans中的javafx开发一个项目。我在不同的包中制作了很多课程。现在我想在不同的包中连接2个类。怎么做 ?以下代码是主要类:
package createaccount;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
public class CreateAccount extends Application {
@Override
public void start(Stage stage) {
// Use a border pane as the root for scene
BorderPane border = new BorderPane();
border.setTop(addHBox());
border.setLeft(addVBox());
border.setCenter(addVBox1());
border.setBottom(addHBox1());
Scene scene = new Scene(border,700,400,Color.OLDLACE);
stage.setScene(scene);
stage.setTitle("Create Account");
stage.setResizable(false);
scene.getStylesheets().add
(CreateAccount.class.getResource("CreateAccount.css").toExternalForm());
stage.show();
}
private HBox addHBox() {
HBox hbox = new HBox();
hbox.setPadding(new Insets(15, 12, 15, 230));
hbox.setSpacing(10); // Gap between nodes
Label lb1=new Label("CREATE YOUR NEW ACCOUNT");
lb1.setAlignment(Pos.CENTER);
lb1.setFont(Font.font("Trebuchet MS",FontWeight.BOLD,20));
hbox.getChildren().addAll(lb1);
return hbox;
}
private VBox addVBox() {
VBox vbox = new VBox();
vbox.setPadding(new Insets(50,10,20,100)); // Set all sides to 10
vbox.setSpacing(10); // Gap between nodes
Label lb1=new Label("Full Name ");
lb1.setFont(Font.font("Calibri",FontWeight.BOLD,18));
lb1.setPrefSize(150, 30);
lb1.setTextFill(Color.WHITE);
Label lb2=new Label("User Name ");
lb2.setFont(Font.font("Calibri",FontWeight.BOLD,18));
lb2.setPrefSize(150, 30);
lb2.setTextFill(Color.WHITE);
Label lb3=new Label("Password ");
lb3.setFont(Font.font("Calibri",FontWeight.BOLD,18));
lb3.setPrefSize(150, 30);
lb3.setTextFill(Color.WHITE);
Label lb4=new Label("Subject ");
lb4.setFont(Font.font("Calibri",FontWeight.BOLD,18));
lb4.setPrefSize(150, 30);
lb4.setTextFill(Color.WHITE);
Label lb5=new Label("Semester ");
lb5.setFont(Font.font("Calibri",FontWeight.BOLD,18));
lb5.setPrefSize(150, 30);
lb5.setTextFill(Color.WHITE);
vbox.getChildren().addAll(lb1,lb2,lb3,lb4,lb5);
return vbox;
}
private VBox addVBox1() {
VBox vbox = new VBox();
vbox.setPadding(new Insets(50,200,20,50)); // Set all sides to 10
vbox.setSpacing(10); // Gap between nodes
TextField t1=new TextField();
t1.setPrefSize(150,30);
TextField t2=new TextField();
t2.setPrefSize(150,30);
PasswordField t3=new PasswordField();
t3.setPrefSize(150,30);
PasswordField t4=new PasswordField();
t4.setPrefSize(150,30);
ObservableList<String> options2 =
FXCollections.observableArrayList(
"Semester 1","Semester 2","Semester 3","Semester 4",
"Semester 5","Semester 6");
final ComboBox comboBox2 = new ComboBox(options2);
comboBox2.setPrefSize(200,30);
vbox.getChildren().addAll(t1,t2,t3,t4,comboBox2);
return vbox;
}
private HBox addHBox1() {
HBox hbox = new HBox();
hbox.setPadding(new Insets(15, 12, 15, 200));
hbox.setSpacing(10);
Button b1=new Button("SUBMIT");
b1.setFont(Font.font("Calibri",FontWeight.BOLD,20));
b1.setPrefSize(130,30);
b1.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
}
});
Button b2=new Button("CANCEL");
b2.setFont(Font.font("Calibri",FontWeight.BOLD,20));
b2.setPrefSize(130,30);
hbox.getChildren().addAll(b1,b2);
return hbox;
}
/**
* The main() method is ignored in correctly deployed JavaFX application.
* main() serves only as fallback in case the application can not be
* launched through deployment artifacts, e.g., in IDEs with limited FX
* support. NetBeans ignores main().
*
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
以下2个类用于连接数据库:
package javasql;
import com.mysql.jdbc.Driver;
import java.sql.*;
public class Connect {
public Connect() throws SQLException{
makeConnection();
}
private Connection koneksi;
public Connection makeConnection() throws SQLException {
if (koneksi == null) {
new Driver();
// buat koneksi
koneksi = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/mysql","root","virus");
}
return koneksi;
}
public static void main(String args[]) {
try {
Connect c = new Connect();
System.out.println("Connection established");
}
catch (SQLException e) {
e.printStackTrace();
System.err.println("Connection Failure");
}
}
}
package javasql;
import java.sql.*;
public class SqlStatement {
private Statement statement;
public SqlStatement() throws SQLException{
makeStatement();
}
public Statement makeStatement() throws SQLException{
Connect c = new Connect();
Connection conn = c.makeConnection();
statement = conn.createStatement();
return statement;
}
public void insert(String name,int npm)throws SQLException{
statement.execute("insert into Student values(\""+name+"\","+npm+");");
}
public static void main(String arg[]){
try {
SqlStatement s = new SqlStatement();
s.insert("Ferdi",1);
s.insert("Anca",2);
System.out.println("Success");
}
catch(SQLException e){
System.out.println("Failed");
e.printStackTrace();
}
}
}
现在你看到了代码。我想要的是从主代码中读取值并通过数据库代码存储它。我只是想知道如何使这些代码相互链接或连接。有人请帮忙...
答案 0 :(得分:4)
两个包都在同一个项目中吗?如果是这样,那么您应该能够很好地链接它们(可以从任何地方访问public
关键字)
如果没有,则将项目导出为.jar文件,并将其添加到其他项目的依赖项中,然后您应该可以正常导入它。
编辑:(发送给提问者的私人电子邮件的内容)
您可以将包的内容移动到其他项目,也可以按照以下步骤链接它们:
答案 1 :(得分:0)
从Project中创建一个jar
文件,并将该jar文件作为依赖项添加到类路径中,然后导入该包并使用所需的类。