Java实验五参考答案
1.找错误
btOK.setOnAction( new EventHandler<ActionEvent> { public void handle (ActionEvent e) { System.out.println (e.getSource()); } } ); // Something missing here
编程题15.1
import javafx.application.Application; import javafx.stage.*; import javafx.scene.*; import javafx.scene.control.Button; import javafx.scene.layout.VBox; import javafx.geometry.*; import javafx.scene.layout.*; import javafx.scene.image.*; import java.util.*;
public class getcards extends Application { @Override public void start(Stage primaryStage){ VBox pane=new VBox(5); pane.setPadding(new Insets(5,5,5,5)); Cardspane cards=new Cardspane(); pane.getChildren().add(cards); Button btRefresh = new Button("Refresh"); pane.getChildren().add(btRefresh); pane.setAlignment(Pos.CENTER); VBox.setMargin(btRefresh, new Insets(5,5,5,5)); btRefresh.setOnAction(e->{cards.refresh();}); Scene scene=new Scene(pane,400,400); primaryStage.setTitle("etcards"); primaryStage.setScene(scene); primaryStage.show(); } public class Cardspane extends HBox{ private Integer[] cardsNum ={1,2,3,4,5,6,7,8,9 ,10,11,12,13,14,15,16,17,18,19,120,21,22 ,23,24,25,26,27,28,29,30,31,32,33,34,35, 36,37,38,39,40,41,42,43,44,45,46,47,48,49,50 ,51,52,53,54 }; private ArrayList<Integer>list =new ArrayList<>(Arrays.asList(cardsNum)); private ImageView p1; private ImageView p2; private ImageView p3; private ImageView p4; public void CardsPane() { setSpacing(5); Collections.shuffle(list); p1=new ImageView("实验五/card/"+Integer.valueOf(list.get(0))+".png"); p2=new ImageView("实验五/card/"+Integer.valueOf(list.get(1))+".png"); p3=new ImageView("实验五/card/"+Integer.valueOf(list.get(2))+".png"); p4=new ImageView("实验五/card/"+Integer.valueOf(list.get(3))+".png"); super.getChildren().addAll(p1,p2,p3,p4); } public void refresh(){ Collections.shuffle(list); super.getChildren().removeAll(p1,p2,p3,p4); p1=new ImageView("实验五/card/"+Integer.valueOf(list.get(0))+".png"); p2=new ImageView("实验五/card/"+Integer.valueOf(list.get(1))+".png"); p3=new ImageView("实验五/card/"+Integer.valueOf(list.get(2))+".png"); p4=new ImageView("实验五/card/"+Integer.valueOf(list.get(3))+".png"); super.getChildren().addAll(p1,p2,p3,p4); } } public static void main(String[] args) { launch(args); }
}
编程题15.2
package javaseniorprograme; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.BorderPane; import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle; import javafx.stage.Stage; /** * 15.2 (旋转一个四边形) * @author ASUS */ public class Exercise15_02 extends Application{ private double angle = 0; @Override public void start(Stage primaryStage){ BorderPane root = new BorderPane(); BorderPane bottom = new BorderPane(); // 创建一个按钮 Button btOK = new Button("Rotate"); // 创建一个矩形 Rectangle rec = new Rectangle(150, 80); // 设置矩形轮廓颜色 rec.setStroke(Color.BLACK); // 设置矩形填充色 rec.setFill(Color.WHITE); // 按钮事件处理 btOK.setOnAction((ActionEvent e) -> { angle += 15; rec.setRotate(angle); System.out.println(e.getSource()); }); // 控件布局 root.setCenter(rec); bottom.setCenter(btOK); root.setBottom(bottom); // 场景 Scene scene = new Scene(root, 320, 200); primaryStage.setTitle("Exercise15_02"); primaryStage.setScene(scene); primaryStage.show(); } // 主方法 public static void main(String[] args){ launch(args); } }
爱我没结果!