如何从另一个类调用其他类的函数

时间:2022-11-19 11:38:12

I am developing a project in javafx using NetBeans IDE. Now I want to call a method of a class in some other package from a class in another package. Both packages are under the same project. Code of my main class is below :

我正在使用NetBeans IDE在javafx中开发一个项目。现在我想从另一个包中的类调用其他包中的类的方法。两个包都在同一个项目下。我的主要课程代码如下:

package welcomepage;

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.control.Tooltip;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
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;
import help.*;

public class WelcomePage extends Application {

@Override
public void start(Stage stage) {


    BorderPane border = new BorderPane();

    border.setTop(addVBox());
    border.setLeft(addVBox1());

    Scene scene = new Scene(border,700,450);
    stage.setScene(scene);
    stage.setResizable(false);
    scene.getStylesheets().add
    (WelcomePage.class.getResource("WelcomePage.css").toExternalForm());
    stage.show();

}

private VBox addVBox() {

    VBox vbox = new VBox();
    vbox.setPadding(new Insets(5, 12, 5, 20));
    vbox.setSpacing(10);   // Gap between nodes

    Image image = new Image(getClass().getResourceAsStream("logo11.png"));
    Label lb1=new Label("    C - MARK AND ATTENDANCE CALCULATOR");
    lb1.setAlignment(Pos.CENTER);
    lb1.setFont(Font.font("Calibri",FontWeight.BOLD,28));
    lb1.setTextFill(Color.BLACK);
    lb1.setGraphic(new ImageView(image));

    vbox.getChildren().addAll(lb1);

    return vbox;
}

private VBox addVBox1()
{
    VBox vbox1=new VBox();
    vbox1.setPadding(new Insets(20, 2, 15, 20));
    vbox1.setSpacing(20);


    Button btnl2=new Button("SIGN IN");
    btnl2.setFont(Font.font("Calibri",FontWeight.BOLD,16));
    btnl2.setPrefSize(300,60);
    btnl2.setStyle(" -fx-base: #0066cc;");
    //Image imageOk = new Image(getClass().getResourceAsStream("icon22.png"));
    //btnl2.setGraphic(new ImageView(imageOk));

    final Tooltip tooltip2 = new Tooltip();
    tooltip2.setText("If you have an account,\nSign in here.");
    btnl2.setTooltip(tooltip2);

    btnl2.setOnAction(new EventHandler<ActionEvent>() {
     @Override
     public void handle(ActionEvent e) {
       signin();
     }
     });


    Button btnl4=new Button("HELP");
    btnl4.setFont(Font.font("Calibri",FontWeight.BOLD,16));
    btnl4.setPrefSize(300,60);
    btnl4.setStyle(" -fx-base: #0066cc;");

    final Tooltip tooltip4 = new Tooltip();
    tooltip4.setText("Get help content\nabout this software.");
    btnl4.setTooltip(tooltip4);

    btnl4.setOnAction(new EventHandler<ActionEvent>() {
     @Override
     public void handle(ActionEvent e) {

// I want to call the method of other class here.

     }
     });

    Button btnl5=new Button("ABOUT");
    btnl5.setFont(Font.font("Calibri",FontWeight.BOLD,16));
    btnl5.setPrefSize(300,60);
    btnl5.setStyle(" -fx-base: #0066cc;");

    final Tooltip tooltip5 = new Tooltip();
    tooltip5.setText("Know about\nthis software.");
    btnl5.setTooltip(tooltip5);

    btnl5.setOnAction(new EventHandler<ActionEvent>() {
     @Override
     public void handle(ActionEvent e) {
      about();
     }
     });

    Button btnl6=new Button("EXIT");
    btnl6.setFont(Font.font("Calibri",FontWeight.BOLD,16));
    btnl6.setPrefSize(300,60);
    btnl6.setStyle(" -fx-base: #0066cc;");

    final Tooltip tooltip6 = new Tooltip();
    tooltip6.setText("Exit if you had\nfinished your works.");
    btnl6.setTooltip(tooltip6);

    btnl6.setOnAction(new EventHandler<ActionEvent>() {
     @Override
     public void handle(ActionEvent e) {
      System.exit(0);
     }
     });

    vbox1.getChildren().addAll(btnl2,btnl4,btnl5,btnl6);

    return vbox1;
}

public void signin()
{
    Stage stage=new Stage();
    BorderPane border = new BorderPane();

    border.setTop(loginHBox1());
    border.setLeft(loginVBox1());
    border.setRight(loginVBox2());

    Scene scene = new Scene(border,700,450);
    stage.setScene(scene);
    stage.setResizable(false);
    scene.getStylesheets().add
    (Login.class.getResource("Login.css").toExternalForm());
    stage.show();

}

private HBox loginHBox1() {

    HBox hbox = new HBox();
    hbox.setPadding(new Insets(15, 12, 10, 180));
    hbox.setSpacing(10);   

    Label lb1=new Label("LOG IN OR CREATE NEW ACCOUNT");
    lb1.setAlignment(Pos.CENTER);
    lb1.setFont(Font.font("Calibri",FontWeight.BOLD,26));
    lb1.setTextFill(Color.BLACK);

    hbox.getChildren().addAll(lb1);

    return hbox;
}

private VBox loginVBox1() {

    VBox hbox = new VBox();
    hbox.setPadding(new Insets(20,30,15,50)); 
    hbox.setSpacing(10);     

    Label lb3=new Label("LOG  IN");
    lb3.setAlignment(Pos.CENTER);
    lb3.setFont(Font.font("Calibri",FontWeight.BOLD,24));
    lb3.setTextFill(Color.BLACK);

    Label lb1=new Label("Username");
    lb1.setAlignment(Pos.CENTER);
    lb1.setFont(Font.font("Calibri",FontWeight.BOLD,16));
    lb1.setTextFill(Color.BLACK);

    TextField t1=new TextField();
    t1.setPrefSize(150,30);

    Label lb2=new Label("Password");
    lb2.setAlignment(Pos.CENTER);
    lb2.setFont(Font.font("Calibri",FontWeight.BOLD,16));
    lb2.setTextFill(Color.BLACK);

    PasswordField pw1=new PasswordField();
    pw1.setPrefSize(150,30);

    Button b1=new Button("LOG IN");
    b1.setFont(Font.font("Calibri",FontWeight.BOLD,16));
    b1.setPrefSize(80,5);

    hbox.getChildren().addAll(lb3,lb1,t1,lb2,pw1,b1);

    return hbox;
}

private VBox loginVBox2()
{
   VBox hbox1 = new VBox();
   hbox1.setPadding(new Insets(15, 50, 15, 10));
   hbox1.setSpacing(10);

   Label lb4=new Label("CREATE  NEW  ACCOUNT");
   lb4.setFont(Font.font("Calibri",FontWeight.BOLD,24));
   lb4.setPrefSize(250,30);
   lb4.setTextFill(Color.BLACK);

   Label lb1=new Label("Full Name ");
   lb1.setFont(Font.font("Calibri",FontWeight.BOLD,18));
   lb1.setPrefSize(100, 30);
   lb1.setTextFill(Color.BLACK);

   TextField t1=new TextField();
   t1.setPrefSize(50,30);

   Label lb2=new Label("User Name ");
   lb2.setFont(Font.font("Calibri",FontWeight.BOLD,18));
   lb2.setPrefSize(150, 30);
   lb2.setTextFill(Color.BLACK);

   TextField t2=new TextField();
   t2.setPrefSize(100,30);

   Label lb3=new Label("Password ");
   lb3.setFont(Font.font("Calibri",FontWeight.BOLD,18));
   lb3.setPrefSize(150, 30);
   lb3.setTextFill(Color.BLACK);

   PasswordField t3=new PasswordField();
   t3.setPrefSize(100,30);

   Label lb5=new Label("Gender ");
   lb5.setFont(Font.font("Calibri",FontWeight.BOLD,18));
   lb5.setPrefSize(150, 30);
   lb5.setTextFill(Color.BLACK);

   ObservableList<String> options2 = 
   FXCollections.observableArrayList(
   "Male","Female");
   final ComboBox comboBox2 = new ComboBox(options2);
   comboBox2.setPrefSize(250,30);

   Button btn1=new Button("CREATE");
   btn1.setFont(Font.font("Calibri",FontWeight.BOLD,18));
   btn1.setPrefSize(100,30);


   hbox1.getChildren().addAll(lb4,lb1,t1,lb2,t2,lb3,t3,lb5,comboBox2,btn1);
   return hbox1;
}


