简体   繁体   English

JavaFX:如何调用和传递控制对象?

[英]JavaFX: How do you call and pass the control object?

This is a method for getting the text of different JavaFx controls.这是一种获取不同 JavaFx 控件文本的方法。

private String getText(Node item) {
        
    String txt = "";

    if (item instanceof Label) {
        if(!((Label)item).getText().isEmpty()){
            txt = ((Label)item).getText();
        }
    }
    if (item instanceof TextField) {
        if(!((TextField)item).getText().isEmpty()){
            txt = ((TextField)item).getText();
        }
    }
    if (item instanceof CheckBox) {
        if(!((CheckBox)item).getText().isEmpty()){
            txt = ((CheckBox)item).getText();
        }
    }
    return txt;
}

I'm trying to lessen the lines of codes.我正在尝试减少代码行。 Instead of defining Label, TextField, CheckBox, etc. Is there an object class that I could call for all other controls and put in beside the Node item so that I don't need to repeat and specify each control?而不是定义 Label、TextField、CheckBox 等。是否有一个对象类可以为所有其他控件调用并放在 Node 项旁边,以便我不需要重复和指定每个控件? Is there a way to make it like this?有没有办法让它像这样?

private String getText(Node item, X x) {

    String txt = "";

    if (item instanceof x) {
        if(!((x)item).getText().isEmpty()){
            txt = ((x)item).getText();
        }
    return txt;
}

What should I replace X & x.我应该用什么代替 X & x。 I don't know what object should I call or replace the Control.我不知道我应该调用或替换 Control 什么对象。 Just don't mind about the logic of isEmpty.只是不要在意 isEmpty 的逻辑。 My main concern is what object should I call or replace the Control.我主要关心的是我应该调用或替换 Control 什么对象。 And call the method like this并像这样调用方法

getText(txtSample, TextField.class) 

or或者

getText(lblSample, Label.class)

Thank you.谢谢你。

Since the common superclass Control has no method getText() (because not all controls have text), this is not possible.由于通用超类 Control 没有getText()方法(因为并非所有控件都有文本),因此这是不可能的。

EDIT: as James_D pointed out, Label and CheckBox do have a common ancestor that has the method getText() , namely Labeled .编辑:正如 James_D 指出的那样, LabelCheckBox确实有一个共同的祖先,它有方法getText() ,即Labeled This saves us another instanceof check.这为我们节省了另一个检查instanceof

However, the rest of the similarities can be extracted.但是,可以提取其余的相似性。 Suppose we first write a helper method to extract the text from the control (note the down casting in the same statement as the instanceof :假设我们首先编写一个辅助方法来从控件中提取文本(注意与instanceof相同的语句中的向下转换:

    private String getRawText( final Node item )
    {
        if( item instanceof Labeled labeled )
            return labeled.getText();
        else if( item instanceof TextField field )
            return field.getText();
        else //this is the default
            return "";
    }

then the original function becomes:那么原来的函数就变成了:

    private String getText( final Node item ) 
    {

        final var  txt = getRawText( item );

        return txt.isEmpty() 
                ? ""
                : txt;
    }

But note that you check text.isEmpty() while the default, with which you initialized the variable txt is itself empty ( "" ).但请注意,您检查text.isEmpty()而默认值,您使用它初始化变量txt本身为空( "" )。 Thus the check for empty is not needed.因此不需要检查空。 Did you mean .isBlank() (stripping whitespace only strings)?您的意思是.isBlank() (仅去除空白字符串)? Or getText() == null ?还是getText() == null

Otherwise, it can be removed, and then your left with the function getRawText() only.否则,它可以被删除,然后你只剩下函数getRawText()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM