《Java编程语言:原理与范例》课后实验源代码 下载本文

内容发布更新时间 : 2024/5/5 9:20:28星期一 下面是文章的全部内容请认真阅读。

/*********************************

4. 编写程序模拟用以表示直线的Line类,具体包含以下逻辑:

① 表示直线起点和终点的嵌套类Point,其包含描述横纵坐标的x、y字段。

② 表示直线方向的嵌套枚举Direction,其包含表示上下左右等方向的枚举常量。

在Line类中编写必要的代码,然后在测试类LineTest中构造几个Line对象并输出每个对象的有关信息。

**********************************/ package ch07;

import ch07.Line.Direction;

class Line { class Point { private int x; private int y; public Point(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } } enum Direction { UP, DOWN, LEFT, RIGHT; } private Point start; private Point end;

private Direction direction; void draw() { System.out.printf(\绘制直线:起点(%d, %d),终点(%d, %d),方向(%s)\start.getY(), end.getX(), end.getY(), direction.toString()); } public Point getStart() { return start; } public void setStart(Point start) { this.start = start; } public Point getEnd() { return end; } public void setEnd(Point end) { this.end = end; } public Direction getDirection() { return direction; } public void setDirection(Direction direction) { this.direction = direction; } }

public class Exp6_4 { public static void main(String[] args) { Line line = new Line(); line.setStart(line.new Point(1, 2)); line.setEnd(line.new Point(5, 8)); line.setDirection(Direction.DOWN); line.draw(); } }

第七章 实验一

/********************************* 1. 编写满足以下要求的GUI程序。

① 顶部两个文本框只接受大于0小于11的整数。

② 文本框数字改变时,自动刷新下部网格区域的按钮。 ③ 鼠标进入按钮时,在该按钮上显示“★”。 ④ 鼠标移出按钮时,隐藏该按钮上的文字。 **********************************/ package ch08;

import java.awt.BorderLayout; import java.awt.GridLayout;

import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent;

import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField;

// 处理文本框输入事件的监听器

class MyKeyListener extends KeyAdapter { public void keyTyped(KeyEvent e) { char c = e.getKeyChar(); // 得到输入的字符 if (!(c >= KeyEvent.VK_0 && c <= KeyEvent.VK_9)) { // 不是数字字符 e.consume(); // 吃掉 } } }

// 处理鼠标进入退出按钮事件的监听器

class MyMouseListener extends MouseAdapter { public void mouseEntered(MouseEvent e) { JButton b = (JButton) e.getSource(); b.setText(\★\ } public void mouseExited(MouseEvent e) {

JButton b = (JButton) e.getSource(); b.setText(\ } }

public class Exp7_1 extends JFrame implements ActionListener { int rows = 5, cols = 4; JTextField rowTf = new JTextField(\ JTextField colTf = new JTextField(\ JPanel center = new JPanel(); // 存放按钮的面板 MyKeyListener keyListener = new MyKeyListener(); MyMouseListener mouseListener = new MyMouseListener(); void addButtons() { // 将按钮加入center面板 center.setLayout(new GridLayout(rows, cols, 2, 2)); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { JButton b = new JButton(); b.addMouseListener(mouseListener); center.add(b); } } } void initUI() { JPanel top = new JPanel(new GridLayout(1, 4)); top.add(new JLabel(\行数:\ rowTf.addActionListener(this); rowTf.addKeyListener(keyListener); top.add(rowTf); top.add(new JLabel(\列数:\ colTf.addActionListener(this); rowTf.addKeyListener(keyListener); top.add(colTf); this.addButtons(); super.setLayout(new BorderLayout()); super.add(top, BorderLayout.NORTH); super.add(center, BorderLayout.CENTER); }

public void actionPerformed(ActionEvent e) { if (e.getSource() == rowTf) { rows = Integer.parseInt(rowTf.getText()); // 已经处理了键盘输入事件,此时的文本框文字一定可以解析为数字 // 判断行数是否超过了范围 if (rows > 10) { rows = 10; rowTf.setText(\ } else if (rows < 1) { rows = 1; rowTf.setText(\ } } else { cols = Integer.parseInt(colTf.getText()); // 判断行数是否超过了范围 if (cols > 10) { cols = 10; colTf.setText(\ } else if (cols < 1) { cols = 1; colTf.setText(\ } } center.removeAll(); // 删除面板中的所有组件 this.addButtons(); // 重新添加按钮到面板 center.validate(); // 通知系统重新排列面板 center.repaint(); // 重绘面板 } public static void main(String[] args) { Exp7_1 t = new Exp7_1(); t.initUI(); t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); t.setResizable(false); t.setSize(600, 400); t.setVisible(true); } }

实验二

/*********************************

2. 在窗口中按下鼠标左键并拖曳时,绘制矩形(要求使用适配器类)。 **********************************/ package ch08;