我是一个非常新手的Java编码器,我想做个gui来向妈妈展示为什么我应该通过使用Import Scanner选项提交合理的数字来得到一份显示事实和数字的工作。我想让它成为一个实际的程序,而不是为了向她展示日食。我尝试运行它,它将无法打开。请告诉我我做错了。谢谢。我也有所有的进口,但没有复制粘贴到这里。

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.");



    }





}

最佳答案

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

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

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

}

关于java - 我试图用Java做一个GUI。它没有出现,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27198205/

10-12 04:36
查看更多