- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
您好,我正在尝试创建显示字段值的条形图。字段值由排序算法更改,图表应显示任何更改。
public class FXMLDocumentController implements Initializable {
static int[] pole = new int[10]; // field
int hodnota;
@FXML // bar chart
private BarChart barChart;
@FXML
private void handleButtonAction(ActionEvent ev) { //button for filling up new random graph
for (int i = 0; i < 10; i++) {
hodnota = intValue((StrictMath.random() * 100));
pole[i] = hodnota;
}
final CategoryAxis osaX = new CategoryAxis();
final NumberAxis osaY = new NumberAxis();
ObservableList<XYChart.Series<String, Number>> barChartData = FXCollections.observableArrayList();
final BarChart.Series<String, Number> series1 = new BarChart.Series<>();
series1.getData().add(new XYChart.Data<>("1", pole[0]));
series1.getData().add(new XYChart.Data<>("2", pole[1]));
series1.getData().add(new XYChart.Data<>("3", pole[2]));
series1.getData().add(new XYChart.Data<>("4", pole[3]));
series1.getData().add(new XYChart.Data<>("5", pole[4]));
series1.getData().add(new XYChart.Data<>("6", pole[5]));
series1.getData().add(new XYChart.Data<>("7", pole[6]));
series1.getData().add(new XYChart.Data<>("8", pole[7]));
series1.getData().add(new XYChart.Data<>("9", pole[8]));
series1.getData().add(new XYChart.Data<>("10", pole[9]));
barChartData.add(series1);
barChart.setData(barChartData);
}
@FXML // button which starts sorting algorhitm, it is changing values in field
private void bubbleButton(ActionEvent ev) {
BubbleSort vlakno=new BubbleSort("vypoctoveVlakno");
vlakno.start();
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
我已经尝试创建一个新线程来一次又一次地加载数据,但它没有用。还尝试将它们加载到 FXML Controller 中,但图形窗口卡住了。实际上我对此很陌生。
FXML 文档,main 只是从 fxml 加载阶段
<AnchorPane id="AnchorPane" prefHeight="487.0" prefWidth="610.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="radicialgoritmy.FXMLDocumentController">
<children>
<Button fx:id="button" layoutX="14.0" layoutY="14.0" onAction="#handleButtonAction" text="Vygeneruj" />
<BarChart fx:id="barChart" animated="false" cache="true" layoutX="30.0" layoutY="188.0" legendVisible="false" prefHeight="292.0" prefWidth="552.0">
<xAxis>
<CategoryAxis fx:id="osaX" side="BOTTOM" />
</xAxis>
<yAxis>
<NumberAxis fx:id="osaY" side="LEFT" />
</yAxis>
</BarChart>
<Button fx:id="bubble" layoutX="100.0" layoutY="14.0" mnemonicParsing="false" onAction="#bubbleButton" text="BubbleSort" />
排序算法类。
class BubbleSort extends Thread {
public BubbleSort(final String jmeno) {
super(jmeno);
}
@Override
public void run() {
for (int i = 0; i < pole.length - 1; i++) {
for (int j = 0; j < pole.length - i - 1; j++) {
if (pole[j] < pole[j + 1]) {
final int tmp = pole[j];
pole[j] = pole[j + 1];
pole[j + 1] = tmp;
}
try {
Thread.sleep(250);
} catch (final InterruptedException ex) {
Logger.getLogger(BubbleSort.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
}
最佳答案
这里是 mcve 演示了一种将变化的数据耦合到 BarChart
的机制。
还有其他方法可以实现这种“绑定(bind)”,但我尽量保持简单。
冒泡排序算法与问题无关,与答案无关, 所以最好不要考虑。 mcve 需要演示问题/答案,而不是应用程序:
import java.util.Random;
import javafx.beans.Observable;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener.Change;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.chart.XYChart.Data;
public class FXMLDocumentController {
@FXML private BarChart barChart;
private ObservableList<XYChart.Series<String, Number>> barChartData;
private ObservableList<DataItem> items;
private Random rnd = new Random();
private static int SIZE = 10;
@FXML
void initialize(){
//an observable collection of DataItem objects, each representing data of one bar
items = FXCollections.observableArrayList(
dataItem -> new Observable[] { dataItem.nameProperty(), dataItem.valueProperty()});
//initialize with random values
for (int i = 0; i < SIZE; i++) {
items.add(new DataItem(String.valueOf(i), rnd.nextInt(100)));
}
}
@FXML
private void handleButtonAction( ActionEvent ev) {
ObservableList<Data<String, Number>> seriesData = new DataConvertor(items).getData();
CategoryAxis osaX = new CategoryAxis();
NumberAxis osaY = new NumberAxis();
barChartData = FXCollections.observableArrayList();
BarChart.Series<String, Number> series1 = new BarChart.Series<>();
series1.setData(seriesData);
barChartData.add(series1);
barChart.setData(barChartData);
}
@FXML
private void bubbleButton( ActionEvent ev) {
BubbleSort vlakno=new BubbleSort(items);
vlakno.start();
}
}
//pojo to represent items of one bar
class DataItem {
private StringProperty name = new SimpleStringProperty(this, "name");
private IntegerProperty value = new SimpleIntegerProperty(this, "value");
public DataItem( String name, int value) {
setName(name);
setValue(value);
}
public StringProperty nameProperty() {
return name;
}
public String getName() {
return this.nameProperty().get();
}
public void setName( String name) {
this.nameProperty().set(name);
}
public IntegerProperty valueProperty() {
return value;
}
public int getValue() {
return this.valueProperty().get();
}
public void setValue( int value) {
this.valueProperty().set(value);
}
}
//simple coupling between items and graph data
class DataConvertor{
ObservableList<DataItem> items;
ObservableList<Data<String, Number>>data;
DataConvertor( ObservableList<DataItem> items) {
super();
this.items = items;
data = FXCollections.observableArrayList();
items.forEach(item -> data.add(new Data<>(item.getName(), item.getValue())));
items.addListener( ( Change<? extends DataItem> arg0 ) -> update());
}
void update() {
for(int i =0; i < data.size(); i++){
Data<String, Number> d = data.get(i);
d.setYValue(items.get(i).getValue());
}
data.clear();
items.forEach(item -> data.add(new Data<>(item.getName(), item.getValue())));
}
ObservableList<Data<String, Number>>getData(){
return data;
}
}
BubbleSort
类并不真正进行排序。它只是定期和随机地改变数据:
import java.util.Random;
import javafx.animation.PauseTransition;
import javafx.collections.ObservableList;
import javafx.util.Duration;
class BubbleSort {
private ObservableList<DataItem> items;
private Random rnd = new Random();
private PauseTransition periodicTask;
private static final long CYCLE = 500;
public BubbleSort(final ObservableList<DataItem> items) {
this.items = items;
periodicTask = new PauseTransition(Duration.millis(CYCLE));
periodicTask.setOnFinished((e) -> {
calculateNewValues();
periodicTask.playFromStart();
});
}
private void calculateNewValues() {
for(DataItem d : items){
d.setValue(rnd.nextInt(100));
}
}
void start(){
periodicTask.playFromStart();
}
}
请注意,发布的 fxml 文件不完整。缺少这两个标签:
</children>
</AnchorPane>
关于java - Javafx 中的动态条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53573747/
我不知道这是不是问这种问题的最佳地点, 我看到一些 JavaFX伙计们,重新标记 一些问题通过替换 javafx来自 javafx-2并采用新的 javafx-8 .它被弃用了还是什么? 编辑 : 不
错误本身: Error:java: invalid flag: --add-modules=javafx.fxml,javafx.graphics,javafx.controls,javafx.bas
这个想法是让一个应用程序在每个显示器上显示两个不同的窗口(阶段),该应用程序应该知道计算机有多少个显示器及其分辨率。 javafx有可能吗? 最佳答案 对于当前版本的 JavaFX (2.2),您可以
我正在将我的项目从 javafx 1.3 转换为 javafx 2.1。但我对 javafx.lang 有疑问包裹。 最佳答案 JavaFX 1.3 lang 包内容被拆分并移至下一个位置: 时长变为
当我尝试将标签添加到 gridpane 中时,如第二张图片所示,它不起作用。我已经尝试了很多东西,比如添加 CSS,但它仍然无法正常工作。为什么第 113 和 114 行不起作用? (opcje.se
我有一个JavaFX ContextMenu分配给滚动面板的鼠标右键单击。它会打开,但在滚动 Pane 外部单击时不会关闭。我可以在滚动 Pane 中添加另一个鼠标事件以将其隐藏,但这只能解决1个问题
我有一个tableview,其中附有一个可观察到的自定义类对象的列表(类类型:SalesInvoiceNetSale)。该表中的所有数据都可以正常显示。可观察列表中的最后一项是总计行(类类型:Sale
关闭。这个问题需要更多 focused .它目前不接受答案。 想改进这个问题?更新问题,使其仅关注一个问题 editing this post . 2年前关闭。 Improve this questi
我想知道如何在JavaFX中绘制半圆。我尝试使用Shape和QuadCurve,但无法制作出完美的半圆。 这是我要绘制的图片: 最佳答案 您链接的图片实际上是一个半圆环。您可以通过绘制嵌套的2条圆弧和
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
我正在寻找 JavaFX 支持的图像类型(最新)列表,例如PNG、JPEG、TIFF。不同的搜索引擎没有帮助......知道从哪里开始吗? 更特别的是,我对 16 位灰度图像(不同格式)和罕见的受支持
我希望在 javafx 中让标签每 0.1 秒闪烁一次。文本显示在后台运行的 ImageView gif 的顶部。我将如何去做,或者您对最佳方法有什么建议? 谢谢 最佳答案 @fabian 的解决方案
我需要测试所选项目的值以调用不同的方法,因此我编写了添加侦听器的代码,但是该代码生成语法错误 @FXML private JFXComboBox cmbComp; cmbComp.valuePrope
我正在 Javafx 中编写一个非常简单的应用程序,其中舞台上有一个带有文本框的按钮作为一个场景。现在,我想要的行为是,当我单击按钮时,我可以使用另一个按钮加载另一个场景和舞台上的一个文本框,然后删除
编辑:如果用户单击“删除”以删除 ListView 中的项目,我会弹出一个警告框。它有效,但我希望它能超越原来的舞台。它出现在我的第一台显示器上。有什么方法可以设置警报显示时的位置吗? 请注意,“所有
我想使用 JavaFX 编写一个笔画绘图应用程序。我有一个压敏绘图板,如果能够读取笔的压力和倾斜值,那就太好了。 JavaFX 有一个 API 可以处理鼠标、触摸和滑动输入,但似乎没有任何东西可以产生
我在 JavaFX 中使用条形图和折线图。当我使两个图表大小相同并将它们放在同一位置时,它们完美地相互重叠。我如何使折线图显示在条形图的顶部。 目前我已将它们的不透明度设置为 0.7,这样它们“看起来
此问题与 this 相关。现在我想为字段值等于某个值的行着色。 @FXML private TableView tv_mm_view; @FXML private Ta
我有一个程序,可以生成高度图(0-255 的整数的 2D 数组),并使用 Shape3D“Box”对象为每个“像素”构建 3D View ,其高度与其在高度图中的值成比例。这会创建一个看起来很酷的四四
我想为 JavaFX 创建上下文菜单。这是我测试过的代码。但是由于某种原因,当我右键单击树节点时没有上下文菜单。你能帮我找出我的错误吗。 import java.util.Arrays; import
我是一名优秀的程序员,十分优秀!