Here is my code for the Button click event. It wants to get the textfield content and prints them into the txt file. I was tring to get the name text field, but when I look at the txt file, it showed me nothing. What' the problem? Could anyone help me?Below are the code:
package Event;import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;public class EventSubmit extends JFrame{
public JTextField jtfSerialno = new JTextField();
public JTextField jtfName = new JTextField();
JButton jbt = new JButton("Submit");
public EventSubmit(){
setLayout(new FlowLayout(FlowLayout.CENTER,10,20));
add(new JLabel("Serials Number"));
add(new JTextField(8));
add(new JLabel("Name"));
add(new JTextField(8));
add(jbt);
jbt.addActionListener(new ButtonListener());
}
public class ButtonListener implements ActionListener{
public String name;
public String serialno; @Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
serialno = jtfSerialno.getText();
name = jtfName.getText();
if (e.getSource() == jbt){
FileOutputStream out;
PrintStream p;
try {
out = new FileOutputStream("Data.txt");
p = new PrintStream( out );
p.append(name);
p.close(); } catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
public static void main(String[] args) throws Exception{
//System.out.print(name);
EventSubmit frame = new EventSubmit();
frame.setTitle("my layout");
frame.pack();
frame.setSize(400,300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}}
package Event;import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;public class EventSubmit extends JFrame{
public JTextField jtfSerialno = new JTextField();
public JTextField jtfName = new JTextField();
JButton jbt = new JButton("Submit");
public EventSubmit(){
setLayout(new FlowLayout(FlowLayout.CENTER,10,20));
add(new JLabel("Serials Number"));
add(new JTextField(8));
add(new JLabel("Name"));
add(new JTextField(8));
add(jbt);
jbt.addActionListener(new ButtonListener());
}
public class ButtonListener implements ActionListener{
public String name;
public String serialno; @Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
serialno = jtfSerialno.getText();
name = jtfName.getText();
if (e.getSource() == jbt){
FileOutputStream out;
PrintStream p;
try {
out = new FileOutputStream("Data.txt");
p = new PrintStream( out );
p.append(name);
p.close(); } catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
public static void main(String[] args) throws Exception{
//System.out.print(name);
EventSubmit frame = new EventSubmit();
frame.setTitle("my layout");
frame.pack();
frame.setSize(400,300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}}
解决方案 »
- rose 2003做java逆向的问题
- 如何在java桌面程序中实现视频的播放??
- 关于java中不需要class文件生成object如何实现
- 为什么设置了ActionListener后,KeyListener就不会显示了?
- 请问如何求任意数的阶乘(用Java或C/C++实现均可)?
- 一个小问题您能看看么?
- 什么配置错了!!!郁昧(在线等待)
- 有谁用过"类Vector、JTable、AbstractTableModel"对数据库进行表格式输出!!!
- 我是java的初学者,请问除了记事本之类的外,还有什么好一点的编辑环境!谢谢!
- 数据库,在JAVA中如何查询日期呢?(有内容)谢谢(只有5分了。)
- 二维数组赋值空指针异常,求指教
- 求教一个问题现象~3Q~
public EventSubmit() {
setLayout(new FlowLayout(FlowLayout.CENTER, 10, 20)); add(new JLabel("Serials Number"));
add(jtfSerialno);
add(new JLabel("Name"));
add(jtfName); add(jbt);
jbt.addActionListener(new ButtonListener()); }
could u give me some hint?
public class EventSubmit extends JFrame { public JTextField jtfSerialno = new JTextField(8);
public JTextField jtfName = new JTextField(8);
JButton jbt = new JButton("Submit"); public EventSubmit() {
setLayout(new FlowLayout(FlowLayout.CENTER, 10, 20)); add(new JLabel("Serials Number"));
add(jtfSerialno);
add(new JLabel("Name"));
add(jtfName); add(jbt);
jbt.addActionListener(new ButtonListener()); } public class ButtonListener implements ActionListener {
public String name;
public String serialno; public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
serialno = jtfSerialno.getText();
name = jtfName.getText(); if (e.getSource() == jbt) { FileOutputStream out;
PrintStream p; try {
out = new FileOutputStream("Data.txt");
p = new PrintStream(out); p.append(name);
p.close(); } catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
} } public static void main(String[] args) throws Exception { // System.out.print(name);
EventSubmit frame = new EventSubmit();
frame.setTitle("my layout");
frame.pack();
frame.setSize(400, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}