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);
}}

解决方案 »

  1.   

    Did not get the value of "name"
      

  2.   

    ye, so i need someone helps me to figure out what's the problem?
      

  3.   


    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()); }
      

  4.   


    could u give me some hint?
      

  5.   

    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);
    }
    }
      

  6.   

    i tried it and it shows me the result. now i am comparing those two pices of code.thx udbwcso. may be i will ask more questions. sorry to bother you.
      

  7.   

    oh, i check my code and finally i found the problem. thx udbwcso.