我试图在java中制作一个gui。它没有出现

时间:2014-11-29 02:21:02

标签: java user-interface

我是一个非常新手的java编码器,我想制作一个gui向我的母亲展示为什么我应该通过导入扫描仪选项提交合理的数字来显示有关事实和数字的工作。我想把它作为一个精算程序,而不是打开eclipse来展示她。我试过运行它,它不会打开。请告诉我我做错了什么。谢谢。我也有所有的导入,但没有复制和粘贴到这里。

JFrame program;
JPanel p;

public job()
{

    gui();

}

public void gui()
{

    program = new JFrame("Job");
    program.setVisible(true);
    program.setSize(1000,800);
    program.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    p = new JPanel();
    p.setBackground(Color.RED);

    program.add(p);


}

public static void main(String[] args) {

    Scanner weeks = new Scanner(System.in);
    System.out.println("How many weeks did you work in the year?");
    int week = weeks.nextInt();

    Scanner payhourly = new Scanner(System.in);
    System.out.println("How much does your job pay per hour?");
    double pay = payhourly.nextDouble();

    Scanner hoursworkedweekly = new Scanner(System.in);
    System.out.println("How many hours did you work per week on average?");
    int hoursworked = hoursworkedweekly.nextInt();

    Scanner bankenter = new Scanner(System.in);
    System.out.println("What is the percent of the money earned going to your bank account?");
    int bank = bankenter.nextInt();

    Scanner personalenter = new Scanner(System.in);
    System.out.println("What is the percent of the money earned going to your own personal use?");
    int personal = personalenter.nextInt();

    double totalpay = (pay * hoursworked) * week;
    double banktotal = ((double)bank / 100) * totalpay;
    double personalusetotal = ((double)personal / 100) * totalpay;

    System.out.println("While working " + week + " weeks");
    System.out.println("");
    System.out.println("You earned a total $" + totalpay);

    System.out.println("$"+ banktotal +" will go into your account .");

    System.out.println("$"+ personalusetotal + " will go to your own personal use.");



    }





}

1 个答案:

答案 0 :(得分:1)

您需要在main方法中调用gui()方法。但我不明白为什么你甚至不需要JFrame,因为你没有写任何文字。

编辑:我强烈建议使用JavaFX for GUI。这是您的程序的重写版本:

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;

public class GUI extends Application{

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

    @Override
    public void start(Stage stage) throws Exception {
        FlowPane root = new FlowPane();
        root.setVgap(25);
        root.setPadding(new Insets(50, 50, 50, 50));
        root.setPrefSize(500, 500);

        TextField weeks = new TextField();
        weeks.setPromptText("How many weeks did you work in the year?");
        weeks.setPrefWidth(250);
        TextField pay = new TextField();
        pay.setPromptText("How much does your job pay per hour?");
        pay.setPrefWidth(250);
        TextField hours = new TextField();
        hours.setPromptText("How many hours did you work per week on average?");
        hours.setPrefWidth(250);
        TextField bank = new TextField();
        bank.setPromptText("What is the percent of the money earned going to your bank account?");
        bank.setPrefWidth(250);
        TextField personal = new TextField();
        personal.setPromptText("What is the percent of the money earned going to your own personal use?");
        personal.setPrefWidth(250);

        Button submit = new Button("Submit");
        submit.setOnAction(event ->{
            StringBuilder message = new StringBuilder();
            message.append("While working " + weeks.getText() + " weeks\n");
            message.append("You earned a total $" + pay.getText() + "\n");
            message.append("$"+ bank.getText() +" will go into your account. \n");
            message.append("$"+ personal.getText() + " will go to your own personal use.\n");
            Label messageLabel = new Label(message.toString());
            root.getChildren().add(messageLabel);
        });

        root.getChildren().addAll(weeks, pay, hours, bank, personal, submit);

        stage.setScene(new Scene(root));
        stage.show();
    }

}