JavaFX作业[重复]
Posted
技术标签:
【中文标题】JavaFX作业[重复]【英文标题】:JavaFX Homework [duplicate] 【发布时间】:2016-03-04 13:52:01 【问题描述】:希望完成这项作业。我有一个程序将显示多达 5 个“FanPanes”,这些“FanPanes”将使风扇旋转。您可以使用滑块来控制风扇的速度,并使用按钮来停止、播放和反转风扇的方向。
我在弄清楚如何让我的滑块(“slider2”是我想从 FanPane 类中使用它)更新风扇显示的叶片数量时遇到问题。我希望能够拥有从 1 个刀片到 10 个刀片的任何位置。
另外,我在让我的粉丝正确排列时遇到了问题。我之前是正确的,现在我添加了这些新滑块,我的刀片在我的圈子内没有正确排列。
这是我的代码:
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Arc;
import javafx.scene.shape.ArcType;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Duration;
import javafx.scene.control.Slider;
import javafx.scene.layout.StackPane;
import javafx.scene.text.*;
public class FanWithControls extends Application
@Override // Override the start method in the Application class
public void start(Stage primaryStage)
FanPane fan = new FanPane();
FlowPane spane = new FlowPane(5, 5);
spane.getChildren().addAll(fan);
HBox hBox = new HBox(5);
GridPane botPane = new GridPane();
Label numFans = new Label("Num Fans: ");
Label blades = new Label(" Blades: ");
Label total = new Label("Control All Fans: ");
Button btPause = new Button("Pause");
Button btResume = new Button("Resume");
Button btReverse = new Button("Reverse");
hBox.setAlignment(Pos.CENTER);
hBox.getChildren().addAll(btPause, btResume, btReverse);
Slider slider = new Slider(0, 5, 0);
Slider bSlider = new Slider(0, 10,0);
slider.setShowTickLabels(true);
slider.setShowTickMarks(true);
bSlider.setShowTickLabels(true);
bSlider.setShowTickMarks(true);
botPane.getChildren().addAll(numFans, slider, blades, bSlider, total);
botPane.setConstraints(total, 0, 0);
botPane.setConstraints(numFans, 3, 0);
botPane.setConstraints(slider, 4, 0);
botPane.setConstraints(blades, 6, 0);
botPane.setConstraints(bSlider, 7, 0);
BorderPane pane = new BorderPane();
pane.setPadding(new Insets(10, 30, 12, 12));
pane.setCenter(spane);
pane.setTop(hBox);
pane.setBottom(botPane);
// Create a scene and place it in the stage
Scene scene = new Scene(pane, 1200, 400);
primaryStage.setTitle("FanWithControls"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
fan.setAnimation(fan);
Timeline animation = fan.getAnimation();
//scene.widthProperty().addListener(e -> fan.setW(fan.getWidth()));
//scene.heightProperty().addListener(e -> fan.setH(fan.getHeight()));
spane.setHgap(5);
btPause.setOnAction((e) ->
for (Node fans : spane.getChildren())
FanPane fanpane = (FanPane) fans;
fanpane.getAnimation().pause();
// animation.pause();
);
btResume.setOnAction((e) ->
for (Node fans : spane.getChildren())
FanPane fanpane = (FanPane) fans;
fanpane.getAnimation().play();
// animation.play();
);
btReverse.setOnAction((e) ->
for (Node fans : spane.getChildren())
FanPane fanpane = (FanPane) fans;
fanpane.reverse();
// fan.reverse();
);
slider.valueProperty().addListener((ov) ->
// animation.setRate(slider.getValue());
if (spane.getChildren().size() < (int) slider.getValue())
for (int i = spane.getChildren().size(); i < (int) slider.getValue(); i++)
FanPane fanPane = new FanPane();
spane.getChildren().add(fanPane);
fanPane.setAnimation(fanPane);
fanPane.rate();
else if (spane.getChildren().size() > (int) slider.getValue())
for (int i = (int) slider.getValue(); i < spane.getChildren().size(); i++)
spane.getChildren().remove(spane.getChildren().size() - 1);
);
/**
* The main method is only needed for the IDE with limited JavaFX support.
* Not needed for running from the command line.
*/
public static void main(String[] args)
launch(args);
class FanPane extends Pane
private double w = 200;
private double h = 200;
private double radius = Math.min(w, h) * 0.45;
private Arc arc[] = new Arc[4];
private double startAngle = 30;
private Circle circle = new Circle(w / 2, h / 2, radius);
private Timeline animation;
private Slider slider1 = new Slider(0, 15, 0);
private Slider slider2 = new Slider(0, 10, 0);
private BorderPane bpane = new BorderPane();
private Button pauseBt = new Button("Pause");
private Button resumeBt = new Button("Resume");
private Button reverseBt = new Button("Reverse");
private HBox btBox = new HBox(5);
private Label blank = new Label(" ");
private Label speed = new Label("Fan Speed: ");
private Label numBlades = new Label("Num Blades: ");
private VBox topBox = new VBox();
private VBox midBox = new VBox();
private GridPane gpane = new GridPane();
public FanPane()
circle.setStroke(Color.BLUE);
circle.setFill(Color.WHITE);
circle.setStrokeWidth(4);
getChildren().add(bpane);
slider1.setShowTickLabels(true);
slider1.setShowTickMarks(true);
slider1.setMajorTickUnit(5);
slider1.setMinorTickCount(0);
slider1.setBlockIncrement(1);
slider2.setShowTickLabels(true);
slider2.setShowTickMarks(true);
slider2.setMajorTickUnit(5);
slider2.setMinorTickCount(0);
slider2.setBlockIncrement(1);
gpane.setConstraints(speed, 0, 0);
gpane.setConstraints(slider1, 1, 0);
gpane.setConstraints(numBlades, 0, 1);
gpane.setConstraints(slider2, 1, 1);
gpane.getChildren().addAll(speed, slider1, numBlades, slider2);
btBox.getChildren().addAll(pauseBt,resumeBt,reverseBt);
topBox.getChildren().addAll(blank, btBox);
bpane.setCenter(circle);
bpane.setTop(topBox);
bpane.setBottom(gpane);
bpane.setAlignment(topBox, Pos.CENTER);
pauseBt.setOnAction(e -> animation.pause());
resumeBt.setOnAction(e -> animation.play());
reverseBt.setOnAction(e -> this.reverse());
bpane.widthProperty().addListener(e -> this.setW(this.getWidth()));
bpane.heightProperty().addListener(e -> this.setH(this.getHeight()));
for (int i = 0; i < 4; i++)
arc[i] = new Arc(w / 2, h / 2, radius * 0.9, radius * 0.9, startAngle + i * 90, 35);
arc[i].setFill(Color.RED); // Set fill color
arc[i].setType(ArcType.ROUND);
getChildren().addAll(arc[i]);
public Timeline getAnimation()
return animation;
public void setAnimation(FanPane fan)
this.animation = new Timeline(new KeyFrame(Duration.millis(50), e -> fan.move()));
animation.setCycleCount(Timeline.INDEFINITE);
animation.play();
private double increment = 5;
public void reverse()
increment = -increment;
public void rate()
slider1.valueProperty().addListener(ov -> animation.setRate(slider1.getValue()));
public void move()
setStartAngle(startAngle + increment);
public void setStartAngle(double angle)
startAngle = angle;
setValues();
public void setValues()
radius = Math.min(w, h) * 0.45;
circle.setRadius(radius);
circle.setCenterX(w / 2);
circle.setCenterY(h / 2);
for (int i = 0; i < 4; i++)
arc[i].setRadiusX(radius * 0.9);
arc[i].setRadiusY(radius * 0.9);
arc[i].setCenterX(w / 2);
arc[i].setCenterY(h / 2);
arc[i].setStartAngle(startAngle + i * 90);
public void setW(double w)
this.w = w;
setValues();
public void setH(double h)
this.h = h;
setValues();
public double getCenterX()
return circle.getCenterX();
public double getCenterY()
return circle.getCenterY();
public double getRadius()
return circle.getRadius();
【问题讨论】:
这与您的previous question 有何不同? 我将我的旧问题标记为已回答,但我在个人资料中没有看到它,所以我又做了一个。在我实现了上一个问题的帮助之后,我开始在我的程序中添加更多东西。在我为单个风扇速度添加单独的滑块后,我的风扇叶片不再正确排列。另外,我想弄清楚如何添加一个滑块来改变风扇内的叶片数量。我试图添加一个滑块来更改我的 for 循环中的整数,并且数组会起作用,但它肯定不会。 您需要了解如何制作 4 叶片扇形窗格。将这些计算通用化以制作 n 叶片扇形。 您的粉丝不在圈子中,因为您添加了arc
,这使您的粉丝直接进入了“窗格”,但是您将“圈子”添加到了“borderPane”中然后添加到“窗格”中。尝试在新的Pane
中添加圆和弧,然后将pane
放入borderpane
我现在明白了,非常感谢!我的粉丝又排好队了!
【参考方案1】:
增加编号。刀片的动态,您需要更改编号。您正在制作的弧线,其长度和角度差异。
根据以下代码更改FanPane
的构造函数。
public FanPane(int blade)
circle.setStroke(Color.BLUE);
circle.setFill(Color.WHITE);
circle.setStrokeWidth(4);
getChildren().add(bpane);
slider1.setShowTickLabels(true);
slider1.setShowTickMarks(true);
slider1.setMajorTickUnit(5);
slider1.setMinorTickCount(0);
slider1.setBlockIncrement(1);
slider2.setShowTickLabels(true);
slider2.setShowTickMarks(true);
slider2.setMajorTickUnit(5);
slider2.setMinorTickCount(0);
slider2.setBlockIncrement(1);
gpane.setConstraints(speed, 0, 0);
gpane.setConstraints(slider1, 1, 0);
gpane.setConstraints(numBlades, 0, 1);
gpane.setConstraints(slider2, 1, 1);
gpane.getChildren().addAll(speed, slider1, numBlades, slider2);
btBox.getChildren().addAll(pauseBt, resumeBt, reverseBt);
topBox.getChildren().addAll(blank, btBox);
Pane pane = new Pane(circle);
bpane.setCenter(pane);
bpane.setTop(topBox);
bpane.setBottom(gpane);
// bpane.setAlignment(topBox, Pos.CENTER);
pauseBt.setOnAction(e -> animation.pause());
resumeBt.setOnAction(e -> animation.play());
reverseBt.setOnAction(e -> this.reverse());
// bpane.widthProperty().addListener(e -> this.setW(this.getWidth()));
// bpane.heightProperty().addListener(e -> this.setH(this.getHeight()));
try
arc = new Arc[blade];
int angleDiff = 360 / blade;
for (int i = 0; i < blade; i++)
arc[i] = new Arc(w / 2, h / 2, radius * 0.9, radius * 0.9, startAngle + i * angleDiff, 180 / blade);
arc[i].setFill(Color.RED); // Set fill color
arc[i].setType(ArcType.ROUND);
pane.getChildren().addAll(arc[i]);
catch (ArithmeticException ex)
blade = 2;
arc = new Arc[blade];
int angleDiff = 360 / blade;
for (int i = 0; i < blade; i++)
arc[i] = new Arc(w / 2, h / 2, radius * 0.9, radius * 0.9, startAngle + i * angleDiff, 180 / blade);
arc[i].setFill(Color.RED); // Set fill color
arc[i].setType(ArcType.ROUND);
pane.getChildren().addAll(arc[i]);
【讨论】:
以上是关于JavaFX作业[重复]的主要内容,如果未能解决你的问题,请参考以下文章
即使在eclipse中安装javafx后我也无法运行javafx程序[重复]