JAVA黑皮书课后练习题15章15.8

*15.8 (Display the mouse position) Write two programs, such that one displays the mouse position when the mouse button is clicked (see Figure 15.26a) and the other displays the mouse position when the mouse button is pressed and ceases to display it when the mouse button is released.

package section_15;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class _08_1 extends Application {
	public void start(Stage primaryStage) {
		Text text = new Text();

		Pane pane = new Pane();
		pane.getChildren().add(text);
		pane.setOnMouseClicked(e -> {
			double x = e.getX();
			double y = e.getY();
			text.setText("(" + x + "," + y + ")");
			text.setX(x);
			text.setY(y);
		});
		Scene scene = new Scene(pane, 400, 200);

		primaryStage.setTitle("_15_08_1");
		primaryStage.setScene(scene);
		primaryStage.show();

	}

	public static void main(String[] args) {
		Application.launch(args);
	}

}

 

package section_15;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class _08_2 extends Application {
	public void start(Stage primaryStage) {
		Text text = new Text();

		Pane pane = new Pane();
		pane.setOnMousePressed(e -> {
			double x = e.getX();
			double y = e.getY();
			text.setText("(" + x + ", " + y + ")");
			text.setX(x);
			text.setY(y);
			pane.getChildren().add(text);
		});
		pane.setOnMouseReleased(e -> pane.getChildren().remove(text));
		Scene scene = new Scene(pane, 400, 200);

		primaryStage.setTitle("_15_08_2");
		primaryStage.setScene(scene);
		primaryStage.show();
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java皮书课后习题10.11要求我们编写一个程序,该程序在一个二维数组中查找是否存在一个指定的值。如果存在,则返回该值的位置(行和列),否则返回(-1, -1)。 首先,我们需要创建一个二维数组,并初始化它。然后,通过遍历数组的每个元素来查找指定值。当找到指定值时,记录其位置并返回。 以下是解题思路: 1. 创建一个名为findValue的方法,参数为一个二维整数数组和一个整数值。 2. 在方法内,使用两个嵌套的for循环遍历数组的每个元素,外层循环控制行,内层循环控制列。 3. 在每次循环中,检查当前元素是否等于指定值。如果是,返回当前位置(row, column)。 4. 如果遍历完整个数组都没有找到指定值,返回位置(-1, -1)。 以下是代码示例: ```java public class Main { public static void main(String[] args) { // 创建二维数组 int[][] array = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; int target = 5; // 指定值 // 调用findValue方法查找指定值的位置 int[] position = findValue(array, target); // 输出结果 if (position[0] == -1 && position[1] == -1) { System.out.println("指定值不存在!"); } else { System.out.println("找到指定值,位置为(" + position[0] + ", " + position[1] + ")"); } } public static int[] findValue(int[][] array, int target) { for (int row = 0; row < array.length; row++) { for (int column = 0; column < array[row].length; column++) { if (array[row][column] == target) { return new int[]{row, column}; } } } return new int[]{-1, -1}; } } ``` 以上代码会输出"找到指定值,位置为(1, 1)",即指定值5在二维数组的第2行第2列。如果指定值不在数组中,则输出"指定值不存在!"。 希望这个回答对你有帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值