我有一个应用程序读取文件并显示文件中每个字母出现次数的图表(在我的应用程序文件中必须位于" C:/new1.txt")。
我想根据所选的单选按钮设置条形颜色,但它不会改变颜色。我如何设置颜色以及我在哪里犯错?
控制器类:
package sample;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Controller {
@FXML
public BarChart<String,Number> barchart;
@FXML
RadioButton redbutton=new RadioButton("Red");
@FXML
RadioButton bluebutton=new RadioButton("Blue");
@FXML
ToggleGroup group=new ToggleGroup();
public void selam(ActionEvent a){
//Array to store number of occurrences of each letter in file
//Ex = if exist 2 "a" and 4 "d" then counter[0]=2 and counter[3]=4
int[] counter = new int[26];
//Read file
try {
File file = new File("C:/new1.txt");
Scanner scanner = new Scanner(file,"utf-8");
while (scanner.hasNext()) {
char[] chars = scanner.nextLine().toLowerCase().toCharArray();
for (Character karakter : chars) {
if(Character.isLetter(karakter)) {
//add to array
counter[karakter - 'a']++;
}
}
}
} catch (FileNotFoundException e) {
System.out.println("File does not exist");
}
char[] alphabet = "abcdefghijklmnopqrstuvwxyz".toCharArray();
int orderofletter=0;
XYChart.Series series1 = new XYChart.Series();
//Add number of occurrences of each letter to XY chart
for (char c:alphabet){
series1.getData().add(new XYChart.Data(String.valueOf(c), counter[orderofletter]));
orderofletter++;
}
// CHANGE COLOR
if(redbutton.isSelected()){
barchart.setStyle("-fx-bar-fill: red;");
System.out.println("Red");
}
if(bluebutton.isSelected()){
barchart.setStyle("-fx-bar-fill: blue;");
}
//Add xychart to barchart
barchart.getData().addAll(series1);
}
}
FXML课程:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.chart.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<Pane id="mainpane" fx:id="mainpane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="590.0" prefWidth="1085.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<TextField layoutY="517.0" prefHeight="67.0" prefWidth="309.0" />
<BarChart fx:id="barchart" barGap="2.0" layoutY="8.0" prefHeight="498.0" prefWidth="1085.0" style="-fx-border-color: chartreuse;">
<xAxis>
<CategoryAxis endMargin="0.0" side="BOTTOM" startMargin="0.0" />
</xAxis>
<yAxis>
<NumberAxis prefHeight="364.0" prefWidth="33.0" side="LEFT" upperBound="0.0" />
</yAxis>
</BarChart>
<Button layoutX="447.0" layoutY="512.0" mnemonicParsing="false" onAction="#selam" prefHeight="67.0" prefWidth="139.0" text="Draw" />
<RadioButton fx:id="bluebutton" layoutX="324.0" layoutY="551.0" mnemonicParsing="false" text="Blue">
<toggleGroup>
<ToggleGroup fx:id="group" />
</toggleGroup>
</RadioButton>
<RadioButton fx:id="redbutton" layoutX="324.0" layoutY="517.0" mnemonicParsing="false" text="Red" toggleGroup="$group" />
</children>
</Pane>
答案 0 :(得分:1)
我认为您需要为每个图表栏单独设置颜色:
barchart.getData().addAll(series1);
if (bluebutton.isSelected()) {
barchart.lookupAll(".default-color0.chart-bar")
.forEach(n -> n.setStyle("-fx-bar-fill: blue;"));
}