目录
1、使用单选按钮
2、选择几何图形
3、交通信号灯
4、演示TextField的属性
5、演示TextArea的属性
6、选择一种字体
7、演示 Label 的属性
8、使 用ComboBox 和 ListView
9、使 用 ScrollBar 和 Slider
10、模拟:一个转动的风扇
1、使用单选按钮
编写一个 GUI 程序如图所示。可以使用按钮将消息进行左右移动,并且使用单选按钮来修改消息显示的颜色。
代码
package GUI_Practice;
import GUI_basis.ButtonDemo;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
public class ChangeColorWithRadioButton extends ButtonDemo {
protected BorderPane getPane() {
BorderPane pane = super.getPane();
HBox paneForRadioButtons = new HBox();
paneForRadioButtons.setPadding(new Insets(5, 5, 5, 5));
paneForRadioButtons.setStyle("-fx-border-color:green");
paneForRadioButtons.setStyle("-fx-border-width:2px;-fx-border-color:green");
RadioButton rbRed = new RadioButton("Red");
RadioButton rbGreen = new RadioButton("Green");
RadioButton rbBlue = new RadioButton("Blue");
RadioButton rbOrange = new RadioButton("Orange");
RadioButton rbYellow = new RadioButton("Yellow");
RadioButton rbPink = new RadioButton("Pink");
paneForRadioButtons.getChildren().addAll(rbRed, rbGreen, rbBlue, rbOrange, rbYellow, rbPink);
pane.setTop(paneForRadioButtons);
ToggleGroup group = new ToggleGroup();
rbRed.setToggleGroup(group);
rbGreen.setToggleGroup(group);
rbBlue.setToggleGroup(group);
rbOrange.setToggleGroup(group);
rbYellow.setToggleGroup(group);
rbPink.setToggleGroup(group);
rbRed.setOnAction(e -> {
if (rbRed.isSelected())
text.setFill(Color.RED);
});
rbGreen.setOnAction(e -> {
if (rbGreen.isSelected())
text.setFill(Color.GREEN);
});
rbBlue.setOnAction(e -> {
if (rbBlue.isSelected())
text.setFill(Color.BLUE);
});
rbOrange.setOnAction(e -> {
if (rbOrange.isSelected())
text.setFill(Color.ORANGE);
});
rbYellow.setOnAction(e -> {
if (rbYellow.isSelected())
text.setFill(Color.YELLOW);
});
rbPink.setOnAction(e -> {
if (rbPink.isSelected())
text.setFill(Color.PINK);
});
return pane;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Application.launch(args);
}
}
运行结果
2、选择几何图形
编写一个绘制各种几何图形的程序,如图所示。用户从单选按钮中选择 一个几何图形,并且使用复选框指定是否被填充。
代码
package GUI_Practice;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.CheckBox;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Ellipse;
import javafx.scene.shape.Rectangle;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;
import javafx.scene.paint.Color;
public class DrawGeometry extends Application {
protected Circle circle = new Circle(50);
protected Rectangle rectangle = new Rectangle(100, 50);
protected Ellipse ellipse = new Ellipse(80, 50);
protected BorderPane getPane() {
BorderPane pane = new BorderPane();
HBox paneForRadioButtons = new HBox(20);
paneForRadioButtons.setPadding(new Insets(5, 5, 5, 5));
RadioButton rbCircle = new RadioButton("Circle");
RadioButton rbRectangle = new RadioButton("Rectangle");
RadioButton rbEllipse = new RadioButton("Ellipse");
CheckBox chkFill = new CheckBox("Fill");
paneForRadioButtons.getChildren().addAll(rbCircle, rbRectangle, rbEllipse, chkFill);
paneForRadioButtons.setAlignment(Pos.CENTER);
ToggleGroup group = new ToggleGroup();
rbCircle.setToggleGroup(group);
rbRectangle.setToggleGroup(group);
rbEllipse.setToggleGroup(group);
pane.setBottom(paneForRadioButtons);
StackPane paneForGeometry = new StackPane();
pane.setCenter(paneForGeometry);
circle.setStroke(Color.BLACK);
circle.setFill(Color.WHITE);
rectangle.setStroke(Color.BLACK);
rectangle.setFill(Color.WHITE);
ellipse.setStroke(Color.BLACK);
ellipse.setFill(Color.WHITE);
rbCircle.setOnAction(e -> {
if (rbCircle.isSelected())
paneForGeometry.getChildren().add(circle);
});
rbRectangle.setOnAction(e -> {
if (rbRectangle.isSelected())
paneForGeometry.getChildren().add(rectangle);
});
rbEllipse.setOnAction(e -> {
if (rbEllipse.isSelected())
paneForGeometry.getChildren().add(ellipse);
});
EventHandler<ActionEvent> handler = e -> {
if (rbCircle.isSelected())
circle.setFill(Color.GREEN);
else if (rbRectangle.isSelected())
rectangle.setFill(Color.GREEN);
else
ellipse.setFill(Color.GREEN);
};
chkFill.setOnAction(handler);
return pane;
}
public void start(Stage primaryStage) {
Scene scene = new Scene(getPane(), 450, 200);
primaryStage.setTitle("DrawGeometry");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Application.launch(args);
}
}
运行结果
3、交通信号灯
编写一个程序来模拟交通信号灯。程序可以让用户从红、黄、绿三种顔色灯中 选择一种。当选择一个单选按钮后,相应的灯被打开,并且一次只能亮一种灯(如图所示)。程序开始时所有的灯都是不亮的。
代码
package GUI_Practice;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.RadioButton;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.CheckBox;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Ellipse;
import javafx.scene.shape.Rectangle;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
public class ShowTrafficLight extends Application {
protected Circle circle1 = new Circle(40);
protected Circle circle2 = new Circle(40);
protected Circle circle3 = new Circle(40);
protected BorderPane getPane() {
BorderPane pane = new BorderPane();
HBox paneForRadioButtons = new HBox(20);
paneForRadioButtons.setPadding(new Insets(5, 5, 5, 5));
RadioButton rbRed = new RadioButton("Red");
RadioButton rbYellow = new RadioButton("Yellow");
RadioButton rbGreen = new RadioButton("Green");
paneForRadioButtons.getChildren().addAll(rbRed, rbYellow, rbGreen);
paneForRadioButtons.setAlignment(Pos.CENTER);
pane.setBottom(paneForRadioButtons);
VBox paneForLights = new VBox(10);
paneForLights.setPadding(new Insets(5, 5, 5, 5));
circle1.setStroke(Color.BLACK);
circle1.setFill(Color.WHITE);
circle2.setStroke(Color.BLACK);
circle2.setFill(Color.WHITE);
circle3.setStroke(Color.BLACK);
circle3.setFill(Color.WHITE);
paneForLights.getChildren().addAll(circle1, circle2, circle3);
paneForLights.setAlignment(Pos.CENTER);
pane.setCenter(paneForLights);
rbRed.setOnAction(e -> {
if (rbRed.isSelected())
circle1.setFill(Color.RED);
});
rbYellow.setOnAction(e -> {
if (rbYellow.isSelected())
circle2.setFill(Color.YELLOW);
});
rbGreen.setOnAction(e -> {
if (rbGreen.isSelected())
circle3.setFill(Color.GREEN);
});
return pane;
}
public void start(Stage primaryStage) {
Scene scene = new Scene(getPane(), 400, 400);
primaryStage.setTitle("BorderPane");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Application.launch(args);
}
}
运行结果
4、演示TextField的属性
编写一个程序,动态地设置文本域的水平对齐属性和列宽厲性,如图所示。
代码
package GUI_Practice;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.RadioButton;
public class ShowPropertiesOfTextField extends Application {
protected BorderPane getPane() {
BorderPane pane = new BorderPane();
// 创建放置Text Field及其Label的面板
BorderPane paneForTextField = new BorderPane();
paneForTextField.setPadding(new Insets(5, 5, 5, 5));
paneForTextField.setLeft(new Label("TextField"));
// 创建Text Field
TextField tf = new TextField();
tf.setAlignment(Pos.BOTTOM_RIGHT);
paneForTextField.setCenter(tf);
pane.setTop(paneForTextField);
// 创建放置单选按钮的面板
HBox paneForRadioButtons = new HBox(10);
RadioButton rbLeft = new RadioButton("Left");
RadioButton rbCenter = new RadioButton("Center");
RadioButton rbRight = new RadioButton("Right");
// 设置列宽属性的文本域和标签
Label label = new Label("Column Size");
TextField tfColumnSize = new TextField();
tfColumnSize.setAlignment(Pos.BOTTOM_RIGHT);
paneForRadioButtons.getChildren().addAll(rbLeft, rbCenter, rbRight, label, tfColumnSize);
paneForRadioButtons.setAlignment(Pos.BOTTOM_CENTER);
pane.setBottom(paneForRadioButtons);
rbLeft.setOnAction(e -> tf.setAlignment(Pos.BOTTOM_LEFT));
rbCenter.setOnAction(e -> tf.setAlignment(Pos.BOTTOM_CENTER));
rbRight.setOnAction(e -> tf.setAlignment(Pos.BOTTOM_RIGHT));
tfColumnSize.setOnAction(e -> tf.setPrefColumnCount(Integer.valueOf(tfColumnSize.getText())));
return pane;
}
public void start(Stage primaryStage) {
Scene scene = new Scene(getPane());
primaryStage.setTitle("ShowPropertiesOfTextField");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Application.launch(args);
}
}
运行结果
5、演示TextArea的属性
编写一个程序,演示文本域的属性,程序使用复选框表明文本是否换行,如图所示
代码
package GUI_Practice;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollBar;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.image.ImageView;
import javafx.scene.text.Font;
public class ShowPropertiesOfTextArea extends Application {
public void start(Stage primaryStage) {
BorderPane pane = new BorderPane();
HBox paneForCheckBoxes = new HBox(10);
paneForCheckBoxes.setPadding(new Insets(5, 5, 5, 5));
CheckBox chkEditable = new CheckBox("Editable");
CheckBox chkWrap = new CheckBox("Wrap");
paneForCheckBoxes.getChildren().addAll(chkEditable, chkWrap);
paneForCheckBoxes.setAlignment(Pos.CENTER);
TextArea textArea = new TextArea();
textArea.setEditable(false);
textArea.setWrapText(false);
ScrollPane sbHorizontal = new ScrollPane();
ScrollBar sbVertical = new ScrollBar();
sbVertical.setOrientation(Orientation.VERTICAL);
pane.setCenter(textArea);
pane.setBottom(sbHorizontal);
pane.setRight(sbVertical);
pane.setBottom(paneForCheckBoxes);
chkEditable.setOnAction(e -> textArea.setEditable(true));
chkWrap.setOnAction(e -> textArea.setWrapText(true));
Scene scene = new Scene(pane, 800, 400);
primaryStage.setTitle("ShowPropertiesOfTextArea");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Application.launch(args);
}
}
运行结果
6、选择一种字体
编写一个程序,可以动态地改变堆栈面板上显示的标签中文本的字体。这个
代码
package GUI_Practice;
import GUI_basis.DescriptionPane;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.text.Text;
import javafx.scene.control.Label;
import javafx.scene.control.ComboBox;
import javafx.scene.image.ImageView;
import javafx.scene.text.Font;
public class CheckFont extends Application {
private Text text = new Text("Programming is fun");
private String[] fontNames = { "SimSun", "SimHei", "Microsoft YaHei", "KaiTi", "LiSu" };
private String[] fontSize = new String[50];
private ComboBox<String> cbo1 = new ComboBox<>();
private ComboBox<String> cbo2 = new ComboBox<>();
private DescriptionPane descriptionPane = new DescriptionPane();
public void start(Stage primaryStage) {
BorderPane pane = new BorderPane();
HBox paneForComboBox = new HBox(10);
Label lblFontName = new Label("Font Name");
Label lblFontSize = new Label("Font Size");
paneForComboBox.getChildren().addAll(lblFontName, cbo1, lblFontSize, cbo2);
pane.setTop(paneForComboBox);
cbo1.setPrefWidth(200);
cbo1.setValue("SimSun");
cbo2.setPrefWidth(50);
cbo2.setValue("20");
for (int i = 0; i < 50; i++)
fontSize[i] = (i + 20) + "";
ObservableList<String> items1 = FXCollections.observableArrayList(fontNames);
cbo1.getItems().addAll(items1);
pane.setCenter(text);
cbo1.setOnAction(e -> setDisplay1(items1.indexOf(cbo1.getValue())));
ObservableList<String> items2 = FXCollections.observableArrayList(fontSize);
cbo2.getItems().addAll(items2);
cbo2.setOnAction(e -> setDisplay2(items2.indexOf(cbo2.getValue())));
Scene scene = new Scene(pane, 500, 200);
primaryStage.setTitle("CheckFont");
primaryStage.setScene(scene);
primaryStage.show();
}
public void setDisplay1(int index) {
Font font = new Font(fontNames[index], text.getFont().getSize());
text.setFont(font);
}
public void setDisplay2(int index) {
text.setFont(new Font(Double.valueOf(fontSize[index])));
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Application.launch(args);
}
}
运行结果
7、演示 Label 的属性
编 写 一 个 程 序,允 许 用 户 动 态 地 设 置 属 性 contentDisplay和 graphicTextCap, 如图所示。
代码
package GUI_Practice;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollBar;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.image.ImageView;
import javafx.scene.text.Font;
public class ShowPropertiesOfLabel extends Application {
public void start(Stage primaryStage) {
BorderPane pane = new BorderPane();
HBox hbox = new HBox(10);
hbox.setPadding(new Insets(5, 5, 5, 5));
ComboBox<String> cbo = new ComboBox<>();
TextField tf = new TextField();
hbox.getChildren().addAll(new Label("contentDisplay"), cbo, new Label("graphicTextGap"), tf);
pane.setTop(hbox);
Label lbl = new Label("grapes", new ImageView("image/grapes.jpg"));
lbl.setStyle("-fx-border-color:purple;-fx-border-width:2");
lbl.setContentDisplay(ContentDisplay.LEFT);
pane.setCenter(lbl);
String[] contentDisplay = { "LEFT", "RIGHT", "CENTER" };
ObservableList<String> items = FXCollections.observableArrayList(contentDisplay);
cbo.getItems().addAll(items);
cbo.setOnAction(e -> {
if (cbo.getValue() == "LEFT")
lbl.setContentDisplay(ContentDisplay.LEFT);
if (cbo.getValue() == "RIGHT")
lbl.setContentDisplay(ContentDisplay.RIGHT);
if (cbo.getValue() == "CENTER")
lbl.setContentDisplay(ContentDisplay.CENTER);
});
tf.setOnAction(e -> lbl.setGraphicTextGap(Double.valueOf(tf.getText())));
Scene scene = new Scene(pane);
primaryStage.setTitle("ShowPropertiesOfLabel");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Application.launch(args);
}
}
运行结果
8、使 用ComboBox 和 ListView
![Java UI组件和多媒体](https://imgs.yssmx.com/Uploads/2023/06/484670-15.png)
代码
package GUI_Practice;
import GUI_basis.DescriptionPane;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Pos;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.FlowPane;
import javafx.scene.text.Text;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.SelectionMode;
import javafx.scene.image.ImageView;
import javafx.scene.text.Font;
public class UseComboBoxAndListView extends Application {
public void start(Stage primaryStage) {
BorderPane pane = new BorderPane();
HBox hbox = new HBox(10);
ComboBox<String> cbo = new ComboBox<>();
String[] selectionNode = { "MULTIPLE", "SINGLE" };
ObservableList<String> items = FXCollections.observableArrayList(selectionNode);
cbo.getItems().addAll(items);
hbox.getChildren().addAll(new Label("Choose Selection Node:"), cbo);
hbox.setAlignment(Pos.CENTER);
pane.setTop(hbox);
String[] countries = { "Canada", "China", "Denmark", "France", "Germany", "India" };
ListView<String> lv = new ListView<>(FXCollections.observableArrayList(countries));
lv.setPrefSize(400, 400);
pane.setCenter(lv);
Text text = new Text("Selected iterms are");
pane.setBottom(text);
lv.getSelectionModel().selectedItemProperty().addListener(ov -> {
if (cbo.getValue() == "MULTIPLE")
for (Integer i : lv.getSelectionModel().getSelectedIndices())
text.setText(text.getText() + " " + countries[i] + " ");
else {
text.setText("Selected iterms are ");
for (Integer i : lv.getSelectionModel().getSelectedIndices())
text.setText(text.getText() + countries[i]);
}
});
Scene scene = new Scene(pane);
primaryStage.setTitle("UseComboBoxAndListView");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Application.launch(args);
}
}
运行结果
9、使 用 ScrollBar 和 Slider
代码
package GUI_Practice;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.geometry.Orientation;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.paint.*;
public class ShowColors extends Application {
public void start(Stage primaryStage) {
VBox pane = new VBox(20);
HBox hbox = new HBox();
Text text = new Text("Show Color");
text.setFont(new Font(30));
hbox.getChildren().add(text);
hbox.setAlignment(Pos.CENTER);
pane.getChildren().add(hbox);
GridPane paneForSliders = new GridPane();
paneForSliders.setAlignment(Pos.CENTER);
Slider sldRed = new Slider();
Slider sldGreen = new Slider();
Slider sldBlue = new Slider();
Slider sldOpadty = new Slider();
Label lb1 = new Label("Red");
lb1.setFont(new Font(20));
Label lb2 = new Label("Green");
lb2.setFont(new Font(20));
Label lb3 = new Label("Blue");
lb3.setFont(new Font(20));
Label lb4 = new Label("Opacity");
lb4.setFont(new Font(20));
paneForSliders.add(lb1, 0, 0);
paneForSliders.add(sldRed, 1, 0);
paneForSliders.add(lb2, 0, 1);
paneForSliders.add(sldGreen, 1, 1);
paneForSliders.add(lb3, 0, 2);
paneForSliders.add(sldBlue, 1, 2);
paneForSliders.add(lb4, 0, 3);
paneForSliders.add(sldOpadty, 1, 3);
pane.getChildren().add(paneForSliders);
sldRed.valueProperty().addListener(ov -> {
Color color = (Color) text.getFill();
text.setFill(new Color(sldRed.getValue(), color.getGreen(), color.getBlue(), color.getOpacity()));
});
sldGreen.valueProperty().addListener(ov -> {
Color color = (Color) text.getFill();
text.setFill(new Color(color.getRed(), sldGreen.getValue(), color.getBlue(), color.getOpacity()));
});
sldBlue.valueProperty().addListener(ov -> {
Color color = (Color) text.getFill();
text.setFill(new Color(color.getRed(), color.getGreen(), sldBlue.getValue(), color.getOpacity()));
});
sldOpadty.valueProperty().addListener(ov -> {
Color color = (Color) text.getFill();
text.setFill(new Color(color.getRed(), color.getGreen(), color.getBlue(), sldOpadty.getValue()));
});
Scene scene = new Scene(pane, 300, 200);
primaryStage.setTitle("ShowColors");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Application.launch(args);
}
}
运行结果
10、模拟:一个转动的风扇
增加一个滑动条控制风扇的速度,如图所示
代码
实在不会了啊啊啊啊,先这样吧,搞了半天了,剩下的下次
ControlFans
package GUI_Practice;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.stage.Stage;
import javafx.util.Duration;
import javafx.geometry.Orientation;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Slider;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Arc;
import javafx.scene.shape.ArcType;
import javafx.scene.shape.Circle;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;
public class ControlFans extends Application {
public void start(Stage primaryStage) {
BorderPane borderPane = new BorderPane();
HBox hbox1 = new HBox(20);
FanBorderPane pane1 = new FanBorderPane(), pane2 = new FanBorderPane(), pane3 = new FanBorderPane();
hbox1.getChildren().addAll(pane1, pane2, pane3);
borderPane.setTop(hbox1);
HBox hbox2 = new HBox(10);
hbox2.setAlignment(Pos.CENTER);
Button btStart = new Button("Start All");
Button btStop = new Button("Stop All");
hbox2.getChildren().addAll(btStart, btStop);
borderPane.setBottom(hbox2);
// EventHandler<ActionEvent> handler1 = e -> {
//
// };
//
// Timeline timeline = new Timeline(new KeyFrame(Duration.millis(20), handler1));
// timeline.setCycleCount(Timeline.INDEFINITE);
// timeline.play();
//
// btStop.setOnMouseClicked(e -> timeline.play());
// btStart.setOnMouseClicked(e -> timeline.pause());
Scene scene = new Scene(borderPane);
primaryStage.setTitle("ControlFans");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Application.launch(args);
}
}
FanPane文章来源:https://www.toymoban.com/news/detail-484670.html
package GUI_Practice;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Arc;
import javafx.scene.shape.ArcType;
import javafx.scene.shape.Circle;
public class FanPane extends Pane {
protected Circle circle = new Circle(100, 70, 60);;
protected Arc[] arc = new Arc[4];
protected double startAngle = 30;
protected double increment = 5;
public FanPane() {
circle.setStroke(Color.BLACK);
circle.setFill(Color.WHITE);
getChildren().add(circle);
for (int i = 0; i < 4; i++) {
arc[i] = new Arc(100, 70, 50, 50, startAngle + 90 * i, 30);
arc[i].setType(ArcType.ROUND);
arc[i].setFill(Color.BLACK);
getChildren().add(arc[i]);
}
}
public void setIncrement(double i) {
increment = i;
}
public void setAngle(double angle) {
startAngle = angle;
for (int i = 0; i < 4; i++)
arc[i].setStartAngle(startAngle + 90 * i);
}
public void resume() {
setAngle(startAngle + increment);
}
public void reverse() {
increment *= -1;
}
}
FanBorderPane文章来源地址https://www.toymoban.com/news/detail-484670.html
package GUI_Practice;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.Slider;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.util.Duration;
public class FanBorderPane extends BorderPane {
protected FanPane pane = new FanPane();
protected HBox hbox = new HBox();
protected Timeline timeline;
public FanBorderPane() {
hbox.setSpacing(10);
hbox.setAlignment(Pos.CENTER);
Button btPause = new Button("Pause");
Button btResume = new Button("Resume");
Button btReverse = new Button("Reverse");
hbox.getChildren().addAll(btPause, btResume, btReverse);
timeline = new Timeline(new KeyFrame(Duration.millis(20), e -> pane.resume()));
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.play();
btPause.setOnAction(e -> timeline.pause());
btResume.setOnAction(e -> timeline.play());
btReverse.setOnAction(e -> pane.reverse());
Slider sld = new Slider();
sld.valueProperty().addListener(ov -> {
pane.setIncrement(sld.getValue());
});
setTop(pane);
setCenter(hbox);
setBottom(sld);
BorderPane.setAlignment(hbox, Pos.CENTER);
}
}
到了这里,关于Java UI组件和多媒体的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!