Java UI组件和多媒体

这篇具有很好参考价值的文章主要介绍了Java UI组件和多媒体。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

目录

1、使用单选按钮

2、选择几何图形

 3、交通信号灯

 4、演示TextField的属性

5、演示TextArea的属性

6、选择一种字体

 7、演示 Label 的属性

 8、使 用ComboBox 和 ListView 

9、使 用 ScrollBar 和 Slider  

 10、模拟:一个转动的风扇


1、使用单选按钮

编写一个 GUI 程序如图所示可以使用按钮将消息进行左右移动并且使用单选按钮来修改消息显示的颜色。

Java UI组件和多媒体

 代码

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);
	}

}

运行结果

Java UI组件和多媒体

2、选择几何图形

编写一个绘制各种几何图形的程序如图所示用户从单选按钮中选择 一个几何图形,并且使用复选框指定是否被填充

 Java UI组件和多媒体

 代码

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);
	}

}

运行结果

Java UI组件和多媒体

 3、交通信号灯

编写一个程序来模拟交通信号灯程序可以让用户从红绿三种顔色灯中 选择一种。当选择一个单选按钮后相应的灯被打开并且一次只能亮一种灯如图所示)程序开始时所有的灯都是不亮的

Java UI组件和多媒体

 代码

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);
	}

}

运行结果

Java UI组件和多媒体

 4、演示TextField的属性

编写一个程序动态地设置文本域的水平对齐属性和列宽厲性如图所示。

Java UI组件和多媒体

 代码

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);
	}

}

运行结果

Java UI组件和多媒体

5、演示TextArea的属性

编写一个程序演示文本域的属性,程序使用复选框表明文本是否换行,如图所示

 Java UI组件和多媒体

 代码

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);
	}

}

运行结果

Java UI组件和多媒体

6、选择一种字体

编写一个程序可以动态地改变堆栈面板上显示的标签中文本的字体这个

消息可以同时以粗体和斜体显示 可以从组合框中选择字体名和字体大小 如图 所示。 使用 Font .getFamilies()  可以得到可用的宇体名 。字体大小的组合框初始化为从 1到 100 之间的数字。

 Java UI组件和多媒体

 代码

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);
	}

}

运行结果

Java UI组件和多媒体

 7、演示 Label 的属性

 编 写 一 个 程 序,允 许 用 户 动 态 地 设 置 属 性 contentDisplay和 graphicTextCap, 如图所示。

Java UI组件和多媒体

 代码

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);
	}

}

运行结果

Java UI组件和多媒体

 8、使 用ComboBox 和 ListView 

编写一个程序,演示在列表中选择的条目 。程序用组合框指定 选择方式, 如图 所示 。当选择条目后,列表下方的标签中就会显示选定项
Java UI组件和多媒体

 代码

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);
	}

}

运行结果

Java UI组件和多媒体

9、使 用 ScrollBar 和 Slider  

编写一个程序,使用滚动条或者滑动条选择文本的颜色,如图所示 使用四个水平滚动条选择颜色 红色 绿色和蓝色 ) 以及透明度的百分比

 Java UI组件和多媒体

 代码

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);
	}

}

运行结果

Java UI组件和多媒体

 10、模拟:一个转动的风扇

增加一个滑动条控制风扇的速度如图所示

Java UI组件和多媒体

 代码

实在不会了啊啊啊啊,先这样吧,搞了半天了,剩下的下次

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

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模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Android多媒体功能开发(11)——使用AudioRecord类录制音频

    AudioRecord类优点是能录制到缓冲区,能够实现边录边播(AudioRecord + AudioTrack)以及对音频的实时处理(如QQ电话)。缺点是输出是PCM格式的原始采集数据,如果直接保存成音频文件,不能够被播放器播放,所以必须用代码实现数据编码以及压缩。 使用AudioRecord录音的基本步骤是

    2023年04月09日
    浏览(28)
  • 软考:中级软件设计师:多媒体基础,音频,图像,颜色,多媒体技术的种类,图像音频视频的容量计算,常见的多媒体标准

    提示:系列被面试官问的问题,我自己当时不会,所以下来自己复盘一下,认真学习和总结,以应对未来更多的可能性 关于互联网大厂的笔试面试,都是需要细心准备的 (1)自己的科研经历, 科研内容 ,学习的相关领域知识,要熟悉熟透了 (2)自己的实习经历,做了 什

    2024年02月09日
    浏览(36)
  • 参会记录|全国多媒体取证暨第二届多媒体智能安全学术研讨会(MAS‘2023)

    前言 :2023年4月8日上午,我与实验室的诸位伙伴们共聚浙江杭州西子湖畔的六通宾馆,参加了为期一天半的全国多媒体取证暨第二届多媒体智能安全学术研讨会(MAS’2023)。本届学术研讨会由浙江省自然科学基金委员会资助,杭州电子科技大学承办。来自国内多媒体取证与

    2024年02月08日
    浏览(37)
  • 多媒体API

    许小墨のBlog —— 菜鸡博客直通车 系列文章完整版,配图更多,CSDN博文图片需要手动上传,因此文章配图较少,看不懂的可以去菜鸡博客参考一下配图! 前端系列文章——传送门 后端系列文章——传送门 video 只接受几种视屏格式:ogg、mp4、avi 基本使用: controls属性,出现

    2024年02月02日
    浏览(43)
  • 多媒体音频焦点浅析

    多个音源可以同时向同一个输出流进行播放音频,如果没有音频焦点管控,就会出现多个音源同时播放的现象,给用户带来不便;而Android为了避免多个音源同时播放,就引入了音频焦点的概念,所有音频应用都统一按照音频焦点的规定执行,就可以避免该现象发生。 当应用

    2024年02月13日
    浏览(25)
  • 多媒体开发之cgo

         go语言作为近十年来优秀的现代开发语言的代表,由于继承了c语言的简洁和很多现代语言的表达方式,在广泛的应用场景中得到众多爱好者的喜爱,如何将go和c、c++进行联合开发,拓展整个开发生态,不用重复造轮子,掌握cgo可以让你得心应手的在c和go之间传递信息,

    2024年02月16日
    浏览(31)
  • AIGC生成多媒体流程

    给定 生成多个故事标题 多个故事标题进行反向推导出 再生成标题 直到达到一个相似度 多个标题固定总结合并为一个标题 根据生成故事多个章节标题 多个章节标题反向生成一个标题 对比前后两个标题相似度 不断重复直到达到一定相似度 第一个章

    2024年02月12日
    浏览(30)
  • 计算机网络——多媒体网络

    通俗易懂,风趣幽默,忍不住分享一下给大家, 跳转到网站 我的计算机网络专栏,是自己在计算机网络学习过程中的学习笔记与心得,在参考相关教材,网络搜素的前提下,结合自己过去一段时间笔记整理,而推出的该专栏,整体架构是根据计算机网络 自顶向下 方法而整理

    2024年02月20日
    浏览(25)
  • HTML5多媒体单元测试

    (单选题, 10.0分) 为元素指定多个视频源使用( )标签(元素)。 A select B datalist C source D src (单选题, 10.0分) 判断浏览器是否支持指定的媒体类型需用到audio或video对象的( )方法。 A load() B play() C pause() D canPlayType() (多选题, 10.0分) HTML5新增了强大的多媒体的功能,主要体现在

    2024年02月04日
    浏览(30)
  • 多媒体网络教学模式的评价方式

    传统教学评价只注重学生掌握的基本知识,忽视其他各方面的技能,已经不能 满足多媒体网络教学模式下的评价要求。建构主义指导下的评价方式也应遵循以学 习者为中心的原则,强调合作学习,体现学习过程的重要性。因此,评价方式的多 元化必然会取代传统单一的评价

    2024年02月11日
    浏览(29)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包