开发者

Why won't this TextArea return to non-Bold format, or is Font.PLAIN just like an "add-on"?

I am trying to create a simple notepad in Java.

This is the code that I think contains the problem:

if(cb.getSelectedItem().equals("Plain")){
        MainText.setFont(new Font(getFontName(MainText),
                                  Font.PLAIN,
                                  getFontSize(MainText)));
}

Here are the methods used above:

public int getFontSize(TextArea t){
    return t.getFont().getSize();
}

public String getFontName(TextArea t开发者_如何学JAVA){
    return t.getFont().getFontName();
}

public int getFontStyle(TextArea t){
    return t.getFont().getStyle();
}


Setting the Font the way you have it is perfectly fine. You can also do it like this:

 MainText.setFont(MainText.getFont().deriveFont(Font.PLAIN));

As the Font code is fine, you should ensure that your cb.getSelecedItem test is working as expected. Perhaps stepping through or including some debug statements would be a good next step.

Also, note that in Java it is convention to begin variable names with lower case letters. Upper is used to start class names.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