我学Java不到一个月,请高手帮我看下以下的代码,给点好的建议.

本文介绍了一个使用Java Swing实现的简单图形用户界面应用程序,该程序能够进行文本输入并将其保存到文件中,同时也能从文件中读取这些信息并显示出来。主要组件包括用于输入用户名和密码的文本框及按钮。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package PKLesson6;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.
event.ActionEvent;
import java.awt.
event.ActionListener;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
//import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
//import java.awt.GridLayout;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class Drrp extends JApplet implements ActionListener {
    JPanel pl 
= new JPanel();

    JButton b1 
= new JButton("保存");

    JButton b2 
= new JButton("读取");

    JLabel l1 
= new JLabel("Name");

    JLabel l2 
= new JLabel("Phone");

    JTextField tf1 
= new JTextField(10);

    JPasswordField pwd 
= new JPasswordField(10);

    
// JTextField tf2=new JTextField(20);
    GridBagLayout gby = new GridBagLayout();

    
//GridLayout glt=new GridLayout();
    GridBagConstraints gbc = new GridBagConstraints();

    FileInputStream fis;
    
    BufferedReader br;

    InputStreamReader isr;

    
public void init() {
        
this.getContentPane().add(pl);
        pl.setLayout(gby);
        
//gbc.fill=GridBagConstraints.BOTH;
        
//gbc=anchor.gridBagConstraints.CETER;
        gbc.gridx = 3;
        gbc.gridy 
= 1;
        gby.setConstraints(l1, gbc);
        pl.add(l1);

        gbc.gridx 
= 4;
        gbc.gridy 
= 1;
        gby.setConstraints(tf1, gbc);
        pl.add(tf1);

        gbc.gridx 
= 3;
        gbc.gridy 
= 2;
        gby.setConstraints(l2, gbc);
        pl.add(l2);

        gbc.gridx 
= 4;
        gbc.gridy 
= 2;
        gby.setConstraints(pwd, gbc);
        pl.add(pwd);

        gbc.gridx 
= 3;
        gbc.gridy 
= 3;
        gby.setConstraints(b1, gbc);
        pl.add(b1);

        gbc.gridx 
= 4;
        gbc.gridy 
= 3;
        gby.setConstraints(b2, gbc);
        pl.add(b2);

        b1.addActionListener(
this);
        b2.addActionListener(
this);
    }


    
public void actionPerformed(ActionEvent e) {
        
if (e.getSource() == b1) {
            String n 
= tf1.getText();
            
char[] c = pwd.getPassword();
            String p 
= String.copyValueOf(c);
            
try {
                RandomAccessFile raf 
= new RandomAccessFile("d:/pwd.txt""rw");
                raf.seek(raf.length());
                
//raf.length();
                raf.writeBytes(n + " " + p);
                
//raf.writeBytes(n);
                
//raf.writeBytes( );
                
//raf.writeBytes(p);
            }
 catch (FileNotFoundException e1) {
                e1.printStackTrace();
            }
 catch (IOException e2) {
                e2.printStackTrace();
            }

            tf1.setText(
"");
            pwd.setText(
"");
        }
 else {
            
if (e.getSource() == b2) {
                
try {
                    fis 
= new FileInputStream("d:/pwd.txt");
                    
                    isr 
= new InputStreamReader(fis);
                    br 
= new BufferedReader(isr);
                    String s 
= new String();
                    
while ((s = br.readLine()) != null)
                         System.
out.println(s);
                        
//System.out.println(tf1);
                       
// System.out.println(pwd);

                    fis.close();
                }
 catch (FileNotFoundException fn) {
                    System.
out.println("File not found!");
                }
 catch (IOException fn) {
                    System.
out.println("I/O Error");
                }

            }

        }


    }

}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值