我正在尝试制作图表,但我现在被困住了。我有一份文件,我可以接受挑战。我是JavaFX的新手,当我点击一个按钮时,我正试图让2个图表工作。我有折线图和饼图。图表不起作用,因为我没有数据。我想要的是点击“饼图”,饼图显示,消息显示“绘制饼图”。饼图不会显示,因为没有数据,但现在不是重点。当我点击其中一个图表时,我会使图表不可见,一个图表可见,另一个图表不可见。这是我尝试的一切,但它没有用。如果你不理解评论,我会尽力帮助。
控制器
package ala.javafxchart;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.PieChart;
import javafx.scene.control.Label;
public class MainSceneController implements Initializable {
@FXML
private Label label;
@FXML
private PieChart pieChart;
@FXML
private LineChart lineChart;
@FXML
private void handlePieChartAction(ActionEvent event) {
System.out.println("You clicked the Pie Chart!");
label.setText("Drawing a pie chart!!");
pieChart.setVisible(true);
}
@FXML
private void handleLineChartAction(ActionEvent event) {
System.out.println("You clicked the Pie Chart!");
label.setText("Drawing a line chart!!");
lineChart.setVisible(false);
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
@FXML
private void handleLineChartAction(ActionEvent event) {
System.out.println("You clicked the Pie Chart!");
label.setText("Drawing a line chart!!");
setVisible(lineChart);
}
private void setVisible(PieChart chart){
}
private void setVisible(LineChart chart){
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.chart.CategoryAxis?>
<?import javafx.scene.chart.LineChart?>
<?import javafx.scene.chart.NumberAxis?>
<?import javafx.scene.chart.PieChart?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<AnchorPane id="AnchorPane" prefHeight="768.0" prefWidth="1366.0" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ala.javafxchart.MainSceneController">
<children>
<BorderPane prefHeight="768.0" prefWidth="1498.0">
<top>
<Label text="Chart Demo" BorderPane.alignment="CENTER">
<font>
<Font size="35.0" />
</font>
</Label>
</top>
<left>
<VBox prefHeight="692.0" prefWidth="241.0" BorderPane.alignment="CENTER">
<children>
<Button fx:id="lineChart" mnemonicParsing="false" prefHeight="45.0" prefWidth="245.0" text="Line Chart" />
<Button fx:id="pieChart" mnemonicParsing="false" prefHeight="45.0" prefWidth="245.0" text="Pie Chart" />
</children>
</VBox>
</left>
<bottom>
<Label fx:id="statusLabel" text="status message" BorderPane.alignment="CENTER">
<padding>
<Insets right="1400.0" />
</padding>
</Label>
</bottom>
<center>
<StackPane prefHeight="150.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<children>
<LineChart title="Lost Luggage 2016" visible="false">
<xAxis>
<CategoryAxis side="BOTTOM" />
</xAxis>
<yAxis>
<NumberAxis side="LEFT" />
</yAxis>
</LineChart>
<PieChart title="Lost Luggage 2016" visible="false" />
</children>
</StackPane>
</center>
</BorderPane>
</children>
</AnchorPane>
答案 0 :(得分:1)
试试这个我对你的fxml做了一些修改,使它更具可读性
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.PieChart;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import java.net.URL;
import java.util.ResourceBundle;
public class MainSceneController implements Initializable {
public Button lineChartButton;
public Button pieChartButton;
public LineChart lineChart;
public PieChart pieChart;
public Label label;
@FXML
private void handlePieChartAction() {
System.out.println("You clicked the Pie Chart!");
label.setText("Drawing a pie chart!!");
if(pieChart.isVisible())
pieChart.setVisible(false);
else
pieChart.setVisible(true);
}
@FXML
private void handleLineChartAction() {
System.out.println("You clicked the Line Chart!");
label.setText("Drawing a line chart!!");
if(lineChart.isVisible())
lineChart.setVisible(false);
else
lineChart.setVisible(true);
}
@Override
public void initialize(URL location, ResourceBundle resources) {
// TODO
lineChartButton.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent e) {
handleLineChartAction();
}
});
pieChartButton.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent e){
handlePieChartAction();
}
});
}
}
这是FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.chart.CategoryAxis?>
<?import javafx.scene.chart.LineChart?>
<?import javafx.scene.chart.NumberAxis?>
<?import javafx.scene.chart.PieChart?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.Font?>
<AnchorPane id="AnchorPane" prefHeight="768.0" prefWidth="1366.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="MainSceneController">
<children>
<BorderPane prefHeight="768.0" prefWidth="1498.0">
<top>
<Label text="Chart Demo" BorderPane.alignment="CENTER">
<font>
<Font size="35.0" />
</font>
</Label>
</top>
<left>
<VBox prefHeight="692.0" prefWidth="241.0" BorderPane.alignment="CENTER">
<children>
<Button fx:id="lineChartButton" mnemonicParsing="false" prefHeight="45.0" prefWidth="245.0" text="Line Chart" />
<Button fx:id="pieChartButton" mnemonicParsing="false" prefHeight="45.0" prefWidth="245.0" text="Pie Chart" />
</children>
</VBox>
</left>
<bottom>
<Label fx:id="label" text="status message" BorderPane.alignment="CENTER">
<padding>
<Insets right="1400.0" />
</padding>
</Label>
</bottom>
<center>
<StackPane prefHeight="150.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<children>
<LineChart fx:id="lineChart" title="Lost Luggage 2016" visible="false">
<xAxis>
<CategoryAxis side="BOTTOM" />
</xAxis>
<yAxis>
<NumberAxis side="LEFT" />
</yAxis>
</LineChart>
<PieChart fx:id="pieChart" title="Lost Luggage 2016" visible="false" />
</children>
</StackPane>
</center>
</BorderPane>
</children>
</AnchorPane>
我将你的按钮重命名为lineChartButton和pieChartButton而不是lineChart和pieChart,因为这会让人感到非常困惑并给了pieChart和lineChart