顺手存一个eclipse汉化网址 http://download.eclipse.org/technology/babel/update-site/R0.12.1/luna/
一、将类文件和main文件分开并扩展类的变量及方法
1.Course类及方法
package version1;
public class Course {
private long course_id;
private String course_name;
private int course_score;
private double course_gpa;
public Course(long course_id, int course_score)
{
this.course_id = course_id;
this.course_score = course_score;
this.course_gpa = calculateGpa(course_score);
}
public double calculateGpa(int score)
{
double gpa = 0;
if(score<0 || score>100)
System.out.println("Wrong Inupt! Try Again!");
else if(score>=90 && score<=100)
gpa = 4.0;
else if(score>=85)
gpa =3.7;
else if(score>=82)
gpa = 3.3;
else if(score>=78)
gpa = 3.0;
else if(score>=75)
gpa = 2.7;
else if(score>=72)
gpa = 2.3;
else if(score>=68)
gpa = 2.0;
else if(score>=64)
gpa = 1.5;
else if(score>=60)
gpa = 1.0;
else
gpa = 0.0;
return gpa;
}
}
2.控制台的main
public class main {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("What's the course id?");
long course_id = in.nextLong();
System.out.println("What's your score?");
int score = in.nextInt();
Course theCourse = new Course(course_id,score);
//theCourse.calculateGpa(score);
System.out.println("The GPA is : " + theCourse.calculateGpa(score));
}
}
二、尝试GUI
附录:关于GUI为什么要加EventQueue.invokeLater的答案:
Java's GUI is strictly single-threaded. All GUI related things in java should always go through a single thread. The thread is our legendary "AWT-EventQueue-0" . Hence all GUI related actions should necessarily go through the AWT Event thread. If not so you may end up in a deadlock. For small programs, this might never happen. But for a huge java application if you try frame.setVisible(true) kind of thing in main thread, you will soon find yourself searching a new job. What invokeLater() does is to post your Runnable in the AWT thread's event queue. So the code in your run method will be executed in the AWT-Eventqueue thread.
大意是说,java的GUI都是的单线程,应该使用事件调度线程去执行,如果没意思使用事件调度线程的话,可能造成死锁。但是在小的程序中,这种现象(死锁)不会发生的;大的应用程序中才会出现这种现象
1.FrameToolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int screenHeight = screenSize.height;
int screenWidth = screenSize.width;
setSize(screenWidth/2,screenHeight/2);
setLocationByPlatform(true);
2.添加button,并设定event改变背景色
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int screenHeight = screenSize.height;
int screenWidth = screenSize.width;
setSize(screenWidth/2,screenHeight/2);
setLocationByPlatform(true);
public void makeButton(String name, Color backgroundColor)
{
JButton button = new JButton(name);
//add button to panel
buttonPanel.add(button);
ColorAction action = new ColorAction(backgroundColor);
button.addActionListener(action);
}
3.添加panel
<span style="white-space:pre"> </span>//添加一个panel
buttonPanel = new JPanel();
//add a button
makeButton("yellow",Color.YELLOW);
//add panel to frame
add(buttonPanel);