我的ListView不可滚动,我无法在窗口中选择任何其他内容。通常,当您将鼠标悬停在按钮上时,它会改变颜色,但即使发生这种情况也不会。单击按钮可以正常工作。
我真的不知道如何解决这个问题,请帮忙!
ListView保持选中状态(蓝色边框),但我无法滚动
我的代码:
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package hue1_fx_client;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.chart.PieChart;
import javafx.scene.control.Button;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.stage.Stage;
/**
* FXML Controller class
*
* @author Benjamin Kesetovic
*/
public class Window_statistikController implements Initializable {
@FXML
private PieChart piechart;
@FXML
private ListView<String> listview;
private Map<Integer, String[]> log = new HashMap<>();
private String[] arr;
private int richtig = 0;
private ObservableList<PieChart.Data> pieChartData;
ObservableList<String> ol = FXCollections.observableArrayList();
@FXML
private Button btn;
/**
* Initializes the controller class.
*
* @param url
* @param rb
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
log = Business_logic.getLog();
listview.setItems(ol);
listview.setCellFactory((ListView<String> ListView) -> new ListCell<String>() {
//Wenn ein neues Element @text zur Listview hinzugefuegt wird
@Override
protected void updateItem(String text, boolean empty) {
super.updateItem(text, empty);
if (text == null || text.equals("")) {
setText("");
setStyle("-fx-background-color: lightgray;");
} else {
if (text.charAt(0) == '+') {
setStyle("-fx-background-color: green;");
setText(text.substring(1));
} else {
setStyle("-fx-background-color: red;");
setText(text);
}
}
}
});
//Einen leerstring hinzufügen um die Hintergrundfarbe lightgray zu setzen
ol.add("");
//Wieder entfernen
ol.remove(0);
//ObservableList füllen
for (int i : log.keySet()) {
arr = log.get(i);
if (arr[1].equals("ok")) {
ol.add("+" + arr[0] + " -> " + arr[2]);
richtig++;
} else {
ol.add(arr[0] + " -> " + arr[2] + " [" + arr[1] + "]");
}
}
pieChartData
= FXCollections.observableArrayList(
new PieChart.Data("Richtig", richtig),
new PieChart.Data("Falsch", (log.size() - richtig)));
//Piechart mit Daten füllen
piechart.setData(pieChartData);
//Legende verstecken
piechart.legendVisibleProperty().setValue(false);
//Richtige Seite grün einfärben
pieChartData.get(0).getNode().setStyle(
"-fx-pie-color: green;"
);
//Falsche Seite rot einfärben
pieChartData.get(1).getNode().setStyle(
"-fx-pie-color: red;"
);
piechart.setTitle("Vokabel Prüfung");
//Neu starten des Programmes um neu anzufangen
btn.setOnAction((event) -> {
try {
Hue1_fx_client.getKlasse().close_stage();
new Hue1_fx_client().start(new Stage());
} catch (Exception ex) {
Logger.getLogger(Window_IOerrorController.class.getName()).log(Level.SEVERE, null, ex);
}
});
}
FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.chart.PieChart?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="759.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="hue1_fx_client.Window_statistikController">
<children>
<PieChart fx:id="piechart" layoutX="14.0" layoutY="60.0" prefHeight="280.0" prefWidth="280.0" />
<ListView fx:id="listview" layoutX="308.0" layoutY="23.0" prefHeight="371.0" prefWidth="444.0" style="-fx-font-size: 18; -fx-font-weight: bold;" />
<Label layoutX="308.0" layoutY="-2.0" prefHeight="25.0" prefWidth="444.0" style="-fx-alignment: center; -fx-background-color: lightgray;" text="{Vokabel} -> {Eingabe} [Lösung]">
<font>
<Font size="15.0" />
</font></Label>
<Button fx:id="btn" layoutX="122.0" layoutY="361.0" mnemonicParsing="false" text="Nochmal" />
</children>
</AnchorPane>
答案 0 :(得分:-1)
深入了解您的代码会很有帮助......
但是为ListView对象尝试setfocusable(true)
。