Java textField的setText()方法无效
来源:百度知道 编辑:UC知道 时间:2024/05/29 13:43:16
//部分主要代码
public class testTextField extends JFrame {
public testTextField(String title){
super(title);
initDialog();
}
JTextField txt1 = new JTextField(10);//为什么此文本框仍然为空???应该是hello才对呀。
public void setValue(String str){
txt1.setText("hello");
}
private JPanel initCenter(){
JPanel centerPanel = new JPanel();//用来容纳多个组件的容器
JPanel infoPanel = new JPanel();//该容器使用GridBagLayout来布局组件
GridBagLayout layout = new GridBagLayout();
infoPanel.setLayout(layout);//设置容器的布局管理器
JLabel lbl1 = new JLabel("商品编号:");
JLabel lbl2 = new JLabel("商品名称:");
JLabel lbl3 = new JLabel("规格型号:");
JLabel lbl4 = new JLabel("单位:");
JLabel lbl5 = new JLabel("品牌:");
JLabel lbl6 = new JLabel("颜色:");
JLabel lbl7 = new JLabel("当前库存:");
JLabel lbl8 = new JLabel("备
public class testTextField extends JFrame {
public testTextField(String title){
super(title);
initDialog();
}
JTextField txt1 = new JTextField(10);//为什么此文本框仍然为空???应该是hello才对呀。
public void setValue(String str){
txt1.setText("hello");
}
private JPanel initCenter(){
JPanel centerPanel = new JPanel();//用来容纳多个组件的容器
JPanel infoPanel = new JPanel();//该容器使用GridBagLayout来布局组件
GridBagLayout layout = new GridBagLayout();
infoPanel.setLayout(layout);//设置容器的布局管理器
JLabel lbl1 = new JLabel("商品编号:");
JLabel lbl2 = new JLabel("商品名称:");
JLabel lbl3 = new JLabel("规格型号:");
JLabel lbl4 = new JLabel("单位:");
JLabel lbl5 = new JLabel("品牌:");
JLabel lbl6 = new JLabel("颜色:");
JLabel lbl7 = new JLabel("当前库存:");
JLabel lbl8 = new JLabel("备
Java textField的setText()方法无效的原因如下:
textField没有初始化完成,导致setText()方法无法获取并执行。
java代码存在其他错误,导致所有的对象没有生成,无法使用该方法。
setText()填充的文本是空字符串,没有显示数据。
你把setText()写在setValue()中
但是你调用过setValue()吗
上面的都对,但是楼上的那个
new testTextField("my test").setValue("hello"); //调用这个方法就可以了,这样就初始化文本域了
}
JTextField txt1 = new JTextField(10);
public void setValue(String str){
txt1.setText("hello"); //改成txt1.setText(str);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new testTextField("my test").setValue("hello"); //调用这个方法就可以了,这样就初始化文本域了
}
JAVA中TextField的怪问题,望高人解答...
java中TextField输入法问题
java中如何向一个TextField中输出带有空格的数组,帮我解答解答
java 中TextField怎么设置右对齐?
在java 中,如何判断TextField失去焦点
java中如何输出文字到textField中
如何设置TextField的字符固定数量?
java中的panel和textfield,,还有一些其他的东西我自学时候有点不理解,感觉上有点相同,怎么区分?
java图形编程中在textfield和textarea中怎样读取数据
在JAVA中大家知道textfield可以实现自动换行吗