无法修复不匹配类型:void 为 boolean

package application;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javafx.event.actionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.DatePicker;
import javafx.scene.control.RadioButton;

public class AbsenceController {
    @FXML
    public DatePicker dateabs;

    @FXML
    public RadioButton justifier,nonjustifier;

    @FXML
    public Button ajouterabsence;

    @FXML
    void ajouterabsence(actionEvent event) {
        try {
            if (nonjustifier.setSelected(true)) {
                String query = "UPDATE bource SET bourcntage=bourcentage+5";
                Connection conn = (Connection) cnx.connectdb();
                PreparedStatement pr;
                pr = conn.prepareStatement(query);
                pr.executeQuery(query);
            }

            Connection pr;
            pr.close();
            Connection conn;
            conn.close();
        } catch (SQLException ex) {
            System.out.println(ex);
        }
    }
}

我在第 27 行遇到问题:

if (nonjustifier.setSelected(true)) 

有错误:

mismatch type cannot convert from void to boolean

有什么解决办法吗?

ilovejingye 回答:无法修复不匹配类型:void 为 boolean

自从我使用 JavaFX 已经很长时间了,但是这里发生的事情是您正在检查一个不是布尔值的条件。

using (var stream = new FileStream(_filePath,FileMode.Open)) using (var reader = new StreamReader(stream) { var nextLine = reader.ReadLineAsync(); for (;;) { var l = nextLine.Result; if (l == null) break; nextLine = reader.ReadLineAsync(); // Deserialize // Do some database lookups // Do some transforms // Pass result to output thread } } 方法的返回类型是 setSelected(boolean),而不是 void,这意味着您正在检查没有 booleantrue 值的内容的条件。本质上,您所做的是将 false 的值设置为 true,但这样做的结果是没有任何结果可以让您检查按钮是否已被选中。

我认为您正在寻找的是

selected

if(nonjustifier.isSelected()) 方法返回类型 isSelected(),这是评估 boolean 条件所需的类型。

ToggleButton (Parent of RadioButton) official documentation

本文链接:https://www.f2er.com/7299.html

大家都在问