📄 adddic.java
字号:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.awt.event.*;
import java.applet.*;
public class AddDic extends JApplet implements ActionListener{
JLabel jlInput = new JLabel("输入要添加的单词");
JLabel jlExplanation = new JLabel("输入添加单词的解释");
JTextField jtfInput = new JTextField();
JTextField jtfExplanation = new JTextField();
String[] inputArray;
int lastPosition;
JButton jbthandUp = new JButton("提交") ;
AudioClip audioClip = Applet.newAudioClip(this.getClass().getResource("wb022.wav"));
public AddDic(){
}
public void init()
{
//setVisible(true);
//setSize(200,150);
JPanel jpanel = new JPanel();
jpanel.setLayout(new GridLayout(4,1));
jpanel.add(jlInput);
jpanel.add(jtfExplanation);
jpanel.add(jlExplanation);
jpanel.add(jtfInput);
// creat jbtPanel to hold the button
JPanel jbtPanel = new JPanel();
jbtPanel.add( jbthandUp);
getContentPane().add(jpanel,BorderLayout.CENTER);
getContentPane().add(jbtPanel,BorderLayout.SOUTH);
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setSize(200,150);
frame.getContentPane().add(this);
jbthandUp.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
/* create a object of Search to invoke the method the check the word whether exits*/
Search searchWord=new Search();
if(e.getSource()==jbthandUp){
//判断输入框是否为空
String wordToAdd ="";
String wordToTranslate ="";
wordToAdd+=jtfInput.getText();
wordToTranslate+= jtfExplanation.getText();
String notice = "添加的单词或解释不能为空!";
if((wordToAdd.equals(""))||wordToTranslate.equals("")){
audioClip.play();
JOptionPane.showMessageDialog(null,notice);
}
else {
if(!searchWord.haveTheWord(jtfInput.getText(), inputArray)){
try{
FileWriter output = new FileWriter("library.txt",true);
output.append(wordToAdd+" ");
output.append(wordToTranslate+" ");
inputArray[lastPosition]=wordToAdd;
inputArray[lastPosition+1]=wordToTranslate;
audioClip.play();
JOptionPane.showMessageDialog(null, "恭喜你!添加成功");
jtfInput.setText(null);
jtfExplanation.setText(null);
output.close();
}
catch(FileNotFoundException ee){}
catch(IOException eee){};
}
else{
JOptionPane.showMessageDialog(null, "此词汇已存在!");
audioClip.play();
jtfInput.setText(null);
jtfExplanation.setText(null);
}
}
}
}
public String[] showAddDic(String[]array){
inputArray = new String[array.length+2];
System.arraycopy(array, 0, inputArray, 0, array.length);
lastPosition=array.length;
init();
return inputArray;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -