62,614
社区成员
发帖
与我相关
我的任务
分享
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FileDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.awt.event.ActionEvent;
import java.awt.Color;
public class Text7 extends JFrame {
private JPanel contentPane;
private JTextField textField;
public FileDialog open, save;
JTextArea textArea;
String arr[][] = new String[50][4];
int count = 0;
double increase = 0, total = 0;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Text7 frame = new Text7();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Text7() {
setTitle("\u804C\u5DE5\u5DE5\u8D44\u4FE1\u606F");
setBounds(100, 100, 266, 385);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
save = new FileDialog(this,"保存",FileDialog.SAVE);
open = new FileDialog(this,"打开",FileDialog.LOAD);
JLabel label = new JLabel("\u59D3\u540D");
label.setBounds(10, 0, 59, 33);
contentPane.add(label);
JLabel label_1 = new JLabel("\u5DE5\u8D44");
label_1.setBounds(93, 0, 59, 33);
contentPane.add(label_1);
JLabel label_2 = new JLabel("\u6D25\u8D34");
label_2.setBounds(196, 0, 59, 33);
contentPane.add(label_2);
textArea= new JTextArea();
textArea.setBounds(0, 25, 255, 207);
contentPane.add(textArea);
JButton button = new JButton("\u6253\u5F00");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String str;
open.setVisible(true);
try {
File f1 = new File(open.getDirectory(), open.getFile());
FileReader fr = new FileReader(f1);
BufferedReader br = new BufferedReader(fr);
str = br.readLine();//读每一行,null时结束
int i = 0;
while (str!=null) {
textArea.append(str + '\n');
arr[i] = str.split("\t\t");
str = br.readLine();
i++;
count++;
}
fr.close();
} catch (Exception e_1) {
}
}
});
button.setBackground(Color.WHITE);
button.setBounds(10, 254, 65, 38);
contentPane.add(button);
JButton button_1 = new JButton("\u4FDD\u5B58");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
increase = Double.parseDouble(textField.getText());
for (int j = 0; j < count; j++) {
total = Double.parseDouble(arr[j][1]);
total = total * (1 + increase);
arr[j][1] = Double.toString(total);
}
save.setVisible(true);
try {
File f1 = new File(save.getDirectory(), save.getFile());
FileWriter fw = new FileWriter(f1);
BufferedWriter f = new BufferedWriter(fw);
for (int k = 0; k < count; k++) {
for (int r = 0; r < 3; r++) {
f.write(arr[k][r] + "\t\t");
}
f.write("\r\n");
}
f.close();
} catch (Exception e_1) {
}
}
});
button_1.setBackground(Color.WHITE);
button_1.setBounds(94, 254, 65, 38);
contentPane.add(button_1);
JButton btnNewButton = new JButton("\u6E05\u5C4F");
btnNewButton.setBackground(Color.WHITE);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
textArea.setText("");
} catch (Exception e_1) {
}
}
});
btnNewButton.setBounds(175, 254, 65, 38);
contentPane.add(btnNewButton);
JLabel label_3 = new JLabel("\u589E\u52A0\u7387");
label_3.setBounds(67, 301, 47, 45);
contentPane.add(label_3);
textField = new JTextField();
textField.setBounds(124, 310, 66, 27);
contentPane.add(textField);
textField.setColumns(10);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}