首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用JavaFX中的按钮更改行的大小

如何使用JavaFX中的按钮更改行的大小
EN

Stack Overflow用户
提问于 2016-12-24 16:11:18
回答 1查看 233关注 0票数 0

我想用一个按钮来改变一条线的大小,这样以后我就可以让这条线看起来像在旋转……下面是我到目前为止掌握的代码:

代码语言:javascript
复制
package JavaFXApplication14;

import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.stage.Stage;

public class JavaFXApplication14 extends Application 
{
    public static void main(String[] args) 
    {
        launch(args);
    }

    int x = 200;

    @Override public void start(Stage primaryStage) throws Exception 
    {
        final GridPane grid = new GridPane();
        grid.setAlignment(Pos.CENTER); 
        grid.setHgap(100);
        grid.setVgap(100);
        grid.setPadding(new Insets(10, 10, 10, 10));

        Scene scene = new Scene(grid, 600, 400); //Color.BLACK ?
        primaryStage.setScene(scene);
        primaryStage.setTitle("4D");
        primaryStage.show();

        Line ln = new Line(100, 200, x, 200); 
        ln.setStroke(Color.BLUE);
        ln.setStrokeWidth(5);
        grid.add(ln, 0, 0);

        Button btn = new Button("X-Y");
        grid.setHalignment(btn, HPos.CENTER);
        btn.setOnAction(e -> btn_Click());
        grid.add(btn, 0, 1);
    }

    public void btn_Click()
    {
        x = x + 50;
    }
}

另外,有时当我使用下面的代码行时,背景的颜色不会改变。

代码语言:javascript
复制
Scene scene = new Scene(grid, 600, 400, Color.BLACK);

原因是什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-12-24 16:43:33

这个按钮工作得很好,您可以测试它,但是这里您只为x设置了一个新值,没有其他任何东西,换句话说,您不更新Line.You的位置( x2 )可以通过使变量ln可访问并更新其值EndX来标记它。

代码语言:javascript
复制
//before the method start
Line ln;
//Inside your btn_click() method add
ln.setEndX(x);

但它只会增加线的大小(水平),而不是旋转它。通过一些研究解释了here,并按照我在评论中告诉您的,您可以根据旋转轴(startX & startY)和要旋转的点(endX &endY)轻松地完成这一任务:

代码语言:javascript
复制
public class Launcher extends Application{

private Pane root = new Pane();
private Scene scene;
private Button btn = new Button("Test");
private Line line;

@Override
public void start(Stage stage) throws Exception {

    line = new Line(200,200,200,100);
    line.setStroke(Color.BLACK);
    line.setStrokeWidth(5);  
    btn.setOnAction(e -> rotate()); 
    root.getChildren().addAll(btn,line);
    scene = new Scene(root,500,500);
    stage.setScene(scene);
    stage.show();

}


private void rotate(){


    double x1 = line.getEndX() - line.getStartX();
    double y1 = line.getEndY() - line.getStartY();

    //The more you reduce the angle the longer the rotation
    double x2 = x1 * Math.cos(0.1) - y1 * Math.sin(0.1); 
    double y2 = x1 * Math.sin(0.1) + y1 * Math.cos(0.1);

    line.setEndX(x2+ line.getStartX());
    line.setEndY(y2+ line.getStartY()); 

}


  public static void main(String[] args) {

    launch(args); 

  }
}

注意:在您的示例中,您使用了一个GridPane**,,因此您将被限制在它的功能上,祝您好运!**

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41314840

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档