【发布时间】:2017-01-12 22:21:19
【问题描述】:
我正在尝试创建一个矩阵表,在 X 轴上我们有课程,在 Y 轴上我们有学生姓名。 每个单元格应代表学生在课程中的成绩。
我还没有找到任何方法来创建一个矩阵表,只是将第一列作为学生的名字。 (如果有其他方法请告诉我)
我能够使单元格可选择。但是我希望学生的姓名列单元格无法选择,但没有运气。
请指教。 谢谢!
【问题讨论】:
标签: java javafx javafx-2 javafx-8
我正在尝试创建一个矩阵表,在 X 轴上我们有课程,在 Y 轴上我们有学生姓名。 每个单元格应代表学生在课程中的成绩。
我还没有找到任何方法来创建一个矩阵表,只是将第一列作为学生的名字。 (如果有其他方法请告诉我)
我能够使单元格可选择。但是我希望学生的姓名列单元格无法选择,但没有运气。
请指教。 谢谢!
【问题讨论】:
标签: java javafx javafx-2 javafx-8
我认为你的问题由两部分组成
第一个:用x轴表示课程,y轴表示学生姓名的表格
首先,我们创建一个简单的类来表示我们的数据模型,并将其命名为 Data 这是它的代码
public class Data{
private String studentName;
private int mathDegree;
private int religionDegree;
private int javaDegree;
public Data(String studentName, int mathDegree, int religionDegree, int javaDegree)
{
this.studentName = studentName;
this.mathDegree = mathDegree;
this.religionDegree = religionDegree;
this.javaDegree = javaDegree;
}
public String getStudentName()
{
return studentName;
}
public void setStudentName(String studentName)
{
this.studentName = studentName;
}
public int getMathDegree()
{
return mathDegree;
}
public void setMathDegree(int mathDegree)
{
this.mathDegree = mathDegree;
}
public int getReligionDegree()
{
return religionDegree;
}
public void setReligionDegree(int religionDegree)
{
this.religionDegree = religionDegree;
}
public int getJavaDegree()
{
return javaDegree;
}
public void setJavaDegree(int javaDegree)
{
this.javaDegree = javaDegree;
}}
现在让我们创建我们的表格视图并用一些数据填充它
这是它的简单代码
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TablePosition;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class JFXION extends Application{
@Override
public void start(Stage stage)
{
List<Data> dataList = new ArrayList<>();
dataList.add(new Data("Mohammed", 50, 50, 50));
dataList.add(new Data("Ahmed", 49, 50, 47));
dataList.add(new Data("Anas", 45, 49, 49));
//tell the table that: your data is represented in [Data]objects
TableView<Data> table = new TableView();
table.setEditable(true);
TableColumn<Data, String> studentsColumn = new TableColumn("student");
//tell the student name column that your data is found in a field called[studentName]
studentsColumn.setCellValueFactory(
new PropertyValueFactory<>("studentName")
);
TableColumn<Data, Integer> mathColumn = new TableColumn("Maths");
mathColumn.setCellValueFactory(new PropertyValueFactory<>("mathDegree"));
TableColumn<Data, Integer> religionColumn = new TableColumn("Religion");
religionColumn.setCellValueFactory(new PropertyValueFactory<>("religionDegree"));
TableColumn<Data, Integer> javaColumn = new TableColumn("Java");
javaColumn.setCellValueFactory(new PropertyValueFactory<>("javaDegree"));
TableColumn coursesColumns = new TableColumn("courses");
coursesColumns.getColumns().addAll(mathColumn, religionColumn, javaColumn);
TablePosition cell = table.getFocusModel().getFocusedCell();
table.getColumns().addAll(studentsColumn, coursesColumns);
table.getItems().addAll(dataList);
Hyperlink link = new Hyperlink("more info");
link.setOnAction(e ->
{
try
{
java.awt.Desktop.getDesktop().browse(new URI("http://docs.oracle.com/javafx/2/ui_controls/table-view.htm"));
} catch (Exception ex)
{
throw new RuntimeException(ex);
}
});
VBox box = new VBox(10, table, link);
Scene scene = new Scene(box);
stage.setScene(scene);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
}
试试这个并构建你自己的代码和类
但是你问题的第二部分不清楚
你也应该看看这个链接javafx table views
希望对你有用,如果有什么问题可以留言
【讨论】: