基於丁老師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;
}
}