?? fileoutputdemo.java
字號:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
class FileOutputDemo extends JFrame
implements ActionListener {
private JTextArea textArea;
private JButton saveButton;
private JTextField nameField;
private JLabel nameLabel;
private PrintWriter outFile;
public static void main(String [] args) {
FileOutputDemo frame = new FileOutputDemo();
frame.setSize(400, 300);
frame.createGUI();
frame.show();
}
private void createGUI() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container window = getContentPane();
window.setLayout(new FlowLayout());
nameLabel = new JLabel("File name: ");
window.add(nameLabel);
nameField = new JTextField(20);
window.add(nameField);
textArea = new JTextArea(10,30);
JScrollPane scrollPane = new JScrollPane(textArea);
window.add(scrollPane);
saveButton = new JButton("save");
window.add( saveButton);
saveButton.addActionListener(this);
}
public void actionPerformed(ActionEvent event) {
if (event.getSource() == saveButton ) {
try{
outFile = new PrintWriter(
new FileWriter(nameField.getText()), true);
outFile.print(textArea.getText());
outFile.close();
}
catch (IOException e) {
JOptionPane.showMessageDialog(null,
"File Error: " + e.toString());
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -