如何从JTextField中选取信息并将其保存在txt文件中并从该文本文件中读取? [英] How to pick information from JTextField and save it on txt file and reading from that text file?

查看:216
本文介绍了如何从JTextField中选取信息并将其保存在txt文件中并从该文本文件中读取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个jFrame带有一个需要检查的Textfield,并且您在JTextfield中输入的信息需要在您按下按钮时保存在某个txt.file上,而当您按下其他按钮时则需要显示该信息输入JTF.我需要y来拾取Jtextfield,保存在txt.file上并显示出来.

I have jFrame with someone Textfield who need to be cheked,and that information which you entered in JTextfield,need to save on someone txt.file when you press button,and when you press other button need to show that information whick you entered in JTF.I need y to pick up Jtextfield,save on txt.file and show that.

import java.awt.BorderLayout;
import java.awt.Desktop.Action;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Prisustvo extends JFrame{
        JPanel prisustvo = new JPanel (new GridLayout(6, 7));
     JButton sacuvaj = new JButton("Upisi");
      JButton prikazi= new JButton("Prikazi");
      JButton predhodni = new JButton("Predhodni");
      JButton sledeci =new JButton("Sledeci");
      JTextField ime = new JTextField();
        JTextField prezime = new JTextField();
          JTextField indeks = new JTextField();
          String[] seminari = {"Microsoft","MAC","Java"};
JComboBox cek = new JComboBox(seminari);
    
    public Prisustvo(){
        prisustvo.add(new JLabel("Ime studenta:"));
         prisustvo.add(ime);
         prisustvo.add(new JLabel("Prezime studenta:"));
         prisustvo.add(prezime);
         prisustvo.add(new JLabel("Broj indeksa:"));
         prisustvo.add(indeks);
         prisustvo.add(cek);
        cek.addItemListener(new Cekbox());
         prisustvo.add(sacuvaj);
         sacuvaj.addActionListener(new Osluskivaci());
         prisustvo.add(prikazi);
         prikazi.addActionListener(new Osluskivaci());
        prisustvo.add(predhodni);
         predhodni.addActionListener(new Osluskivaci());
         prisustvo.add(sledeci);
         sledeci.addActionListener(new Osluskivaci());
       
        
               add(prisustvo,BorderLayout.CENTER);   
         }
    
    
  
         
         
         
        
    
    class Osluskivaci implements ActionListener{
        
 
        @Override
        public void actionPerformed(ActionEvent ae) {
           
        }
    }
    class Cekbox implements ItemListener{

        @Override
        public void itemStateChanged(ItemEvent ie) {
 
        }
        
    }
    
    

  
    public static void main(String[] args) {
       JFrame prisustvo = new Prisustvo();
       prisustvo.setSize(500, 500);
       prisustvo.setTitle("Prisustvo studenata seminarima");
       prisustvo.setLocationRelativeTo(null);
       prisustvo.setDefaultCloseOperation(EXIT_ON_CLOSE);
     prisustvo.pack();
     prisustvo.setVisible(true);

       
       
        
    }
    
}

推荐答案

到目前为止看起来不错!

YOu已将动作侦听器添加到所有组件中-应该是那样的吗?

您可以在actionPerformed()中找到actioncommand并对此做出反应.

如何编写动作监听器 [读取,写入和创建文件 [
Looks good so far!

YOu added the actionlistener to all the components - was it supposed to be that way?

You can figure the actioncommand in the actionPerformed() and react to that.

How to Write an Action Listener[^]

Reading, Writing, and Creating Files[^]

Have fun!


要从JTextField获取文本,可以使用getText()方法,要写文件,可以使用BufferedWriter.

在actionPerformed方法中,

我们需要检查哪些按钮触发了ActionEvent并发现我们可以使用getSource方法,在获取触发事件的按钮之后,我们可以使用条件语句来做适当的工作

因此,通常actionPerformed中的代码如下所示,

To get Text from a JTextField, you can use the getText() method, and to write a file, you can use the BufferedWriter.

In the actionPerformed method,

we need to check which of the buttons have triggered the ActionEvent and to find that we could use the getSource method, after getting the button which triggered the event, we can use conditional statements to do appropriate work

So in general , the code in actionPerformed looks like this,

if(ae.getSource() == button1)
{
    // code to write to file
}
else if(ae.getSource() == button2)
{
     // display contents of file in JTextField.
}


这篇关于如何从JTextField中选取信息并将其保存在txt文件中并从该文本文件中读取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