JAVA练习-第2天

 顺手存一个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.Frame
Toolkit 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改变背景色

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值