||
程序代码:
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.util.regex.*;
import java.io.*;
public class Exercise {
public static void main(String args[]){
new Dwindow();
}
}
class Dwindow extends JFrame implements ActionListener{
JButton inputNumber;
JTextArea save;
Pattern p;
Matcher m;
File file=new File("data.txt");
Dwindow(){
inputNumber=new JButton("单击按钮打开输入对话框");
inputNumber.addActionListener(this);
save=new JTextArea(12,16);
add(inputNumber,BorderLayout.NORTH);
add(new JScrollPane(save),BorderLayout.CENTER);
p=Pattern.compile("\\D+");
setBounds(60,60,300,300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
String str=JOptionPane.showInputDialog(null,"请输入数字字符","输入对话框",JOptionPane.INFORMATION_MESSAGE);
if(str!=null){
m=p.matcher(str);
while(m.find()){
JOptionPane.showMessageDialog(this,"你输入了非法字符","消息对话框",JOptionPane.WARNING_MESSAGE);
str=JOptionPane.showInputDialog(null,"请输入数字字符序列");
m=p.matcher(str);
}
if(Integer.parseInt(str)>1000){
int n=JOptionPane.showConfirmDialog(this,"你输入的数字大于1000,是否继续将数字存入文件?","确认对话框",JOptionPane.YES_NO_CANCEL_OPTION);
if(n==JOptionPane.YES_OPTION)
save.append("\n"+str);
}
else
save.append("\n"+str);
byte b[]=str.getBytes();
try{
FileOutputStream out=new FileOutputStream(file,true);
OutputStreamWriter writer=new OutputStreamWriter(out);
//out.write(b);
//out.close();
writer.write(str+'\0');
writer.close();
out.close();
}
catch(IOException f){
System.out.println(f);
}
}
}
}