16秋计算机JAVA第六节课作业

来源:互联网 发布:网络验证源码 编辑:程序博客网 时间:2024/06/05 08:16

1.

import java.awt.*;import javax.swing.event.*;import javax.swing.*;import java.awt.event.*;public class S{public static void main(String args[]){   Computer fr=new Computer();}} class Computer extends JFrame implements DocumentListener{JTextArea text1,text2;int count=1;double sum=0,aver=0;Computer(){setLayout(new FlowLayout());text1=new JTextArea(6,20);text2=new JTextArea(6,20);add(new JScrollPane(text1));add(new JScrollPane(text2));text2.setEditable(false);(text1.getDocument()).addDocumentListener(this);setSize(300,320);setVisible(true);validate();setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);}public void changedUpdata(DocumentEvent e){String s=text1.getText();String []a=s.split("[^0123456789.]+");sum=0;aver=0;for(int i=0;i<a.length;i++){try{sum=sum+Double.parseDouble(a[i]);}catch(Exception ee){}}count=a.length;aver=sum/count;text2.setText(null);text2.append("\n和:"+sum);text2.append("\n平均值:"+aver);}public void removeUpdate(DocumentEvent e){    changedUpdate(e);  }public void insertUpdate(DocumentEvent e){changedUpdate(e);}}


2.

import java.awt.*;import javax.swing.event.*;import javax.swing.*;import java.awt.event.*;public class S{public static void main(String args[]){   ComputerFrame fr=new ComputerFrame();   fr.setBounds(100,100,650,120);   fr.setTitle("多功能计算器");   fr.setBackground(Color.blue);}} class ComputerFrame extends JFrame implements Actionlistener{JTextField text1,text2,text3;JButton buttonAdd,buttonSub,buttonMul,buttonDiv;JLabel label;public ComputerFrame(){setLayout(new FlowLayout());text1=new JTextField(10);text2=new JTextField(10);text3=new JTextField(10);label=new JLabel("   ",JLabel.CENTER);label.setBackground(Color.green);add(text1);add(label);add(text2);add(text3);buttonAdd=new JButton("加");buttonSub=new JButton("减");buttonMul=new JButton("乘");buttonDiv=new JButton("除");add(buttonAdd);add(buttonSub);add(buttonMul);add(buttonDiv);buttonAdd.addActionListener(this);buttonSub.addActionListener(this);buttonMul.addActionListener(this);buttonDiv.addActionListener(this);setSize(300,320);setVisible(true);validate();setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);}public void actionPerformed(ActionEvent e);{double n;if(e.getSource()==buttonAdd){double n1,n2;try{n1=Double.parseDouble(text1.getText());    n2=Double.parseDouble(text2.getText());n=n1+n2;text3.setText(String.valueOf(n));label.setText("+");}catch(NumberFormatException ee){ text3.setText("请输入数字字符");}}else if(e.getSource()==buttonSub){double n1,n2;try{n1=Double.parseDouble(text1.getText());    n2=Double.parseDouble(text2.getText());n=n1+n2;text3.setText(String.valueOf(n));label.setText("-");catch(NumberFormatException ee){ text3.setText("请输入数字字符");}}else if(e.getSource()==buttonMul){double n1,n2;try{n1=Double.parseDouble(text1.getText());    n2=Double.parseDouble(text2.getText());n=n1+n2;text3.setText(String.valueOf(n));label.setText("*");}catch(NumberFormatException ee){ text3.setText("请输入数字字符");}}else if(e.getSource()==buttonDiv){double n1,n2;try{n1=Double.parseDouble(text1.getText());    n2=Double.parseDouble(text2.getText());n=n1+n2;text3.setText(String.valueOf(n));label.setText("/");}catch(NumberFormatException ee){ text3.setText("请输入数字字符");}} validate();}}


3.

 import java.awt.*;  import java.awt.event.*; import javax.swing.*;public class S { public static void main(String args[]){   Window win = new Window();  win.setTitle("使用MVC结构");    win.setBounds(100,100,420,260);  }}class Window extends JFrame implements ActionListener { Lader lader;             //模型  JTextField textAbove,textBottom,textHeight;   //视图   JTextArea showArea;         //视图  JButton controlButton;        //控制器  Window() {   init();    setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   }  void init(){ lader = new Lader();  textAbove = new JTextField(5);   textBottom = new JTextField(5);  textHeight = new JTextField(5); showArea = new JTextArea();  controlButton=new JButton("计算面积");   JPanel pNorth=new JPanel();    pNorth.add(new JLabel("上底:")); pNorth.add(textAbove);  pNorth.add(new JLabel("下底:"));   pNorth.add(textBottom);     pNorth.add(new JLabel("高:"));  pNorth.add(textHeight);    pNorth.add(controlButton);    controlButton.addActionListener(this);    add(pNorth,BorderLayout.NORTH);add(new JScrollPane(showArea),BorderLayout.CENTER);   }   public void actionPerformed(ActionEvent e) {   try{double above = Double.parseDouble(textAbove.getText().trim()); double bottom = Double.parseDouble(textBottom.getText().trim()); double height = Double.parseDouble(textHeight.getText().trim());   lader.setAbove(above);   lader.setBottom(bottom);              lader.setHeight(height);               double area = lader.getArea();   showArea.append("面积:"+area+"\n");   }    catch(Exception ex) {  showArea.append("\n"+ex+"\n"); }   }}class Lader {  double above,bottom,height;   public double getArea() {    double area = (above+bottom)*height/2.0;    return area;  }   public void setAbove(double a) {   above = a;   }    public void setBottom(double b) { bottom = b; }public void setHeight(double c) {height = c;}}



原创粉丝点击