javafx 使用教程 软件界面设计

首先安装:

SceneBuilder-8.5.0.exe

汉化: SceneBuilderChinesization-master

配置 idea 开发环境

安装好 JavaFX Scene Builder 2.0 后,需要在 idea 中设置这个执行路径

mian:

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 300, 275));
        primaryStage.show();
    }


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

JavaFX极简入门(利用FXML文件)


JavaFX极简入门(利用FXML文件)到精通_白糖炒栗子~的博客-CSDN博客_fxml

import javafx.application.Application;


public class HelloApplication extends Application {
    static private Stage primaryStage;
    @Override
    public void start(Stage primaryStage) throws IOException {
        this.primaryStage=primaryStage;
        primaryStage.setTitle("标题");
        Parent root=FXMLLoader.load(getClass().getResource("hello-view2.fxml"));
        Scene scene=new Scene(root,850,600);
        primaryStage.setScene(scene);
        primaryStage.show();

    }

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

ChoiceBox添加元素

top_choiceBox.getItems().addAll("审计","程序","文件","注册","保护");

top_choiceBox.getSelectionModel().selectFirst();//默认选中第一个选项

选择元素时执行:

                String st[] = { "默认排序",};
                chocebox.getItems().addAll(st);
                chocebox.getSelectionModel().selectFirst();
                chocebox.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>() {

                        // if the item of the list is changed
                        public void changed(ObservableValue ov, Number value, Number new_value)
                        {

                                // set the text for the label to the selected item
                                System.out.println((st[new_value.intValue()] + " selected"));
                        }
                });

TableView数据的导入和插入

import javafx.application.Application;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class test extends Application {

    private TableView<Person> table = new TableView<Person>();
//    private
    final HBox hb = new HBox();

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

    @Override
    public void start(Stage stage) {
        Scene scene = new Scene(new Group());
        stage.setTitle("Table View Sample");
        stage.setWidth(450);
        stage.setHeight(550);

        final Label label = new Label("Address Book");
        label.setFont(new Font("Arial", 20));

        table.setEditable(true);
        final ObservableList<Person> data =
                FXCollections.observableArrayList(
                        new Person("Jacob", "Smith", "jacob.smith@example.com",10),
                        new Person("Isabella", "Johnson", "isabella.johnson@example.com",10),
                        new Person("Ethan", "Williams", "ethan.williams@example.com",20),
                        new Person("Emma", "Jones", "emma.jones@example.com",30),
                        new Person("Michael", "Brown", "michael.brown@example.com",40));

        TableColumn firstNameCol = new TableColumn("First Name");
        firstNameCol.setMinWidth(50);
        firstNameCol.setCellValueFactory(
                new PropertyValueFactory<Person, String>("firstName"));

        TableColumn lastNameCol = new TableColumn("Last Name");
        lastNameCol.setMinWidth(50);
        lastNameCol.setCellValueFactory(
                new PropertyValueFactory<Person, String>("lastName"));

        TableColumn emailCol = new TableColumn("Email");
        emailCol.setMinWidth(10);
        emailCol.setCellValueFactory(
                new PropertyValueFactory<Person, String>("email"));

        TableColumn emailCol2 = new TableColumn("Email2");
        emailCol2.setMinWidth(50);
        emailCol2.setCellValueFactory(
                new PropertyValueFactory<Person, Integer>("email2"));

        table.setItems(data);
        table.getColumns().addAll(firstNameCol, lastNameCol, emailCol,emailCol2);

//        final TextField addFirstName = new TextField();
//        addFirstName.setPromptText("First Name");
//        addFirstName.setMaxWidth(firstNameCol.getPrefWidth());
//        final TextField addLastName = new TextField();
//        addLastName.setMaxWidth(lastNameCol.getPrefWidth());
//        addLastName.setPromptText("Last Name");
//        final TextField addEmail = new TextField();
//        addEmail.setMaxWidth(emailCol.getPrefWidth());
//        addEmail.setPromptText("Email");
//
//        final Button addButton = new Button("Add");
//        addButton.setOnAction(new EventHandler<ActionEvent>() {
//            @Override
//            public void handle(ActionEvent e) {
//                data.add(new Person(
//                        addFirstName.getText(),
//                        addLastName.getText(),
//                        addEmail.getText()));
//                addFirstName.clear();
//                addLastName.clear();
//                addEmail.clear();
//            }
//        });

//        hb.getChildren().addAll(addFirstName, addLastName, addEmail, addButton);
//        hb.setSpacing(3);

        final VBox vbox = new VBox();
        vbox.setSpacing(5);
        vbox.setPadding(new Insets(10, 0, 0, 10));
        vbox.getChildren().addAll(label, table, hb);

        ((Group) scene.getRoot()).getChildren().addAll(vbox);

        stage.setScene(scene);
        stage.show();
    }

    public static class Person {

        private final SimpleStringProperty firstName;
        private final SimpleStringProperty lastName;
        private final SimpleStringProperty email;
        private final SimpleIntegerProperty email2;

        private Person(String fName, String lName, String email,int e1) {
            this.firstName = new SimpleStringProperty(fName);
            this.lastName = new SimpleStringProperty(lName);
            this.email = new SimpleStringProperty(email);
            this.email2 = new SimpleIntegerProperty(e1);
        }

        public String getFirstName() {
            return firstName.get();
        }

        public void setFirstName(String fName) {
            firstName.set(fName);
        }

        public String getLastName() {
            return lastName.get();
        }

        public void setLastName(String fName) {
            lastName.set(fName);
        }

        public String getEmail() {
            return email.get();
        }

        public void setEmail(String fName) {
            email.set(fName);
        }

        public int getEmail2() {
            return email2.get();
        }

        public void setEmail2(int fName) {
            email2.set(fName);
        }
    }
}

JAVAFX控件——TableView数据的导入和插入(数据库)_长相易乐的博客-CSDN博客_javafx tableview添加数据

JavaFX控件——TableView_Hanniel的博客-CSDN博客_javafx table

contronallar.java 
private TableView<student> tableview1;
private TableColumn tv1;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Text;

import javafx.beans.value.*;

public class HelloController {
        @FXML
        private Button button4;

        @FXML
        private TextField textfielda5;

        @FXML
        private Button button5;

        @FXML
        private TextField textfielda4;

        @FXML
        private Button button2;

        @FXML
        private TextField textfielda7;

        @FXML
        private Button button3;

        @FXML
        private TextField textfielda6;

        @FXML
        private Button button6;

        @FXML
        private Button button7;

        @FXML
        private TextField textfield;

        @FXML
        private GridPane gp1;

        @FXML
        private GridPane gp2;
        @FXML
        private Text text1;

        @FXML
        private ChoiceBox chocebox;

        @FXML
        private TextField textfielda1;

        @FXML
        private Button button1;
        @FXML
        private TableView<student> tableview1;
        @FXML
        private TextField textfielda3;

        @FXML
        private TextField textfielda2;

        @FXML
        private TableColumn tv1;

        @FXML
        private TableColumn tv3;

        @FXML
        private TableColumn tv2;

        @FXML
        private TableColumn  tv5;

        @FXML
        private TableColumn tv4;

        @FXML
        private TableColumn  tv7;

        @FXML
        private TableColumn  tv6;
        @FXML
        private TableColumn tv8;
        @FXML
        void chick1(ActionEvent event) {
                System.out.println("开始添加");
                gp2.setVisible(true);
                button1.setVisible(false);
                button2.setVisible(false);
                button3.setVisible(false);
                button4.setVisible(false);
                button5.setVisible(false);
                tableview1.setVisible(false);
                textfield.setVisible(false);
                text1.setVisible(false);
                chocebox.setVisible(false);

        }

        @FXML
        void chick3(ActionEvent event) {

        }

        @FXML
        void chick2(ActionEvent event) {

        }

        @FXML
        void chick4(ActionEvent event) {

        }

        @FXML
        void strat1(ActionEvent event) {
//                导入数据
                tableview1.setEditable(true);
                String st[] = { "默认排序"};
                chocebox.getItems().addAll(st);
                chocebox.getSelectionModel().selectFirst();
                chocebox.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>() {

                        // if the item of the list is changed
                        public void changed(ObservableValue ov, Number value, Number new_value)
                        {

                                // set the text for the label to the selected item
                                System.out.println((st[new_value.intValue()] + " selected"));
                        }
                });
                final ObservableList<student> data =
                        FXCollections.observableArrayList(
                                new student("陈华","00000",69,100,88,2));

            tableview1.getColumns().clear();

                tv1.setCellValueFactory(new PropertyValueFactory<student, String>("name"));
                tv2.setCellValueFactory(new PropertyValueFactory<student, String>("xuehao"));
                tv3.setCellValueFactory(new PropertyValueFactory<student, Double>("shuxue"));
                tv4.setCellValueFactory(new PropertyValueFactory<student, Double>("yingyu"));
                tv5.setCellValueFactory(new PropertyValueFactory<student, Double>("suanfa"));
                tv6.setCellValueFactory(new PropertyValueFactory<student, Double>("caozuo"));
                tv7.setCellValueFactory(new PropertyValueFactory<student, Double>("pingjun"));
                tv8.setCellValueFactory(new PropertyValueFactory<student, Integer>("mingci"));


                tableview1.setItems(data);
                tableview1.getColumns().addAll(tv1,tv2,tv3,tv4,tv5,tv6,tv7,tv8);

        }
        @FXML
        void chick_add(ActionEvent event) {

        }

        @FXML
        void chick_end(ActionEvent event) {
                System.out.println("结束添加");
                gp2.setVisible(false);
                button1.setVisible(true);
                button2.setVisible(true);
                button3.setVisible(true);
                button4.setVisible(true);
                button5.setVisible(true);
                tableview1.setVisible(true);
                textfield.setVisible(true);
                text1.setVisible(true);
                chocebox.setVisible(true);
        }

    }

student class

import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;

public class student {
    private final SimpleStringProperty xuehao;//学号
    private final SimpleStringProperty name;//名字
    private final SimpleDoubleProperty shuxue;//数学成绩
    private final SimpleDoubleProperty yingyu;//英语成绩
    private final SimpleDoubleProperty suanfa;//算法成绩
    private final SimpleDoubleProperty caozuo;//操作系统成绩
    private final SimpleDoubleProperty pingjun;//平均分
    private final SimpleIntegerProperty mingci;//名次


    public student(String name, String xuehao, double shuxue, double yingyu, double suanfa, double caozuo) {
        this.name = new SimpleStringProperty(name);
        this.xuehao = new SimpleStringProperty(xuehao);
        this.shuxue = new SimpleDoubleProperty(shuxue);
        this.yingyu = new SimpleDoubleProperty(yingyu);
        this.suanfa = new SimpleDoubleProperty(suanfa);
        this.caozuo = new SimpleDoubleProperty(caozuo);
        this.pingjun = new SimpleDoubleProperty((shuxue + yingyu + suanfa + caozuo) / 4);
        this.mingci = new SimpleIntegerProperty(0);
    }
}

java快捷方式生成get_IDEA如何快速生成get和set方法-传统方法

1.鼠标右击"Generate"

ce28c22aa923662bf9bf903d216e659c.png

2.点击"Getter and Setter",

 

错误:没有为顶层元素指定控制器

在FXML <GridPane alignment="CENTER" prefHeight="600.0" prefWidth="850.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
后面添加
 fx:controller="HelloController"

参考资料:

java fx 图形界面开发 | MRCODE-BOOK

https://h4ck3r.top/07-21/%E5%9F%BA%E4%BA%8Ejavafx%E7%9A%84%E5%AD%A6%E7%94%9F%E6%88%90%E7%BB%A9%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F/

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
JavaFX界面设计是使用JavaFX技术来创建和布局用户界面的过程。JavaFX提供了一组丰富的控件,如TextField、TextArea、Button和Label等,用于构建用户界面。你可以使用这些控件来创建文本输入框、文本区域、按钮和标签等元素。 在JavaFX界面设计中,可以使用事件驱动的方式来处理用户的操作。通过为控件添加事件处理器,可以响应用户的点击、输入等操作。这样可以实现交互式的用户界面。 JavaFX界面设计还涉及到字符串读写技术和字符流IO等概念。你可以使用这些技术来读取和写入界面上的文本内容,实现对用户输入的处理。 对于Java课设,通常会允许学生使用SceneBuilder这样的可视化工具来辅助设计界面。在SceneBuilder中,你可以通过拖动控件到相应位置来创建界面布局,而不需要手动编写代码。这样可以更加方便和实用。 在设计JavaFX界面时,你可以将界面布局的相关信息添加到FXML文件中。通过在FXML文件中使用fx:controller来指定对应的控制器类,可以将界面和业务逻辑进行分离,使代码更加易于维护。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [JavaFx图形窗口程序设计(互联网程序设计课程 第1讲)](https://blog.csdn.net/GCTTTTTT/article/details/126768926)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [JavaFx界面设计SceneBuilder版】适合初学者](https://blog.csdn.net/ybny0421/article/details/127097008)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值