Java 2 实用教程(第5版)耿祥义版 习题九

习 题 9
一、问答题
1.JFrame类的对象的默认布局是什么布局?
2.一个容器对象是否可以使用add方法添加一个JFrame窗口?
3.JTextField可以触发什么事件?
4.JTextArea中的文档对象可以触发什么类型的事件?
5.MouseListener接口中有几个方法?
6.处理鼠标拖动触发的MouseEvent事件需使用哪个接口?
二、选择题
1.下列哪个叙述是不正确的?
A.一个应用程序中最多只能有一个窗口。
B.JFrame创建的窗口默认是不可见的。
C.不可以向JFrame窗口中添加JFame窗口。
D.窗口可以调用setTitle(String s)方法设置窗口的标题。
2.下列哪个叙述是不正确的?
A.JButton对象可以使用使用addActionLister(ActionListener l)方法将没有实现ActionListener接口的类的实例注册为自己的监视器。
B.对于有监视器的JTextField文本框,如果该文本框处于活动状态(有输入焦点)时,用户即使不输入文本,只要按回车(Enter)键也可以触发ActionEvent事件
C.监视KeyEvent事件的监视器必须实现KeyListener接口。
D.监视WindowEvent事件的监视器必须实现WindowListener接口。
3.下列哪个叙述是不正确的?
A.使用FlowLayout布局的容器最多可以添加5个组件。
B.使用BorderLayout布局的容器被划分成5个区域。
C.JPanel的默认布局是FlowLayout布局。
D.JDialog的默认布局是BorderLayout布局。
三、编程题
1.编写应用程序,有一个标题为“计算”的窗口,窗口的布局为FlowLayout布局。窗口中添加两个文本区,当我们在一个文本区中输入若干个数时,另一个文本区同时对你输入的数进行求和运算并求出平均值,也就是说随着你输入的变化,另一个文本区不断地更新求和及平均值。
2.编写一个应用程序,有一个标题为“计算”的窗口,窗口的布局为FlowLayout布局。设计四个按钮,分别命名为“加”、“差”、“积、”、“除”,另外,窗口中还有三个文本框。单击相应的按钮,将两个文本框的数字做运算,在第三个文本框中显示结果。要求处理NumberFormatException异常。
3.参照例子15编写一个体现MVC结构的GUI程序。首先编写一个封装梯形类,然后再编写一个窗口。要求窗口使用三文本框和一个文本区为梯形对象中的数据提供视图,其中三个文本框用来显示和更新梯形对象的上底、下底和高;文本区对象用来显示梯形的面积。窗口中有一个按钮,用户单击该按钮后,程序用3个文本框中的数据分别作为梯形对象的上底、下底和高,并将计算出的梯形的面积显示在文本区中。
 

一、问答题
1.Frame容器的默认布局是BorderLayout布局。
2.不可以。
3.ActionEvent。
4.DocumentEvent。
5.5个。
6.MouseMotionListener。
二、选择题
1.C。2.A。3.A。4.D。5.C。
三、编程题
1. import java.awt.*;
import javax.swing.event.*;
import javax.swing.*;
import java.awt.event.*;
public class E {
   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 changedUpdate(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) {}
     }
     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 E {
   public static void main(String args[]) {
      ComputerFrame fr=new ComputerFrame();
   }
}
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 E {
   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;
   }
}

 

  • 13
    点赞
  • 56
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值