英汉小词典 java_英汉小词典 java随机存取文件流应用 version1.0

package jnet;

//英汉小词典  随机存取文件   需改进 version1.0

import java.io.*;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class F extends JFrame implements ActionListener{

File file = new File("英汉小词典.txt");

JButton writeBtn = new JButton("录入");

JButton viewBtn = new JButton("显示");

JTextField word = new JTextField(8);

JTextField note = new JTextField(8);

JTextArea txt = new JTextArea(20,30);

JPanel p1 = new JPanel();

JPanel p2 = new JPanel();

F(){

setTitle("英汉小词典");

setBounds(100,50,400,250);

setVisible(true);

setDefaultCloseOperation(EXIT_ON_CLOSE);

add(p1,"North");

p1.setBackground(Color.cyan);

p1.add(new JLabel("输入单词"));

p1.add(word);

p1.add(new JLabel("输入解释"));

p1.add(note);

p1.add(writeBtn);

writeBtn.addActionListener(this);

add(p2,"Center");

p2.add(viewBtn);

p2.add(txt);

viewBtn.addActionListener(this);

validate();

}

@Override

public void actionPerformed(ActionEvent e) {

if (e.getSource()==writeBtn) {

inputWord();

}

if (e.getSource()==viewBtn) {

viewWord();

}

}

//显示汉语解释的方法

public void viewWord()

{

int number=1;

try {

@SuppressWarnings("resource")

RandomAccessFile infile = new RandomAccessFile(file, "rw");

@SuppressWarnings("unused")

String 单词 =null;

while((单词=infile.readUTF())!=null)

{

txt.append("\n"+number+单词);

txt.append(" " + infile.readUTF());  //读取汉语解释

txt.append("\n--------------------");

number++;

}

infile.close();

} catch (Exception e) {

e.printStackTrace();

}

}

//处理输入的单词的

public void inputWord()

{

try {

RandomAccessFile outfile = new RandomAccessFile(file, "rw");

if (file.exists()) {

//实现指针跳到文件末尾

long length = file.length();

outfile.seek(length);

}

outfile.writeUTF("单词:"+word.getText());

outfile.writeUTF("解释:"+note.getText());

outfile.close();

} catch (Exception e) {

e.printStackTrace();

}

}

public static void main(String[] args) {

new F();

}

}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package shiyan; import java.awt.*; import java.awt.event.*; import java.sql.*; import javax.swing.*; public class AddWin extends JFrame implements ActionListener { private static MySqlUtils mySqlUtils = new MySqlUtils(); JTextField 添加汉语解释_文本条, 添加英语单词_文本条; JButton addbtn, cancelbtn; Connection Con = null; Statement Stmt = null; public AddWin() { super("添加单词"); this.setBounds(250, 250, 250, 200); this.setVisible(true); JPanel p1 = new JPanel(); p1.add(new Label("输入要添加的单词:")); 添加英语单词_文本条 = new JTextField(20); p1.add(添加英语单词_文本条); p1.add(new Label("输入添加的单词的解释:")); 添加汉语解释_文本条 = new JTextField(20); p1.add(添加汉语解释_文本条); addbtn = new JButton("提交"); cancelbtn = new JButton("取消"); p1.add(addbtn); p1.add(cancelbtn); this.add(p1); addbtn.addActionListener(this); cancelbtn.addActionListener(this); this.validate(); } public void actionPerformed(ActionEvent e) { if (e.getSource() == addbtn) { if (添加英语单词_文本条.getText().equals("") || 添加汉语解释_文本条.getText().equals("")) { JOptionPane.showMessageDialog(this, "添加的单词或解释不能为空~", "警告", JOptionPane.WARNING_MESSAGE); } else { try { Word word = new Word(); word.setEnglish(添加英语单词_文本条.getText().toString()); word.setChinese(添加汉语解释_文本条.getText().toString()); mySqlUtils.insert(word); 添加英语单词_文本条.setText(""); 添加汉语解释_文本条.setText(""); } catch (Exception ee) { } } } else if (e.getSource() == cancelbtn) { dispose(); } } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值