第五周实验_打字成绩判断程序GUI

基于丁老师http://blog.csdn.net/dyz1982/article/details/22710247作业第五个需求编码

另外,对于丁老师的需求还是有一点不清楚

使用方法,在文本框中输入一分钟打字数量,然后点击计算分数,即可。

另外,这个有些简单(或许是我不了解需求)。所以有时间将会做一个小电子钟外加老师的第一个作业。

代码如下


import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class TouchType {

	/**
	 * @param args
	 * code by cp
	 * version alpha 0.1
	 * date 4/1/2014
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		TouchFrame frame = new TouchFrame();//实例化窗体
		frame.setVisible(true);//设置可见
	}

}
class TouchFrame extends JFrame{
	
	//static boolean Flag = false;
	
	static TouchPanel tp = new TouchPanel();
	private static JLabel lable = new JLabel("成绩:");//标签
	
	public TouchFrame(){
		
		this.setTitle("分数计算");//设置窗体标题
		this.setSize(400, 150);//设置大小
		
		this.add("North",tp);
		this.add("South",lable);
		
		this.setLocationRelativeTo(null);//设置位置居中
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//响应关闭
		
	}
	
	public static void setlabel(String str){
		lable.setText(str);
	}

	
}

class TouchPanel extends JPanel implements ActionListener{
	private static JTextField textfield1,textfield2;//输入字符框
	private static JButton button1,button2;//按钮
	
	static Score score = new Score();//实例化分数
	static boolean Flag = false;
	
	public TouchPanel(){
		
		//this.setTitle("分数计算");//设置窗体标题
		//this.setSize(400, 150);//设置大小
		
		//实例化组件
		textfield1 = new JTextField("请输入打字数",10);
		textfield2 = new JTextField(10);
		button1 = new JButton("计算分数");
		button2 = new JButton("再次计算分数");
		
		textfield2.setVisible(false);
		button2.setVisible(false);
		
		//添加组件
		this.setLayout(new GridLayout(2,2));
		this.add(textfield1);
		this.add(button1);
		this.add(textfield2);
		this.add(button2);
		
		//添加监听事件
		button1.addActionListener(this);
		button2.addActionListener(this);
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {//相应按钮事件
		
		if(Flag = score.computing(getcount())){
			
			textfield1.setEditable(false);//不可编辑
			button1.setEnabled(false);//不可用
			
			

			textfield2.setVisible(true);//可视
			button2.setVisible(true);//可视
			textfield2.requestFocus();
		}
		
		// TODO Auto-generated method stub
		
	}
	
	public int getcount(){//获取打字数
		int x;
		if(Flag){
			x = Integer.parseInt(textfield2.getText());
		}else{
			x = Integer.parseInt(textfield1.getText());
		}
		return x;

	}
	
	public static void setscore(String str){//输出成绩
		
		TouchFrame.setlabel(str);
	}
	
}

class Score {
	
//	static TouchFrame frame = new TouchFrame();//实例化窗体类
	
	public boolean computing(int count){//计算分数
		
		boolean flag_s = false;
		int s = 0;
		//int count = frame.getcount();
		
		if(count>=30){//优秀
			s=15;
			TouchPanel.setscore("成绩优秀,分数为"+s);
		}
		else if (count>=15 && count<30){//合格
			s=10;
			TouchPanel.setscore("成绩合格,分数为"+s);
		}
		else{//不及格
			if(count%2==0)
				s=10-((15-count+1)/2);
			else
				s=10-((15-count)/2);
			TouchPanel.setscore("成绩不合格,分数为"+s+"请四月15号补考!");
			flag_s = true ;
		}
		return flag_s;
		
	}
	
}






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章