  public void about() {


    Stage stage=new Stage();
    BorderPane border = new BorderPane();

    HBox hbox11 = aboutHBox1();
    border.setTop(hbox11);
    border.setCenter(aboutVBox1());
    border.setBottom(aboutHBox2());

    Scene scene = new Scene(border,700,450);
    stage.setScene(scene);
    stage.setResizable(false);
    scene.getStylesheets().add
    (About.class.getResource("About.css").toExternalForm());
    stage.show();
}

private HBox aboutHBox1() {

    HBox hbox11 = new HBox();
    hbox11.setPadding(new Insets(15, 12, 15, 320));
    hbox11.setSpacing(10);   
    hbox11.setStyle("-fx-background-color: #336699;");

    Label lb1=new Label("ABOUT");
    lb1.setAlignment(Pos.CENTER);
    lb1.setFont(Font.font("Trebuchet MS",FontWeight.BOLD,20));

    hbox11.getChildren().addAll(lb1);

    return hbox11;
}

private VBox aboutVBox1() {

    VBox vbox11 = new VBox();
    vbox11.setPadding(new Insets(20)); 
    vbox11.setSpacing(5);     

    Label l1=new Label("C - MARK AND ATTENDANCE CALCULATOR");
    l1.setFont(Font.font("Calibri",FontWeight.BOLD,20));
    l1.setTextFill(Color.GREEN);

    Label l2=new Label("\nSoftware to calculate C-mark and attendance easily.\n"
            + "Supported in Windows XP or above.\n"
            + "Developed using Java.\n"
            + "Advantages : Simple user interface, Easy usage.\n\n"
            + "Developed by :\n"
            + "\t\t Adarsh P.S \n"
            + "\t\t Akhilnath A.R \n"
            + "\t\t Arjun P Das \n"
            + "\t\t Tomin Jacob ");
    l2.setFont(Font.font("Calibri",FontWeight.BOLD,18));
    l2.setTextFill(Color.GREEN);

    vbox11.getChildren().addAll(l1,l2);
    return vbox11;
}

private HBox aboutHBox2()
{
   HBox hbox12 = new HBox();
   hbox12.setPadding(new Insets(15, 12, 15, 300));

   Button btn1=new Button("BACK");
   btn1.setFont(Font.font("Calibri",FontWeight.BOLD,18));
   btn1.setPrefSize(100,40);

   btn1.setOnAction(new EventHandler<ActionEvent>() {
     @Override
     public void handle(ActionEvent e) {

     }
     });

   hbox12.getChildren().addAll(btn1);
    return hbox12;
}


public static void main(String[] args) {
    launch(args);
}

}

