JAVA 语言中如何实现点击一下按钮就将 textField 文本中的值插入到数据库中

如题所述

为按钮添加按钮监听,在监听的actionPerform()方法中获取文本内容并且关联数据库操作就可以了.
大概这么个流程:
你的JFrame构造方法中代码:
TextField text = new TextField();
MyActionListener listener = new MyActionListener();//监听类,最好定义为内部类
JButton button = new JButton();
button.addActionListener(listener);//为按钮添加监听
this.add(text);
this.add(button);

然后是自己定义的MyActionListener类中的actionPerform方法的重写:
public void actionPerform(ActionEvent e){
String str = text.getText();
//以下写将str插入数据库的代码.
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-04-28
js中写代码,写一个方法。
相似回答