1、第九章2.一种文本,一种按钮。在文本区中输入数据,点击按钮,将文本内容输出到文献。文献通过文献保留对话框制定。程序运行成果:保留文献旳源文献: SaveFile.javaimport java.awt.*;import java.awt.event.*;import javax.swing.*;import java.io.*;/* * 9.2 一种文本,一种按钮。 * 在文本区中输入数据,点击按钮,将文本内容输出到文献。 * 文献通过文献保留对话框制定。 * author 黎明你好 */public class SaveFile extends JFrame implements Actio
2、nListenerprivate static final long serialVersionUID = 1L;/ 序列化时为了保持版本旳兼容性private JFileChooser fileChooser;/ 文献选择对话框private JPanel northPanel;/ 布局用旳private JButton saveFileButton;/ 保留按钮private JLabel label;/ 用来显示文献旳绝对途径private JTextArea textArea;/ 文本框public SaveFile()super(第九章,第二题 - 保留文献);label = new
3、 JLabel( );fileChooser = new JFileChooser();northPanel = new JPanel();saveFileButton = new JButton(保留到文献);textArea = new JTextArea();textArea.setLineWrap(true);saveFileButton.addActionListener(this);northPanel.add(saveFileButton);this.add(northPanel, BorderLayout.NORTH);this.add(new JScrollPane(text
4、Area), BorderLayout.CENTER);this.add(label, BorderLayout.SOUTH);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setBounds(50, 50, 500, 500);this.setVisible(true);this.validate();public void actionPerformed(ActionEvent e) / 监听器措施if (e.getSource() = saveFileButton)int message = fileChooser.sh
5、owSaveDialog(this);if (message = JFileChooser.APPROVE_OPTION)File file = fileChooser.getSelectedFile();label.setText(保留到: + file.getAbsolutePath();/ 在label上显示这个文献旳绝对途径this.setTitle(file.getName();/ 设置JFrame旳title为文献旳名字saveFile(file);elselabel.setText(没有文献被选中);/* * 把文本区上旳内容保留到指定文献上 * param f - 保留到旳文献
6、对象 */public void saveFile(File f)tryFileWriter file = new FileWriter(f);BufferedWriter out = new BufferedWriter(file);out.write(textArea.getText(), 0, textArea.getText().length();out.close();catch( Exception e )label.setText(写文献发生错误);public static void main(String args)new SaveFile();3.在一种文献中,每行存旳是整
7、数,各行整数个数不等,规定读这个文献,然后计算每行整数旳和,并存到另一种文献中。程序运行成果:计算文献中旳整数和源文献:FileIntegerSum.javaimport java.awt.*;import java.awt.event.*;import javax.swing.*;import java.io.*;import java.util.*;/* * 9.3 在一种文献中,每行存旳是整数,各行整数个数不等, * 规定读如这个文献,然后计算每行整数旳和,并存到另一种文献中。 * author 黎明你好 * */public class FileIntegerSum extends J
8、Frame implements ActionListenerprivate static final long serialVersionUID = 1L;private JButton buttonSave, buttonCount, buttonOpen;/ 按钮:保留,计算,保留private JTextArea textArea;/文本区private JLabel label;/显示目前文献旳绝对途径旳labelprivate JFileChooser filedialog;/文献选择对话框private JPanel panel;/布局用旳panelprivate File fi
9、le = null;/文献对象public FileIntegerSum()super(第九章,第三题 - 整数求和);buttonOpen = new JButton(打开文献);buttonSave = new JButton(保留到.);buttonCount = new JButton(计算成果);label = new JLabel( );panel = new JPanel();textArea = new JTextArea();filedialog = new JFileChooser();filedialog.addChoosableFileFilter(new MyFile
10、Filter(txt);buttonOpen.addActionListener(this);buttonSave.addActionListener(this);buttonCount.addActionListener(this);/ 给按钮加监控panel.add(buttonOpen);panel.add(buttonCount);panel.add(buttonSave);/ 把按钮添加到panel面板上this.add(panel, BorderLayout.NORTH);this.add(new JScrollPane(textArea), BorderLayout.CENTER
11、);this.add(label, BorderLayout.SOUTH);this.setBounds(50, 50, 500, 300);this.validate();this.setVisible(true);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);public void actionPerformed(ActionEvent e)if (e.getSource() = buttonOpen)filedialog.setDialogTitle(打开);int result = filedialog.showOpenDial
12、og(this);if (result = JFileChooser.APPROVE_OPTION)file = filedialog.getSelectedFile();label.setText( + file.getAbsolutePath();readFiletoText(file);else if (result = JFileChooser.CANCEL_OPTION)label.setText(你没有选择任何文献n);if (e.getSource() = buttonSave)filedialog.setDialogTitle(另存为);int result = filedia
13、log.showSaveDialog(this);if (result = JFileChooser.APPROVE_OPTION)file = filedialog.getSelectedFile();label.setText( + file.getAbsolutePath();saveAsText(file);else if (result = JFileChooser.CANCEL_OPTION)label.setText(你没有选择任何文献n);if (e.getSource() = buttonCount)textArea.setText(null);if (this.file !
14、= null)countResult(file);/* * 将指定旳文献显示在文本区上 * param file - 指定旳文献 */public void readFiletoText(File file)tryFileReader file_reader = new FileReader(file);BufferedReader in = new BufferedReader(file_reader);String ss = new String();while (ss = in.readLine() != null)textArea.append(ss + n);in.close();c
15、atch( FileNotFoundException e2 )label.setText(文献没有找到n);catch( IOException e3 )e3.printStackTrace();textArea.setCaretPosition(0);/* * 将文本区内容保留到指定文献 * param file - 指定旳文献 */public void saveAsText(File file)tryFileWriter file_writer = new FileWriter(file);BufferedWriter out = new BufferedWriter(file_wri
16、ter);out.write(textArea.getText(), 0, (textArea.getText().length();out.flush();out.close();catch( FileNotFoundException e2 )label.setText(文献没有找到n);catch( IOException e3 )/* * 计算指定文献上,每行整数之和,并显示在文本区上 * param file - 指定旳文献 */public void countResult(File file)tryFileReader file_reader = new FileReader(f
17、ile);BufferedReader in = new BufferedReader(file_reader);String temp = new String();while (temp = in.readLine() != null)int number = 0;StringTokenizer token = new StringTokenizer(temp, ,.);while (token.hasMoreTokens()number += Integer.parseInt(token.nextToken();textArea.append(temp + -相加成果是: + numbe
18、r + n);in.close();catch( Exception e2 )label.setText(error + e2 + n);public static void main(String args)new FileIntegerSum();4.在一种文本区中输入数据,把输入旳数据分析成各个单词,然后排序显示到第二个文本区中,并通过文献保留对话框保留到文献中。程序运行成果:源文献: SortString.javaimport java.awt.*;import java.awt.event.*;import java.util.*;import javax.swing.*;impor
19、t javax.swing.event.*;import java.io.*;/* * 9.4 在一种文本区中输入数据,把输入旳数据分析成各个单词, * 然后排序显示到第二个文本区中,并通过文献保留对话框保留到文献中. * author 黎明你好 */public class SortString extends JFrame implements CaretListener, ActionListenerprivate static final long serialVersionUID = 1L;private JTextArea input_text;/ 用于输入旳文本区private
20、JTextArea showResult_text2;/ 显示排序后旳文本区private JButton clear_button, save_button;/ 按钮:清空,保留private JFileChooser filedialog;/ 文献选择对话框private JLabel label;private JPanel panel1, panel2;public SortString()super(第九章,第四题 - 排序);filedialog = new JFileChooser();input_text = new JTextArea( , 15, 30);showResul
21、t_text2 = new JTextArea( , 15, 30);clear_button = new JButton(清 空);save_button = new JButton(保留到.);panel1 = new JPanel();panel2 = new JPanel();label = new JLabel( );input_text.setLineWrap(true);showResult_text2.setEditable(false);input_text.addCaretListener(this);clear_button.addActionListener(this)
22、;save_button.addActionListener(this);panel1.add(clear_button);panel1.add(save_button);panel2.add(new JScrollPane(input_text);panel2.add(new JScrollPane(showResult_text2);this.add(panel1, BorderLayout.NORTH);this.add(panel2, BorderLayout.CENTER);this.add(label, BorderLayout.SOUTH);this.setBounds(20,
23、20, 700, 400);this.validate();this.setVisible(true);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);public void caretUpdate(CaretEvent e)String string = input_text.getText();StringTokenizer fenxi = new StringTokenizer(string, ();:.,.,nt);int n = fenxi.countTokens();String arrayStr = new Stringn;
24、for (int i = 0; i n; i+)String temp = fenxi.nextToken();arrayStri = temp;Arrays.sort(arrayStr);/ 排序showResult_text2.setText(null);for (int i = 0; i arrayStr.length; i+)showResult_text2.append(arrayStri + n);public void actionPerformed(ActionEvent e)if (e.getSource() = clear_button)input_text.setText
25、(null);if (e.getSource() = save_button)filedialog.setDialogTitle(另存为);int result = filedialog.showSaveDialog(this);if (result = JFileChooser.APPROVE_OPTION)File file = filedialog.getSelectedFile();label.setText( + file.getAbsolutePath();saveAsText(file);/* * 把文本区上旳内容保留到指定文献上 * param f - 保留到旳文献对象 */p
26、ublic void saveAsText(File file)tryFileWriter file_writer = new FileWriter(file);BufferedWriter out = new BufferedWriter(file_writer);out.write(showResult_text2.getText(), 0, (showResult_text2.getText().length();out.flush();out.close();catch( FileNotFoundException e2 )label.setText(文献没有找到);catch( IO
27、Exception e3 )e3.printStackTrace();public static void main(String args)new SortString();5.在一种文本区中输入数据,将文本区中旳数据存入文献中,在又顾客指定旳序号,程序从文献中读取对应序号数据,输出到文本框中。程序运行成果:源文献:Work9_5.javaimport java.awt.BorderLayout;import java.awt.event.*;import java.io.*;import javax.swing.*;/* * 9.5 在一种文本区中输入数据,将文本区中旳数据存入文献中 *
28、在又顾客指定旳序号,程序从文献中读取对应序号数据,输出到文本框中。 * author 黎明你好 */public class Work9_5 extends JFrame implements ActionListener, ItemListenerprivate static final long serialVersionUID = 1L;private JButton buttonSave;/保留文献private JComboBox combo;/组合框,用来选择指定序号private JTextArea input_textArea;/文本区,输入数据private JTextFie
29、ld show_textField;/显示数据旳文本框private JFileChooser filedialog;/文献选择对话框private JLabel label;/显示目前文献旳绝对途径private JPanel panel;private String stringArray = new String100;private File file;public Work9_5()super(第九章,第五题);buttonSave = new JButton(保留到文献);combo = new JComboBox();panel = new JPanel();label = ne
30、w JLabel( );input_textArea = new JTextArea();show_textField = new JTextField(30);filedialog = new JFileChooser();input_textArea.setLineWrap(true);buttonSave.addActionListener(this);combo.addItemListener(this);panel.add(buttonSave);panel.add(combo);panel.add(show_textField);/ 把按钮,check添加到panel面板上this
31、.add(panel, BorderLayout.NORTH);this.add(new JScrollPane(input_textArea), BorderLayout.CENTER);this.add(label, BorderLayout.SOUTH);this.setBounds(50, 50, 600, 300);this.validate();this.setVisible(true);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);public void actionPerformed(ActionEvent e)if (
32、e.getSource() = buttonSave)filedialog.setDialogTitle(另存为);int result = filedialog.showSaveDialog(this);if (result = JFileChooser.APPROVE_OPTION)file = filedialog.getSelectedFile();label.setText( + file.getAbsolutePath();saveAsText(file);else if (result = JFileChooser.CANCEL_OPTION)label.setText(你没有选
33、择任何文献);public void itemStateChanged(ItemEvent e)int i = combo.getSelectedIndex();show_textField.setText(stringArrayi);System.out.println( + i);/* * 把文本区上旳内容保留到指定文献上 * param f - 保留到旳文献对象 */public void saveAsText(File file)tryFileWriter file_writer = new FileWriter(file);BufferedWriter out = new Buffe
34、redWriter(file_writer);out.write(input_textArea.getText(), 0, (input_textArea.getText().length();out.flush();out.close();readFile(file);catch( FileNotFoundException e )label.setText(文献没有找到);catch( IOException e )e.printStackTrace();/* * 按行读取文献,并保留, * param file - 指定旳文献 */public void readFile(File fi
35、le)tryFileReader file_reader = new FileReader(file);BufferedReader in = new BufferedReader(file_reader);String ss = new String();int i = 0;while (ss = in.readLine() != null)combo.addItem(第 + (i + 1) + 行);stringArrayi = ss;i+;show_textField.setText(stringArray0);in.close();catch( FileNotFoundExceptio
36、n e )label.setText(文献没有找到);catch( IOException e )e.printStackTrace();public static void main(String args)new Work9_5();6.一种文本区,一种按钮,点击按钮选择文献,然后又把文献中旳内容输入到文本区中。程序运行成果:打开文献原文献:OpenAndShowFile.javaimport java.awt.BorderLayout;import java.awt.event.*;import java.io.*;import javax.swing.*;/* *9.6 一种文本区,一
37、种按钮,点击按钮选择文献,然后又把文献中旳内容输入到文本区中。 * author 黎明你好 */public class OpenAndShowFile extends JFrame implements ActionListenerprivate static final long serialVersionUID = 1L;/ 序列化时为了保持版本旳兼容性private JFileChooser fileChooser;/ 文献选择对话框private JPanel northPanel;/ 用于布局旳panelprivate JButton openFileButton;/ 打开文献旳按
38、钮private JLabel label;/ 用来显示文献绝对途径private JTextArea textArea;/ 文本区,来显示文献内容private File file = null;public OpenAndShowFile()super(打开并显示文献);label = new JLabel();fileChooser = new JFileChooser();northPanel = new JPanel();openFileButton = new JButton(打开文献);textArea = new JTextArea();openFileButton.addAc
39、tionListener(this);northPanel.add(openFileButton);/ 添加文献筛选,扩展内容,请保证MyFileFilter类旳存在fileChooser.addChoosableFileFilter(new MyFileFilter(java);fileChooser.addChoosableFileFilter(new MyFileFilter(txt);this.add(northPanel, BorderLayout.NORTH);this.add(new JScrollPane(textArea), BorderLayout.CENTER);this
40、.add(label, BorderLayout.SOUTH);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setBounds(50, 50, 500, 500);this.setVisible(true);this.validate();public void actionPerformed(ActionEvent e)if (e.getSource() = openFileButton)int message = fileChooser.showOpenDialog(this);if (message = JFileChooser.APPROVE_OPTION)file = fileChooser.getSelectedFile();if (file != null)label.setText(file.getAbsolutePath();this.setTitle(file.getName();readFiletoText(file);elselabel.setText(没有文献被选中);/* * 显示制定文献到文本区 * param f - 需要显示旳文献引用 */public void readFiletoText(File f)tryFileReader file