I commented the place from where I want to call the other class. The following is the code of the class that I want to get worked when the button is pressed:

我评论了我要打电话给另一个班级的地方。以下是按下按钮时我想要工作的类的代码:

package help;

import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
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 Help extends Application {

@Override
public void start(Stage stage) {

// Use a border pane as the root for scene
    BorderPane border = new BorderPane();

    HBox hbox = helpHBox1();
    border.setTop(hbox);
    border.setCenter(helpVBox1());
    border.setBottom(helpHBox2());

    Scene scene = new Scene(border,700,450);
    stage.setScene(scene);
    stage.setResizable(false);
    scene.getStylesheets().add
    (Help.class.getResource("Help.css").toExternalForm());
    stage.show();
}

private HBox helpHBox1() {

    HBox hbox = new HBox();
    hbox.setPadding(new Insets(15, 12, 15, 300));
    hbox.setSpacing(10);   // Gap between nodes
    hbox.setStyle("-fx-background-color: #336699;");

    Label lb1=new Label("HELP");
    lb1.setAlignment(Pos.CENTER);
    lb1.setFont(Font.font("Trebuchet MS",FontWeight.BOLD,20));

    hbox.getChildren().addAll(lb1);

    return hbox;
}

private VBox helpVBox1() {

    VBox vbox = new VBox();
    vbox.setPadding(new Insets(20)); // Set all sides to 10
    vbox.setSpacing(5);     // Gap between nodes

    Label l1=new Label("");
    l1.setFont(Font.font("Calibri",FontWeight.BOLD,20));
    l1.setTextFill(Color.GREEN);

    vbox.getChildren().addAll(l1);
    return vbox;
}

private HBox helpHBox2()
{
   HBox hbox1 = new HBox();
   hbox1.setPadding(new Insets(15, 12, 15, 300));

   Button btn1=new Button("BACK");
   btn1.setFont(Font.font("Calibri",FontWeight.BOLD,18));
   btn1.setPrefSize(100,40);

   hbox1.getChildren().addAll(btn1);
    return hbox1;
}

/**
 * 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 个解决方案

#1


2  

The below solution is based on ur question 'Now I want to call a method of a class in some other package from a class in another package.'Not according to the code

下面的解决方案基于你的问题'现在我想从另一个包中的类调用其他包中的类的方法。'根据代码

Class -1

 Package abc;
    Class A
    {
    public void demo()
    {
     ////////Code
    }
    }

Class -2

Package def;
import abc.A;
Class B
{
public static void main(String arg[])
{
A obj = new A();
obj.demo();   // Class A method is called from Class B which is in different package.
}
}

#2


0  

yo could instantiate a object of the other class and call the method you need, or follow the singleton pattern to call the method you need without create the mentioned object.

你可以实例化另一个类的对象并调用你需要的方法,或者按照单例模式调用你需要的方法而不创建提到的对象。

#1


2  

The below solution is based on ur question 'Now I want to call a method of a class in some other package from a class in another package.'Not according to the code

下面的解决方案基于你的问题'现在我想从另一个包中的类调用其他包中的类的方法。'根据代码

Class -1

 Package abc;
    Class A
    {
    public void demo()
    {
     ////////Code
    }
    }

Class -2

Package def;
import abc.A;
Class B
{
public static void main(String arg[])
{
A obj = new A();
obj.demo();   // Class A method is called from Class B which is in different package.
}
}

#2


0  

yo could instantiate a object of the other class and call the method you need, or follow the singleton pattern to call the method you need without create the mentioned object.

你可以实例化另一个类的对象并调用你需要的方法,或者按照单例模式调用你需要的方法而不创建提到的对象。