首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何做一个干净的代码,但结果与我的代码相同?javafx hbox循环数组

如何做一个干净的代码,但结果与我的代码相同?javafx hbox循环数组
EN

Stack Overflow用户
提问于 2016-03-15 02:38:04
回答 1查看 326关注 0票数 0

有6个按钮,每个按钮都有一个OnMouseEntered处理程序,将按钮的文本更改为“单击我!”还有一个OnMouseExited处理程序,它将按钮的文本更改为原始文本。

是否有任何方法可以使代码尽可能干净?怎么做呢?非常感谢!有大量重复的代码:

代码语言:javascript
复制
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.layout.*;
import javafx.stage.Stage;

public class MyGUI extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        HBox root = new HBox();
        VBox vb = new VBox();
        Button[] btn = new Button[6];
        for (int i = 0; i < 6; i++) {
            btn[i] = new Button("Button" + (i + 1));
        }

        vb.getChildren().addAll(btn);
        root.getChildren().addAll(lv, vb);

        btn[0].setOnMouseEntered(e -> {
            btn[0].setText("Click Me!");
        });
        btn[1].setOnMouseEntered(e -> {
            btn[1].setText("Click Me!");
        });
        btn[2].setOnMouseEntered(e -> {
            btn[2].setText("Click Me!");
        });
        btn[3].setOnMouseEntered(e -> {
            btn[3].setText("Click Me!");
        });
        btn[4].setOnMouseEntered(e -> {
            btn[4].setText("Click Me!");
        });
        btn[5].setOnMouseEntered(e -> {
            btn[5].setText("Click Me!");
        });
        btn[0].setOnMouseExited(e -> {
            btn[0].setText("Button1");
        });
        btn[1].setOnMouseExited(e -> {
            btn[1].setText("Button2");
        });
        btn[2].setOnMouseExited(e -> {
            btn[2].setText("Button3");
        });
        btn[3].setOnMouseExited(e -> {
            btn[3].setText("Button4");
        });
        btn[4].setOnMouseExited(e -> {
            btn[4].setText("Button5");
        });
        btn[5].setOnMouseExited(e -> {
            btn[5].setText("Button6");
        });

        Scene MScene = new Scene(root, 800, 600);
        primaryStage.setTitle("My Button"); 
        primaryStage.setScene(MScene);
        primaryStage.show();
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-15 04:50:24

有不同的解决方案取决于需求,例如:

代码语言:javascript
复制
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class MyGUI extends Application {

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

        HBox root = new HBox();
        VBox vbox = new VBox();
        Button[] buttons = new Button[6];

        for (int i = 0; i < 6; i++) {

            String text = "Button" + (i + 1);

            Button button = new Button(text);

            button.setOnMouseEntered( e -> button.setText("Click Me!"));
            button.setOnMouseExited( e -> button.setText(text));

            buttons[i] = button;

        }

        vbox.getChildren().addAll(buttons);
        root.getChildren().addAll(vbox);

        Scene scene = new Scene(root, 800, 600);

        primaryStage.setTitle("My Button");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36001705

复制
相关文章

相似问题

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