注:做本实验要先安装javaFX
一、实验目的
掌握常用GUI控制组件及其事件处理。
二、实验内容
1.编程包含一个标签和一个按钮,单击按钮时,标签的内容在“你好”和“再见”之间切换。分别用本类(该类自身),外部类,内部类,匿名内部类,以及lambda表达式完成。
本类:
//本类
package project8;
import javafx.application.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.*;
public class Hello_Bye extends Application implements EventHandler<ActionEvent> {
Label l=new Label("你好");
Button btn=new Button();
@Override
public void start(Stage primaryStage) {
VBox root=new VBox(10);
root.setAlignment(Pos.CENTER);
btn.setMinSize(100, 50);
l.setMinSize(40, 30);
root.getChildren().addAll(l,btn);
Hello_Bye h=new Hello_Bye();
btn.setOnAction(h);
Scene s=new Scene(root,250,200);
primaryStage.setTitle("你好和再见");
primaryStage.setScene(s);
primaryStage.show();
}
@Override
public void handle(ActionEvent e) {//利用((Button) (e.getSource())).getParent()获取标签l的父亲面板
if (((Label) ((Button) (e.getSource())).getParent().getChildrenUnmodifiable().get(0)).getText() == "你好")//调用 get(控件在面板的下标) 方法获取标签l,然后强制类型转换
((Label) ((Button) (e.getSource())).getParent().getChildrenUnmodifiable().get(0)).setText("再见");
else
((Label) ((Button) (e.getSource())).getParent().getChildrenUnmodifiable().get(0)).setText("你好");
}
public static void main(String[] args) {
launch(args);
}
}
外部类:
//外部类
package project8;
import javafx.application.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.*;
public class Hello_Bye2 extends Application {
Label l=new Label("你好");
Button btn=new Button();
@Override
public void start(Stage primaryStage) {
VBox root=new VBox(10);
root.setAlignment(Pos.CENTER);
btn.setMinSize(100, 50);
l.setMinSize(40, 30);
root.getChildren().addAll(l,btn);
btnhandle h=new btnhandle();
btn.setOnAction(h);
Scene s=new Scene(root,250,200);
primaryStage.setTitle("你好和再见");
primaryStage.setScene(s);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
class btnhandle extends Hello_Bye2 implements EventHandler<ActionEvent>{
@Override
public void handle(ActionEvent e) {//利用((Button) (e.getSource())).getParent()获取标签l的父亲面板
if (((Label) ((Button) (e.getSource())).getParent().getChildrenUnmodifiable().get(0)).getText() == "你好")//调用 get(控件在面板的下标) 方法获取标签l,然后强制类型转换
((Label) ((Button) (e.getSource())).getParent().getChildrenUnmodifiable().get(0)).setText("再见");
else
((Label) ((Button) (e.getSource())).getParent().getChildrenUnmodifiable().get(0)).setText("你好");
}
}
内部类:
//内部类
package project8;
import javafx.application.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.*;
public class Hello_Bye3 extends Application {
Label l=new Label("你好");
Button btn=new Button();
@Override
public void start(Stage primaryStage) {
VBox root=new VBox(10);
root.setAlignment(Pos.CENTER);
btn.setMinSize(100, 50);
l.setMinSize(40, 30);
root.getChildren().addAll(l,btn);
btnhandle h=new btnhandle();
btn.setOnAction(h);
Scene s=new Scene(root,250,200);
primaryStage.setTitle("你好和再见");
primaryStage.setScene(s);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
class btnhandle implements EventHandler<ActionEvent>{
@Override
public void handle(ActionEvent e) {
if(l.getText()=="你好")
l.setText("再见");
else
l.setText("你好");
System.out.println(btn.getParent());
System.out.println(e.getSource());
System.out.println(e.getSource()==btn);
}
}
}
匿名内部类:
//匿名内部类
package project8;
import javafx.application.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.*;
public class Hello_Bye4 extends Application {
@Override
public void start(Stage primaryStage) {
Label l = new Label("你好");
Button btn = new Button();
VBox root=new VBox(10);
root.setAlignment(Pos.CENTER);
btn.setMinSize(100, 50);
l.setMinSize(40, 30);
root.getChildren().addAll(l,btn);
btn.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent e) {
if(l.getText()=="你好")
l.setText("再见");
else
l.setText("你好");
}
});
Scene s = new Scene(root, 250, 200);
primaryStage.setTitle("你好和再见");
primaryStage.setScene(s);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
lambda表达式:
//lambda表达式
package project8;
import javafx.application.*;
import javafx.geometry.Pos;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.*;
public class Hello_Bye5 extends Application {
@Override
public void start(Stage primaryStage) {
Label l = new Label("你好");
Button btn = new Button();
VBox root=new VBox(10);
root.setAlignment(Pos.CENTER);
btn.setMinSize(100, 50);
l.setMinSize(40, 30);
root.getChildren().addAll(l,btn);
btn.setOnAction(e->{
if (l.getText().equals("你好"))
l.setText("再见");
else
l.setText("你好");
}
);
Scene s = new Scene(root, 250, 200);
primaryStage.setTitle("你好和再见");
primaryStage.setScene(s);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
2.编程包含一个文本框和一个文本区域,文本框内容改变时,将文本框中的内容显示在文本区域中;在文本框中按回车键时,清空文本区域的内容。
package project8;
import javafx.application.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.KeyCode;
public class TextFx extends Application{
@Override
public void start(Stage primaryStage) {
VBox root=new VBox(10);
TextField t1=new TextField("文本框");
TextArea t2=new TextArea("文本区域");
t2.setEditable(false);
root.getChildren().addAll(t1,t2);
t1.setOnKeyPressed(e->{
if(e.getCode()!=KeyCode.ENTER)
t2.setText(t1.getText());
else
t2.setText(null);
});
Scene s=new Scene(root, 300, 200);
primaryStage.setTitle("改变文本框");
primaryStage.setScene(s);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
3.编程包含一个复选按钮和一个普通按钮,复选按钮选中时,普通按钮的背景色为青色,未选中时为灰色。
package project8;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class ColorFx extends Application{
@Override
public void start(Stage primaryStage) {
Pane root = new Pane();
Button btn = new Button();
btn.setMinSize(60, 30);
CheckBox c= new CheckBox();
c.setLayoutX(20);
c.setLayoutY(50);
root.getChildren().addAll(c,btn);
c.setOnAction(e->{
if(c.isSelected()){
btn.setStyle("-fx-background-color:Cyan");
}
else{
btn.setStyle("-fx-background-color:Grey");
}
});
Scene scene = new Scene(root,200,100);
primaryStage.setTitle("复选框控制按钮背景色");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
4.编程显示当前鼠标的位置坐标。
package project8;
import javafx.application.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.*;
public class MouseFx extends Application{
@Override
public void start(Stage primaryStage) {
Pane root=new Pane();
Scene s=new Scene(root,400,300);
TextArea t=new TextArea();
t.setEditable(false);
t.setMinSize(2000,1000);
t.setMaxSize(Double.MAX_VALUE,Double.MAX_VALUE);
root.getChildren().addAll(t);
primaryStage.setScene(s);
t.setOnMouseMoved(e->{
t.setText("("+Double.toString(e.getX())+","+Double.toString(e.getY())+")");
});
primaryStage.setTitle("显示鼠标位置");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
5. 编写程序,实现使用键盘上的上下左右箭头控制界面上图片的移动。移动到边界时从界面另一侧出现。移动过程中显示另一个图片,停止时恢复原来的图片。
package project8;
import javafx.application.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.*;
import javafx.scene.input.KeyCode;
public class PictureFx extends Application {
ImageView image=new ImageView(new Image("project8//sxdx.jpg"));
Button btn=new Button("",image);
@Override
public void start(Stage primaryStage) {
Pane root=new Pane();
Scene scene = new Scene(root, 800, 600);
root.getChildren().addAll(btn);
btn.setMinSize(200, 200);
btn.setMaxSize(200, 200);
btn.setLayoutX(300);btn.setLayoutY(200);
double x=btn.getLayoutX();
double y=btn.getLayoutY();
btn.setOnKeyPressed(e->{
KeyCode s=e.getCode();
switch(s) {
case UP:
if(btn.getLayoutY()+btn.getHeight()==0) {
btn.relocate(x, root.getHeight());
btn.relocate(btn.getLayoutX(), btn.getLayoutY()+50);
}
else
btn.relocate(btn.getLayoutX(), btn.getLayoutY()-50);
break;
case DOWN:
if(btn.getLayoutY()+btn.getHeight()==scene.getHeight()+250) {
btn.relocate(x, -200);
btn.relocate(btn.getLayoutX(), btn.getLayoutY()-50);
}
else
btn.relocate(btn.getLayoutX(), btn.getLayoutY()+50);
break;
case LEFT:
if(btn.getLayoutX()+btn.getWidth()==0) {
btn.relocate(root.getWidth(), y);
btn.relocate(btn.getLayoutX()+50, btn.getLayoutY());
}
else
btn.relocate(btn.getLayoutX()-50, btn.getLayoutY());
break;
case RIGHT:
if(btn.getLayoutX()+btn.getWidth()==scene.getWidth()+250) {
btn.relocate(-200, y);
btn.relocate(btn.getLayoutX()-50, btn.getLayoutY());
}
else
btn.relocate(btn.getLayoutX()+50, btn.getLayoutY());
break;
}
});
btn.setOnKeyReleased(e->{
btn.setLayoutX(300);btn.setLayoutY(200);
});
primaryStage.setResizable(false);
primaryStage.setTitle("键盘控制图片移动");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
6 绘制如下形式的一个二叉树。
package project8;
import javafx.application.*;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.*;
public class TreeFx extends Application{
@Override
public void start(Stage primaryStage) {
Pane root=new Pane();
Circle[] cir=new Circle[9];
Label[] l=new Label[9];
Line[] line=new Line[8];
for(int i=0;i<9;i++) {
cir[i]=new Circle(10);
char ch=(char) (i+65);
l[i]=new Label(String.valueOf(ch));
root.getChildren().addAll(cir[i],l[i]);
cir[i].setStroke(Color.BLACK);
cir[i].setFill(new Color(0,0,0,0));
}
cir[0].setCenterX(340);cir[0].setCenterY(160);//A
cir[1].setCenterX(300);cir[1].setCenterY(200);//B
cir[2].setCenterX(380);cir[2].setCenterY(200);//C
cir[3].setCenterX(260);cir[3].setCenterY(240);//D
cir[4].setCenterX(340);cir[4].setCenterY(240);//E
cir[5].setCenterX(220);cir[5].setCenterY(280);//F
cir[6].setCenterX(300);cir[6].setCenterY(280);//G
cir[7].setCenterX(260);cir[7].setCenterY(320);//H
cir[8].setCenterX(340);cir[8].setCenterY(320);//I
l[0].setLayoutX(335);l[0].setLayoutY(155);//A
l[1].setLayoutX(295);l[1].setLayoutY(195);//B
l[2].setLayoutX(375);l[2].setLayoutY(195);//C
l[3].setLayoutX(255);l[3].setLayoutY(235);//D
l[4].setLayoutX(335);l[4].setLayoutY(235);//E
l[5].setLayoutX(215);l[5].setLayoutY(275);//F
l[6].setLayoutX(295);l[6].setLayoutY(275);//G
l[7].setLayoutX(255);l[7].setLayoutY(315);//H
l[8].setLayoutX(335);l[8].setLayoutY(315);//I
for(int i=0;i<8;i++) {
line[i]=new Line();
root.getChildren().add(line[i]);
}
line[0].setStartX(331);line[0].setStartY(166);line[0].setEndX(308);line[0].setEndY(193);//连接A B
line[1].setStartX(348);line[1].setStartY(166);line[1].setEndX(373);line[1].setEndY(193);//连接A C
line[2].setStartX(291);line[2].setStartY(206);line[2].setEndX(268);line[2].setEndY(233);//连接B D
line[3].setStartX(308);line[3].setStartY(206);line[3].setEndX(333);line[3].setEndY(233);//连接B E
line[4].setStartX(251);line[4].setStartY(246);line[4].setEndX(228);line[4].setEndY(273);//连接D F
line[5].setStartX(268);line[5].setStartY(246);line[5].setEndX(293);line[5].setEndY(273);//连接D G
line[6].setStartX(291);line[6].setStartY(286);line[6].setEndX(268);line[6].setEndY(313);//连接G H
line[7].setStartX(308);line[7].setStartY(286);line[7].setEndX(333);line[7].setEndY(313);//连接G I
Scene s=new Scene(root, 600,500);
primaryStage.setScene(s);
primaryStage.setTitle("二叉树");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}