首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

want to get help for Button Event,该怎么解决

2013-10-15 
want to get help for Button EventHere is my code for the Button click event. It wants to get the te

want to get help for Button Event
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);
}

}
[解决办法]



You can use this code to run about, look at the results, and compared to your code.
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);


}
}

热点排行