简体   繁体   English

您如何在Java应用程序中包含小程序?

[英]How do you include an applet in a Java application?

So for my computer science class we have to make a gpa calculator and include an applet in the same program to display the full name of the student and their GPA. 因此,对于我的计算机科学课程,我们必须制作一个gpa计算器,并在同一程序中包含一个applet,以显示该学生及其GPA的全名。 I have the program working 我有程序在工作

import java.util.*;
import java.text.DecimalFormat;
import javax.swing.JApplet;
import java.awt.*;


public class Project1_Michael_Micool extends JApplet
{
public static void main(String[]args)
{
String first_name, last_name, course1, course2, course3, course4; 
    //Sets all the needed variables to string values for first, last and course names
int cred_course1, cred_course2, cred_course3, cred_course4, total_credits;
    //Sets all the course credits as int values
float grade_course1, grade_course2, grade_course3, grade_course4, grade_point1, grade_point2, grade_point3, grade_point4, total_grades_added, gpa;


Scanner scan = new Scanner(System.in);
DecimalFormat fmt = new DecimalFormat("0.##");


System.out.println("Welcome to the UNG GPA calculator.");

System.out.println();
System.out.println("Please enter your first name");
first_name = scan.next(); //Assigns the entered name as first_name
System.out.println("Please enter your last name");
last_name = scan.next();  //Assigns the entered name as last_name 


System.out.println();
System.out.println("A: 4.0      A-:3.67    ");
System.out.println("B+:3.33     B:3.00    B-:2.67");
System.out.println("C+: 2.33    C:2.00    C-:1.67");
System.out.println("D+: 1.33    D:1.00    F:0.00");


System.out.println();
System.out.println("Please enter your first course name and number. EX: PSYC1101");
course1 = scan.next();
System.out.println("Please enter the number of credits for course 1.");
cred_course1 = scan.nextInt();
System.out.println("Please enter your grade for course 1 using the chart. EX: B+= 3.33");
grade_course1 = scan.nextFloat();    


System.out.println();
System.out.println();
System.out.println("Please enter your second course name and number. EX: PSYC1101");
course2 = scan.next();
System.out.println("Please enter the number of credits for course 2.");
cred_course2 = scan.nextInt();
System.out.println("Please enter your grade for course 2 using the chart. EX: B+= 3.33");
grade_course2 = scan.nextFloat();


System.out.println();
System.out.println();
System.out.println("Please enter your third course name and number. EX: PSYC1101");
course3 = scan.next();
System.out.println("Please enter the number of credits for course 3.");
cred_course3 = scan.nextInt();
System.out.println("Please enter your grade for course 3 using the chart. EX: B+= 3.33");
grade_course3 = scan.nextFloat();


System.out.println();
System.out.println();
System.out.println("Please enter your fourth course name and number. EX: PSYC1101");
course4 = scan.next();
System.out.println("Please enter the number of credits for course 4.");
cred_course4 = scan.nextInt();
System.out.println("Please enter your grade for course 4 using the chart. EX: B+= 3.33");
grade_course4 = scan.nextFloat();


total_credits = cred_course1 + cred_course2 + cred_course3 + cred_course4; 
grade_point1 = grade_course1 * cred_course1; 
grade_point2 = grade_course2 * cred_course2;
grade_point3 = grade_course3 * cred_course3;
grade_point4 = grade_course4 * cred_course4;
total_grades_added = grade_point1 + grade_point2 + grade_point3 + grade_point4;
gpa = total_grades_added / total_credits;
gpa = Math.abs(gpa);


course1 = course1.toUpperCase();
course2 = course2.toUpperCase();
course3 = course3.toUpperCase();
course4 = course4.toUpperCase();


System.out.println("Your name is " + first_name + " " + last_name); 
System.out.println("You are taking " + course1 + " which is worth " + cred_course1 + " credit hours");
System.out.println("You are taking " + course2 + " which is worth " + cred_course2 + " credit hours");
System.out.println("You are taking " + course3 + " which is worth " + cred_course3 + " credit hours");
System.out.println("You are taking " + course4 + " which is worth " + cred_course4 + " credit hours");
System.out.println("Your total credit hours is " + total_credits);
System.out.println("Your GPA is " + fmt.format(gpa));
}
}

Here is the applet that I think should work. 这是我认为应该起作用的小程序。

public void paint(Graphics page)
{
page.drawString(first_name + " " + last_name, 110, 70);
page.drawString(gmt.format(gpa), 130, 100);
}

So my question is, how to you incorporate the applet into the program? 所以我的问题是,如何将小程序合并到程序中?

What you posted is not an "Applet"; 您发布的内容不是“ Applet”; it's just the paint method of something that might be an AWT widget (eg an applet). 它只是可能是AWT小部件(例如applet)的paint方法。 Just use that method in a widget that you integrate into your UI. 只需在集成到UI中的小部件中使用该方法即可。

An Applet (or JApplet ) would not typically have a main(String[]) unless it is an hybrid application/applet (where the application is based around a Frame or JFrame ), but that main method is focused on the command line. 除非它是混合应用程序/小程序(其中应用程序基于FrameJFrame ),否则Applet (或JApplet )通常不会具有main(String[]) ),但是该main方法专注于命令行。

There is no practical way to use the command line code in either an applet or an application. 在applet或应用程序中,没有实际的方式来使用命令行代码。 It needs to be rewritten to use a GUI. 需要重写它才能使用GUI。


..for my computer science class.. ..参加我的计算机科学课程。

Please refer the instructor to Why CS teachers should stop teaching Java applets . 请向指导老师说明为什么CS老师应该停止教授Java applet

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM