Java实验报告图形用户界面——Swing组件及事件一、实验目的:①掌握Java swing组件的使用方法,包括窗口、框架、对话框、面板、文本编辑框、、按钮、组合框等,掌握多种布局方式②理解委托时间处理模型,掌握不同组件,不同事件的事件处理方法,设计出能够响应事件的Java图形用户界面③掌握文件字符流的使用方法二、实验名称:模拟裁判评分三、实验要求:求若干整数的最大值、最小值和平均数。
输入若干整数和浮点数,求出最大值和最小值并显示。
并能用FileReader和FileWrite进行文本文件的输入输出处理,在面板上有保存进行处理。
程序运行界面如下图所示:要求:使用JTextFiled数组,数值个数可变;响应事件,计算最大值、最小值和平均分忽略空值和空串;按实际元素个数求值;解决除数为零的问题;能用FileReader和FileWrite进行文本文件的输入输出处理。
四、实验内容package模拟裁判评分;import java.awt.*;import javax.swing.*;import java.awt.event.*;import java.io.*;public class Source extends JFrame implements ActionListener{private JTextField texts[],text_source;private JButton button;private JButton b1,b2,b3;private JTextField t1,t2;//TextFile afile=new TextFile("");private int rows,columns;public Source(int rows,int columns ) throws IOException{super("模拟裁判评分");this.setBounds(300, 240, 650, 200);this.rows=rows;this.columns=columns;this.setBackground(java.awt.Color.lightGray);this.setDefaultCloseOperation(EXIT_ON_CLOSE);this.getContentPane().setEnabled(false);JPanel p1=new JPanel(new GridLayout(rows,columns));this.getContentPane().add(p1);texts=new JTextField[rows*columns];for(int i=0;i<texts.length;i++){texts[i]=new JTextField(i+2+".0",6);texts[i].setHorizontalAlignment(JTextField.CENTER);p1.add(texts[i]);}JPanel p2=new JPanel(new FlowLayout(FlowLayout.CENTER));this.getContentPane().add(p2,"South");button=new JButton("平均分");p2.add(button);button.addActionListener(this);text_source=new JTextField("",6);text_source.setEditable(false);text_source.setHorizontalAlignment(JTextField.CENTER);p2.add(text_source);b1=new JButton("去掉最高分");b2=new JButton("去掉最低分");b3=new JButton("保存");b1.addActionListener(this);b2.addActionListener(this);b3.addActionListener(this);t1=new JTextField("",6);t2=new JTextField("",6);t1.setEditable(false);t2.setEditable(false);t1.setHorizontalAlignment(JTextField.CENTER);t2.setHorizontalAlignment(JTextField.CENTER);p2.add(b1);p2.add(t1);p2.add(b2);p2.add(t2);p2.add(b3);this.setVisible(true);}public void actionPerformed(ActionEvent e){ String pingjunfen,max,min;double table[]=new double[texts.length];for(int i=0;i<texts.length;i++)try{ if(texts[i].getText().equals("")){remove(texts[i]);}elsetable[i]=Double.parseDouble(texts[i].getText());}catch(NumberFormatException nfe){JOptionPane.showMessageDialog(this, texts[i].getText()+"字符串不能转换为浮点数 请重新输入。
");}if(e.getSource()==button) //平均分{pingjunfen=String.valueOf( Average(table));pingjunfen=pingjunfen.substring(0,5);text_source.setText(pingjunfen);}if(e.getSource()==b1) //去掉最高分{max=String.valueOf( Max(table));t1.setText(max);}if(e.getSource()==b2) //去掉最低分{min=String.valueOf( Min(table));t2.setText(min);}if(e.getSource()==b3) //读写文件{try {this.writeToText();} catch (IOException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}try {this.readFromText();} catch (IOException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}}}public static void main(String args[]) throws IOException {new Source(6,10);}public double Average(double a[]) //平均分{double sum=0.0;double pingjunshu=0.0;for(int i=0;i<texts.length;i++){sum+=a[i];if(a[i]<0){JOptionPane.showMessageDialog(this,"有负数");}}sum-=((Max(a)+Min(a)));return sum/texts.length;}public double Max(double a[]) //最高分{double t=0.0;for(int i=0;i<texts.length;i++){if(a[i]<0){JOptionPane.showMessageDialog(this,"有负数");}if(t<a[i])t=a[i];}return t;}public double Min(double a[]) //最低分{double t=a[2];for(int i=0;i<texts.length;i++){if(a[i]<0){JOptionPane.showMessageDialog(this,"有负数");}if(t>a[i])t=a[i];}return t;}public void writeToText() throws IOException //写方法{FileWriter fout=new FileWriter("fibfile.txt");for(int i=0;i<rows*columns;i++){fout.write(texts[i].getText()+" ");if((i+1)%columns==0)fout.write("\r\n");}fout.write("\r\n");fout.write("平均分:"+text_source.getText()+" "+"最高分:"+t1.getText()+" "+"最低分:"+t2.getText());fout.close();}public void readFromText()throws IOException //读方法{FileReader fin=new FileReader("fibfile.txt");BufferedReader bin=new BufferedReader(fin);System.out.println("fibfile.txt"+":");String aline="";do{aline=bin.readLine();if(aline!=null)System.out.println(aline);}while(aline!=null);bin.close();fin.close();}}六、运行结果:点击四个按钮后的结果:七、实验说明①Source(int rows,int columns )构造方法中带有参数,实现数组可变,并用texts[]文本数组。