【问题标题】:How to connect a class in another package to a separate package如何将另一个包中的类连接到单独的包
【发布时间】:2014-03-01 10:25:51
【问题描述】:

我正在 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();
    }
}
}

现在您可以看到代码了。我想要的是从主代码中读取值并通过数据库代码存储它。我只想知道如何使这些代码相互关联或连接。有人请帮忙...

【问题讨论】:

标签: java mysql javafx javafx-2


【解决方案1】:

两个包是否在同一个项目中?如果是这样,那么您应该可以很好地链接它们(public 关键字可以从任何地方访问)

如果没有,则将项目导出为 .jar 文件,并将其添加到其他项目的依赖项中,然后您应该可以正常导入它。

编辑:(发送给提问者的私人电子邮件的内容)

您可以将包的内容移动到您的其他项目,或者您可以按照以下步骤链接它们:

  1. 右键单击项目,然后单击“属性”
  2. 点击“库”标签(左侧)
  3. 点击“添加项目”按钮
  4. 选择它所在的项目
  5. 按“添加项目 JAR 文件”,该窗口将关闭
  6. 按“确定”
  7. 完成,然后您可以使用 import javasql.Connect 导入所需的类。

【讨论】:

  • 是的,它们属于同一个项目,但位于不同的包中。你能说一下如何创建.jar文件并导出它吗?
  • 同一个项目的意思是两个包都在同一个目录下。你用什么IDE?我主要使用eclipse,eclipse创建项目,相互独立。 (例如 Sudoku 程序和 NoteManager 程序)。如果两个包都在同一个程序中使用,那么它应该就像import 语句一样简单。
  • 两个包在同一个目录下。在我的系统中,它位于 E:/Projects 中。我使用 NetBeans IDE。
  • NetBeans 有项目,如果两个包在同一个项目下,那么你可以简单地import 它。输入import package.name.ClassNameThatYouWant,你应该很可爱。否则,我将向您展示如何导出 jar 文件。
  • 在您的情况下,在CreateAccount 中,您只需输入import javasql.DesiredClass; 即可。
【解决方案2】:

从项目中创建一个 jar 文件并将该 jar 文件添加为依赖项,即在您的类路径中,然后导入该包并使用所需的类。

【讨论】:

    猜你喜欢
    • 2014-05-04
    • 1970-01-01
    • 2021-09-22
    • 2011-11-09
    • 2019-11-15
    • 1970-01-01
    • 1970-01-01
    • 2012-02-22
    • 1970-01-01
    相关资源
    最近更新 更多