- 分享
- 0
- 人气
- 0
- 主题
- 51
- 帖子
- 893
- UID
- 163526
- 积分
- 1336
- 阅读权限
- 18
- 注册时间
- 2008-8-30
- 最后登录
- 2015-2-16
- 在线时间
- 2325 小时
|
import javax.swing.*;//JComponent
import java.awt.*;//frame,layout
import java.awt.event.*;
public class Essays extends GradeActivity implements ActionListener{
private JButton btn1,btn2;
private JTextField F1,F2,F3,F4,F5,F6;
Essays(){
//create a panel with grammar,content,correctlength,spelling
JPanel p1=new JPanel();
p1.setLayout(new GridLayout(6,3,2,4));
p1.add(new JLabel());
p1.add(new JLabel());
p1.add(new JLabel(" Grammar "));
p1.add(F1 = new JTextField(3));
p1.add(new JLabel(" Content "));
p1.add(F2 = new JTextField(3));
p1.add(new JLabel(" Correct length "));
p1.add(F3 = new JTextField(3));
p1.add(new JLabel(" Spelling "));
p1.add(F4 = new JTextField(3));
F1.setHorizontalAlignment(JTextField.RIGHT);
F2.setHorizontalAlignment(JTextField.RIGHT);
F3.setHorizontalAlignment(SwingConstants.RIGHT);
F4.setHorizontalAlignment(SwingConstants.RIGHT);
//create a panel to display total and grade
JPanel p2=new JPanel();
p2.setLayout(new FlowLayout(FlowLayout.CENTER,5,2));
p2.add(new JLabel("Total"));
p2.add(F5 = new JTextField(5));
F5.setEditable(false);
p2.add(new JLabel(" Grade"));
p2.add(F6 = new JTextField(5));
F6.setEditable(false);
//create a panel for buttons
JPanel p3=new JPanel();
p3.setLayout(new FlowLayout(FlowLayout.CENTER,30,30));
p3.add(btn1 = new JButton("Process"));
p3.add(btn2 = new JButton("Exit"));
//create a panel to fit in other panel
JPanel p4=new JPanel();
p4.setLayout(new BorderLayout());
p4.add(p1,BorderLayout.WEST);
p4.add(p2,BorderLayout.SOUTH);
//add panel into frame
add(p3,BorderLayout.SOUTH);
add(p4,BorderLayout.CENTER);
btn1.addActionListener(this);
btn2.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==btn1){
double grammar=Double.parseDouble(F1.getText());
double content=Double.parseDouble(F2.getText());
double correctLength=Double.parseDouble(F3.getText());
double spelling=Double.parseDouble(F4.getText());
double total=grammar+content+correctLength+content;
F5.setText(String.valueOf(total));
char Grade=getGrade();
F6.setText(String.valueOf(Grade));
}
else{
System.exit(0);
}
}
public static void main(String[]args){
JFrame frame=new JFrame();
frame.setTitle("Grade Activity");
frame.setSize(300,320);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
//package lab4;
public class GradeActivity{
private double score=100;
private char grade='A';
public GradeActivity(){super();}
public GradeActivity(double score, char grade){
super();
this.score=score;
this.grade=grade;
}
public double getScore(){
return score;
}
public void setScore(double score){
this.score=score;
}
public char getGrade(){
if(score>=90)
return 'A';
else if(score>=80)
return 'B';
else if(score>=70)
return 'C';
else if(score>=60)
return 'D';
else return 'E';
}
public void setGrade(char grade){
this.grade=grade;
}
public String toString(){
return "Total score\t: " + getScore() + "\nGrade\t\t: " + getGrade();
}
}
//package lab4;
//import java.util.Scanner;
public class testEssays{
public static void main(String[] args){
//Scanner input=new Scanner(System.in);
Essays essay1=new Essays();
Essays essay2=new Essays(27,17.8,20,26.2);
System.out.println(essay1);
System.out.println();
System.out.println(essay2); //same with ;
//System.out.println(essay2.toString())
//System.out.println("Grammar\t\t: " + essay2.getGrammar());
}
}
C:\Users\Chan\Desktop\lab4\Essays.java:61: cannot find symbol
symbol : method add(javax.swing.JPanel,java.lang.String)
location: class Essays
add(p3,BorderLayout.SOUTH);
^
C:\Users\Chan\Desktop\lab4\Essays.java:62: cannot find symbol
symbol : method add(javax.swing.JPanel,java.lang.String)
location: class Essays
add(p4,BorderLayout.CENTER);
^
2 errors
Tool completed with exit code 1 |
|