简体   繁体   English

我试图用Java做一个GUI。 它没有出现

[英]I tried to make a gui in java. It didnt show up

I am a very novice java coder and I wanted to make a gui to show my mother why I should get a job showing hard facts and numbers by me submitting reasonable numbers with the import scanner option. 我是一个非常新手的Java编码器,我想做个gui来向妈妈展示为什么我应该通过使用Import Scanner选项提交合理的数字来得到一份显示事实和数字的工作。 I wanted to make it as an actuale program instead of opening eclipse to show her. 我想让它成为一个实际的程序,而不是为了向她展示日食。 I tried running it and it wont work open. 我尝试运行它,它将无法打开。 Please tell me what I did wrong. 请告诉我我做错了。 Thank you. 谢谢。 Also I do have all the imports but didnt copy and paste to here. 我也有所有的进口,但没有复制粘贴到这里。

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



    }





}

You need to call the gui() method in your main method. 您需要在主方法中调用gui()方法。 But I don't see why you even need the JFrame because you aren't writing any text to it. 但是我不明白为什么您甚至不需要JFrame,因为您没有在其中编写任何文本。

EDIT: I would highly recommend using JavaFX for GUIs. 编辑:我强烈建议对JavaFX使用JavaFX。 Here is a rewritten version of your program: 这是程序的重写版本:

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

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在尝试用Java创建基于文本的GUI游戏。 如何使消息框更新以显示下一个结果? - I'm trying to make a text based GUI game in Java. How do I make the message boxes update to show the next outcomes? 我正在用Java开发基于GUI的文本冒险游戏。 如何使消息框更新以显示结果? - I am making a GUI based text adventure game in Java. How do I make the message boxes update to show the outcomes? 我正在尝试用Java创建GUI。我创建了框架,但面板没有显示。 怎么了? - I am trying to make a GUI in Java.I created the frame, but the panel doesn't show up. What is wrong? 我尝试显示图像3秒钟,然后在Java GUI中切换,但它不起作用 - i tried show image for 3 seconds and then switched in java gui but its doesnt work Java(Netbeans)-如何在if语句中为“访问被拒绝”编码,我尽力了但它没有用..? - Java (Netbeans) - how to code for “access denied” in the if statement, i tried the best but it didnt work..? 链接列表中的数据不会显示在GUI的文本区域中。 Java。 为什么? - The data in linked list won't show on the Text Area in GUI. Java. Why? 甚至 Java 中的阶乘方法。 我已经尝试了一切,但无法弄清楚 - Even Factorial Method in Java. I have tried everything and can't figure it out 我试着在java中读取一个excel文件。 读取整数数据时读取为float y? - I tried to read a excel file in java. While reading the integer data is read as float y? 如何在Java中将0.055的值四舍五入为0.06。 我尝试过DecimalFormat,但它返回0.05 - how to round 0.055 value to 0.06 in java. I have tried DecimalFormat but it returns 0.05 Java-Gui组件不显示 - Java - Gui Components do not show up
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM