我必须做一些projet在JavaFX中用Canvas绘制正多边形,我怀疑如何使用GraphicsContext设计一个带有canvas的圆
我有这个包含两个轴(x,y)的点类
public class Point2D {
private float mX;
private float mY;
public Point2D () {
this (0,0);
}
public Point2D (float x, float y) {
mX = x;
mY = y;
}
public float getX() {
return mX;
}
public float getY() {
return mY;
}
}
Run Code Online (Sandbox Code Playgroud)
我有这个圆圈类,我怀疑使方法public void drawCircle(GraphicsContext gc)
public class Circle{
private Point2D mCenter;
private Color color;
private float mRadius;
public Circle (Point2D center, Color color, float radius ) {
this.mCenter = center;
this.color = …Run Code Online (Sandbox Code Playgroud) 我现在正在教自己JavaFX,我已经采用了一个简单的示例程序,该程序硬编码视图并将其转换为使用FXML的程序(主要是因为我可以使用SceneBuilder来构建UI).我没有编写单独的控制器类,而是使用应用程序类(因此有1个Java文件和1个FXML文件).我没有使用initialize()方法,因为它是线性流(显示UI,填充字段,等待输入).弹出视图,但随后没有任何控件被映射到适当的变量,因此应用程序出错(因此@FXML TableView<...> table,table是null).
但是,我输入了一个initialize()调试方法,在进入时注入控件initialize(),然后在initialize()退出时返回null .
所以问题是,JavaFX是否将应用程序类的新实例实例化为单独的控制器类?这可以解释为什么变量超出范围.或者它是否是其他东西(例如,仅在从JavaFX动作回调时才注入控件)?
因此,我试图显示此窗口。这给我关于未为Button类型定义的方法的错误。我不确定为什么,因为此代码是直接从教程中复制的。在他的IDE上工作,但不在我的工作中。他正在使用intellij,而我正在使用Eclipse。
closeButton.setOnAction(e -> window.close());
Run Code Online (Sandbox Code Playgroud)
该
setOnAction((<no type> e) -> {})类型的方法未定义Button
layout.getChildren().addAll(label, closeButton);
Run Code Online (Sandbox Code Playgroud)
addAll(int, Collection<? extends Node>)类型中的方法List<Node>不适用于参数(Label, Button)“
import java.awt.Button;
...
public static void display(String title, String message) {
Stage window = new Stage();
//Block events to other windows
window.initModality(Modality.APPLICATION_MODAL);
window.setTitle(title);
window.setMinWidth(250);
Label label = new Label();
label.setText(message);
Button closeButton = new Button("Close this window");
closeButton.setOnAction(e -> window.close());
VBox layout = new VBox(10);
layout.getChildren().addAll(label, closeButton);
layout.setAlignment(Pos.CENTER);
//Display window and wait for it to …Run Code Online (Sandbox Code Playgroud) mpcListView.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {
@Override
public ListCell<String> call(ListView<String> param){
return new XCell();
}
});
public class XCell extends ListCell<String>{
HBox hbox = new HBox();
Label label = new Label();
Button button = new Button("",getImage());
String lastItem;
public XCell(){
super();
hbox.setSpacing(120);
hbox.getChildren().addAll(label,button);
button.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent event){
mpcListView.getItems().remove(lastItem);
}
});
}
@Override
protected void updateItem(String item, boolean empty){
super.updateItem(item, empty);
if (empty){
lastItem = null;
setGraphic(null);
}else{
lastItem = item;
label.setText(item);
setGraphic(hbox);
}
}
}
Run Code Online (Sandbox Code Playgroud)
为什么super.updateItem(item, empty) …
我想知道为什么最终修饰符不与getter和setter一起使用?
为什么这样:
private int x;
public void setX(int x)
{
if(x >= 1) throw new IllegalArgumentException("X must be lower than 1");
this.x = x;
}
Run Code Online (Sandbox Code Playgroud)
而不是这个:
private int x;
public final void setX(int x)
{
if(x >= 1) throw new IllegalArgumentException("X must be lower than 1");
this.x = x;
}
Run Code Online (Sandbox Code Playgroud)
它没有改善封装?我一直试图用谷歌澄清它,但我没有运气.
谢谢你提前.
我是javafx的新手.
我试图在根目录下的树表中的每一列中包含复选框,标签和按钮.
有可能做到这一点.帮助我解决方案和相同的参考代码.
谢谢,Vevek.
我有一个ListViewwith items,并开发了一个删除该项目的删除功能。我面临的问题是当我删除一个项目时,下面的项目也会被删除。
为了让您有更好的理解。前任:
如果列表中有 5 个项目,并且我选择并删除“项目 2”,则项目 2 和 3 将被删除。第 1、4 和 5 项保留在列表视图中。如果我删除列表中的最后一个项目,那么该项目将被删除,并且我会得到一个java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
这是我的代码:
public void handleDeleteButton() {
btnDelete.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
final int selectedIdx = playerList.getSelectionModel().getSelectedIndex();
if (selectedIdx != -1) {
String itemToRemove = playerList.getSelectionModel().getSelectedItem();
final int newSelectedIdx =
(selectedIdx == playerList.getItems().size() - 1)
? selectedIdx - 1
: selectedIdx;
playerList.getItems().remove(selectedIdx);
playerList.getSelectionModel().select(newSelectedIdx);
//removes the player for the array
System.out.println("selectIdx: " + selectedIdx);
System.out.println("item: " + …Run Code Online (Sandbox Code Playgroud) 我试图TableView table按某一列排序.当我显示表格时,它显示了但是从我在这里和其他网站上发现的内容来看,它让我越来越困惑.
这是tableview代码
public TableView<Animal> animalLostLocation(ArrayList<Animal> list)
{
TableColumn<Animal, String> atypeColumn = new TableColumn<>("Animal Type");
atypeColumn.setMinWidth(200);
atypeColumn.setSortable(false);
atypeColumn.setCellValueFactory(new PropertyValueFactory<>("aType"));
TableColumn<Animal, String> descriptionColumn = new TableColumn<>("Description");
descriptionColumn.setMinWidth(200);
descriptionColumn.setSortable(false);
descriptionColumn.setCellValueFactory(new PropertyValueFactory<>("description"));
TableColumn<Animal, String> breedColumn = new TableColumn<>("Breed");
breedColumn.setMinWidth(80);
breedColumn.setSortable(false);
breedColumn.setCellValueFactory(new PropertyValueFactory<>("breed"));
TableColumn<Animal, String> nameColumn = new TableColumn<>("Name");
nameColumn.setMinWidth(200);
nameColumn.setSortable(false);
nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
TableColumn<Animal, Catergory> catColumn = new TableColumn<>("Category");
catColumn.setMinWidth(200);
breedColumn.setSortable(false);
catColumn.setCellValueFactory(new PropertyValueFactory<>("category"));
TableColumn<Animal, Integer > ageColumn = new TableColumn<>("Age");
ageColumn.setMinWidth(50);
ageColumn.setSortable(false);
ageColumn.setCellValueFactory(new PropertyValueFactory<>("age"));
TableColumn<Animal, Integer > idColumn = new TableColumn<>("ID");
idColumn.setMinWidth(50); …Run Code Online (Sandbox Code Playgroud) 我想绑定textProperty的Label对象的SimpleIntegerProperty有帮助的Bindings,但是当我改变它不会改变文本 SimpleIntegerProperty对象的实时。任何有关如何进行textProperty更改的帮助将不胜感激。
package sample;
import javafx.beans.binding.Bindings;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import java.net.URL;
import java.util.ResourceBundle;
public class Controller implements Initializable
{
@FXML
private Slider slider;
@FXML
private Label label;
@Override
public void initialize(URL location, ResourceBundle resources) {
MyObject object = new MyObject(0);
label.textProperty().bind(Bindings.createStringBinding(() -> " hello " + object.numberProperty().get() * (10 + 12)/2));
object.numberProperty().bind(slider.valueProperty());
}
}
class MyObject {
private SimpleIntegerProperty number;
public Object(int …Run Code Online (Sandbox Code Playgroud) AWT 和 Swing 对 JavaFX 中的所有内容具有几乎相同的名称,因此我发现自己按它们的包浏览项目,找到 FX 条目,然后查看文档。
我知道模块是可能的,但这是一个与其他所有内容捆绑在一起的核心 JDK 库