首先安装:
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"
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"
参考资料: