gpt4 book ai didi

java - JTextField setEnabled 与 setEditable

转载 作者:搜寻专家 更新时间:2023-10-30 21:39:32 24 4
gpt4 key购买 nike

JTextField.setEnabled()JTextField.setEditable() 有什么区别?在我的代码中,我有一个继承自 JTextField 的类的实例。但是当我设置它的属性 setEnabled(false) 时,我仍然可以在我的程序中编辑和处理它的内容。但是,当我设置其属性 setEditable(false) 时,我无法再编辑其文本。如果是这样的话。那么这里的setEnabled()属性的作用是什么?

我的继承类代码是:

 private static class CCField extends JTextField{
private static final long serialVersionUID = 1L;
public CCField() {
this( DEFAULT_COLUMN_COUNT );
}

public CCField(final int cols) {
super( cols );
}

添加信息当我调用此类的 setEnabled() 属性时,它不会对文本字段产生任何影响,它仍然保持启用状态。我的代码中有一个容器 Jcomponent,它有一个 CCField 作为子组件。因此,当我尝试使用 setEnabled(false) 禁用它时,它仍然可以编辑。而当我尝试使用 setEditable(false) 禁用它时,它被禁用了。这就是我在容器中访问此 textField 的方式:

 JComponent jComp = DDEUtil.getComponent(icTableLayout,icDS);
((JTextField)jComp.getComponent(1)).setEditable(false);

DDEUtil.getComponent 中发生的事情太复杂了,因为它涉及许多类,无法在此处发布。

我想知道我没有覆盖此组件的任何方法,那么为什么它会显示此行为。

最佳答案

在我的例子中,setEditable(false) 使字段变灰,而 setEnabled(false) 没有使字段变灰。

TextField are editable by default. The code setEditable(false) makes the TextField uneditable. It is still selectable and the user can copy data from it, but the user cannot change the TextField's contents directly.


The code setEnabled(false), disables this TextField. It is not selectable and the user can not copy data from it and the user cannot change the TextField's contents directly.


有用的链接

  1. How to Use Text Fields
  2. Component#setEnabled()

关于java - JTextField setEnabled 与 setEditable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21695175/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com