📄 cjtj.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class CJTJ extends JFrame implements ActionListener
{
JLabel jlSubject=new JLabel("考试科目");
JTextField jtfSubject=new JTextField();
JLabel jlCount=new JLabel("考试人数");
JTextField jtfCount=new JTextField();
JButton jbOk=new JButton("确定");
JButton jbAccount=new JButton("统计");
JPanel jpList=new JPanel();
JScrollPane jsp=new JScrollPane(jpList);
JLabel jlSno=new JLabel("学号");
JLabel jlSname=new JLabel("姓名");
JLabel jlScore=new JLabel("成绩");
int total;//总人数
Student[] sa;
public CJTJ()
{
this.setTitle("成绩统计");
this.setLayout(null);
jlSubject.setBounds(10,10,80,20);
this.add(jlSubject);
jtfSubject.setBounds(70,10,90,20);
this.add(jtfSubject);
jlCount.setBounds(170,10,60,20);
this.add(jlCount);
jtfCount.setBounds(230,10,90,20);
this.add(jtfCount);
jbOk.setBounds(330,10,60,20);
this.add(jbOk);
jpList.setBackground(Color.red);
jsp.setBounds(10,40,380,300);
this.add(jsp);
jbAccount.setBounds(170,350,60,20);
this.add(jbAccount);
this.initTable();
this.setBounds(100,100,410,450);
this.setVisible(true);
this.setStatus(false);
jbOk.addActionListener(this);
jbAccount.addActionListener(this);
}
public void initTable()
{
jpList.setLayout(null);
jlSno.setBounds(50,5,80,20);
jpList.add(jlSno);
jlSname.setBounds(165,5,80,20);
jpList.add(jlSname);
jlScore.setBounds(290,5,80,20);
jpList.add(jlScore);
}
public void setStatus(boolean b)
{
this.jtfSubject.setEnabled(!b);
this.jtfCount.setEnabled(!b);
this.jbOk.setEnabled(!b);
this.jbAccount.setEnabled(b);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==jbOk)
{//按下确定按钮
String sSubject=this.jtfSubject.getText();
String sCount=this.jtfCount.getText();
if(sSubject.trim().length()==0
||sCount.trim().length()==0)
{
JOptionPane.showMessageDialog(this,"必须输入科目及人数信息!!!",
"提示",JOptionPane.INFORMATION_MESSAGE);
return;
}
try
{
total=Integer.parseInt(sCount);
}
catch(NumberFormatException ea)
{
JOptionPane.showMessageDialog(this,"考试人数必须为整数!!!",
"提示",JOptionPane.INFORMATION_MESSAGE);
return;
}
if(total<=0)
{
JOptionPane.showMessageDialog(this,"考试人数必须为正整数!!!",
"提示",JOptionPane.INFORMATION_MESSAGE);
return;
}
sa=new Student[total];
jpList.setPreferredSize(new Dimension(375,25*(total+1)));
for(int i=0;i<total;i++)
{
sa[i]=new Student();
sa[i].jtfSno.setBounds(5,25*(i+1),120,20);
jpList.add(sa[i].jtfSno);
sa[i].jtfSname.setBounds(130,25*(i+1),120,20);
jpList.add(sa[i].jtfSname);
sa[i].jtfSscore.setBounds(255,25*(i+1),120,20);
jpList.add(sa[i].jtfSscore);
}
for(int i=0;i<total;i++)
{
sa[i].jtfSno.addActionListener(new MyAdapter(sa[i].jtfSname));
sa[i].jtfSname.addActionListener(new MyAdapter(sa[i].jtfSscore));
if(i<total-1)
{
sa[i].jtfSscore.addActionListener(new MyAdapter(sa[i+1].jtfSno));
}
}
this.setVisible(false);
this.setVisible(true);
this.setStatus(true);
}
else if(e.getSource()==jbAccount)
{//按下统计按钮
boolean flag=false;
for(Student ts:sa)
{
flag=checkInput(ts.jtfSno,"请输入对应学生的学号!");
if(flag)
{
flag=checkInput(ts.jtfSname,"请输入对应学生的姓名!");
if(flag)
{
flag=checkInput(ts.jtfSscore,"请输入对应学生的成绩!");
if(!flag)
{
return;
}
}
else
{
return;
}
}
else
{
return;
}
}
for(Student ts:sa)
{
String tscore=ts.jtfSscore.getText().trim();
try
{
ts.score=Double.parseDouble(tscore);
if(ts.score>100||ts.score<0)
{
JOptionPane.showMessageDialog(this,"成绩必须在0~100之间!",
"提示",JOptionPane.INFORMATION_MESSAGE);
ts.jtfSscore.requestFocus(true);
return;
}
}
catch(NumberFormatException es)
{
JOptionPane.showMessageDialog(this,"成绩必须为数值!",
"提示",JOptionPane.INFORMATION_MESSAGE);
ts.jtfSscore.requestFocus(true);
return;
}
}
double max=0;
double min=100.0;
double avg=0;
for(Student ts:sa)
{
if(max<ts.score)
{
max=ts.score;
}
if(min>ts.score)
{
min=ts.score;
}
avg+=ts.score;
}
avg=avg/total;
String msgr="最高分:"+max+"\n"+
"最低分:"+min+"\n"+
"平均分:"+((int)(avg*100))/100.0;
JOptionPane.showMessageDialog(this,msgr,"成绩统计结果",
JOptionPane.INFORMATION_MESSAGE);
}
}
public boolean checkInput(JTextField jtf,String msg)
{
if(jtf.getText().trim().length()==0)
{
JOptionPane.showMessageDialog(this,msg,
"提示",JOptionPane.INFORMATION_MESSAGE);
jtf.requestFocus(true);
return false;
}
return true;
}
public static void main(String args[])
{
new CJTJ();
}
}
class Student
{
double score;
JTextField jtfSno=new JTextField();
JTextField jtfSname=new JTextField();
JTextField jtfSscore=new JTextField();
}
class MyAdapter implements ActionListener
{
JTextField jtfNext;
public MyAdapter(JTextField jtfNext)
{
this.jtfNext=jtfNext;
}
public void actionPerformed(ActionEvent e)
{
jtfNext.requestFocus(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -