System.out.print(\ s2=reader.next();
System.out.print(\ b2=reader.next();
System.out.print(\ c=reader.next();
System.out.print(\ num=reader.nextInt();
System.out.print(\ to=reader.nextFloat();
student obj1=new student(c,num,to,n2,s2,b2); obj1.displays(); break; } case 2:{
System.out.println(\ System.out.print(\ //n2=reader.next();
n2=buf.readLine(); System.out.print(\ s2=reader.next();
System.out.print(\ b2=reader.next();
System.out.print(\ d=reader.next();
System.out.print(\ w=reader.nextFloat();
teacher obj2=new teacher(d,w,n2,s2,b2); obj2.displayt(); break; }
case 3:System.exit(0);
default:System.out.println(\ } } } } 14、设计一个窗口,在窗口中摆放两个按钮,若按钮被点击了,就将该按钮上的标记改为“已按过”。
//e1.java 简单的事件处理程序(已加入事件处理) import java.awt.*; import javax.swing.*; import java.awt.event.*;
public class e1 extends JFrame implements ActionListener {
static e1 frm=new e1();
static Button b1=new Button(\ static Button b2=new Button(\ public static void main(String args[]) {
b1.addActionListener(frm); //把监听者frm向事件源b1注册 b2.addActionListener(frm); //把监听者frm向事件源b2注册
frm.setTitle(\操作事件\ frm.setLayout(new FlowLayout()); frm.setSize(260,170); frm.add(b1); frm.add(b2);
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frm.setVisible(true); }
public void actionPerformed(ActionEvent e) {
if (e.getSource()==b1) b1.setLabel(\已按过\ else
b2.setLabel(\已按过\ } }
15、设计一个窗口,其中包含两个标签(一个用于给出提示信息,另一个用来输出结果)和一个文本框。要求从文本框中获取用户给出的一个整数,并将该数的绝对值在第二个标签上输出。
//e2.java 简单的事件处理程序(已加入事件处理) import java.awt.*; import javax.swing.*; import java.awt.event.*;
public class e2 extends JFrame implements ActionListener {
static e2 frm=new e2();
static JButton b1=new JButton(\提交\
static JLabel l1=new JLabel(\请输入一个整数\ static JLabel l2=new JLabel(\显示结果\ static JTextField t1=new JTextField(10); public static void main(String args[]) {
b1.addActionListener(frm); //把监听者frm向事件源b1注册
// frm.setTitle(\操作事件\ frm.setLayout(null); frm.setSize(460,370);
l1.setBounds(20,10,100,25); t1.setBounds(140,10,170,25); l2.setBounds(20,30,170,25); b1.setBounds(70,135,150,25);
frm.add(b1); frm.add(l1); frm.add(l2); frm.add(t1);
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frm.setVisible(true); }
public void actionPerformed(ActionEvent e) {
int x=Integer.parseInt(t1.getText()); l2.setText(Math.abs(x)+\ } }
16、13-6
//第13章第6题 import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.awt.Rectangle;
public class Exercises13_6 extends JFrame implements ActionListener {
static Exercises13_6 frm = new Exercises13_6(); static JButton b1 =new JButton(); static int i=0;
public static void main(String[] args)
{ b1.addActionListener(frm); //把监听者frm向事件源b1注册 frm.setSize(300, 200); frm.setTitle(\ frm.add(b1);
b1.setBounds(new Rectangle(71, 40, 147, 49)); b1.setText(\开始点击\
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frm.setVisible(true); }
public void actionPerformed(ActionEvent e)
{
i++;
b1.setText(\被点击\次\ } }
17、Lab68
//copy.java 简单的事件处理程序(已加入事件处理) import java.awt.*; ; import javax.swing.*; import java.awt.event.*;
public class copy implements ActionListener { JButton b;
JTextField L,R;
public void display() {
JFrame f=new JFrame(); f.setSize(400,150);
f.setBackground(Color.lightGray);
f.setLayout(new FlowLayout(FlowLayout.LEFT)); L=new JTextField(10); R=new JTextField(10); b=new JButton(\复制\ f.add(L); f.add(R); f.add(b);
b.addActionListener(this);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); }
public void actionPerformed(ActionEvent e) {
if(e.getSource()==b)
//R.setText(\ R.setText(L.getText()); }
public static void main(String arg[]) {
(new copy()).display(); } }
18.请编写一个应用程序实现如下功能:从键盘输入整型参数x和y,求x和y的平方和并输出。
import java.util.Scanner; public class review20 {
public static void main(String[] args) { Scanner input =new Scanner(System.in);
System.out.println(\请输入两个整型参数:\ int x=input.nextInt(); int y=input.nextInt(); int num; num=x*x+y*y;
System.out.println(\和y的平方和为:\ } }
19、已知抽象类shape,求底面半径为2高位3的圆柱体体积和长、宽、高分别为3、2、4的长方体体积。
abstract class Shape {
protected String name; public Shape(String xm) {
name=xm;
System.out.print(\名称:\ }
abstract public double vol(); }
class cylinder extends Shape {
private final double PI=3.14; private double radius; private double height;
public cylinder(String shapeName,double r,double h) {
super(shapeName); radius=r; height=h; }
public double vol() {
return PI*radius*radius*height; } }
class Rectangle extends Shape {
private double width; private double length; private double height;
public Rectangle(String shapeName,double l,double w,double h) {
super(shapeName); length=l; width=w; height=h; }
public double vol() {
return length*width*height; } }
public class review21 {
public static void main(String[] args) {
Shape rect =new Rectangle(\长方形\ System.out.println(\;周长=\ Shape cy=new cylinder(\圆\
System.out.println(\;周长=\ } }