You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
3.3 KiB

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.table.*;
import java.math.*;
public class test02 implements ActionListener{
File file;
FileReader fileReader;
JFileChooser fileChooser;
JFrame frame;
JButton button;
JButton b;
DefaultTableModel tableModel;
JTable table;
JScrollPane scrollPane;
public test02() {
frame=new JFrame("read");
tableModel=new DefaultTableModel();
table=new JTable(tableModel);
scrollPane=new JScrollPane(table);
frame.add(scrollPane,BorderLayout.CENTER);
b = new JButton("加工资");
button=new JButton("选择文本文件");
frame.add(button,BorderLayout.SOUTH);
frame.add(b,BorderLayout.EAST);
frame.setSize(400, 200);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button.addActionListener(this);
b.addActionListener(this);
}
public void actionPerformed(ActionEvent event) {
if(event.getSource()==button) {
fileChooser=new JFileChooser("D:\\java库\\salary.txt");
if(fileChooser.showOpenDialog(frame)==JFileChooser.APPROVE_OPTION) {
file=fileChooser.getSelectedFile();
try {
fileReader=new FileReader(file);
Vector<String> vector=new Vector<String>();
vector.add("姓名");
vector.add("工资");
vector.add("津贴");
Vector<Vector<String>> dataVector=new Vector<Vector<String>>();
Vector<String> dataVector2=new Vector<String>();
String string="";
while(fileReader.ready()) {
char ch=(char) fileReader.read();
if(ch==' ') {
dataVector2.add(string);
string="";
}
else if(ch=='\n') {
dataVector2.add(string);
string="";
dataVector.add(dataVector2);
dataVector2=new Vector<String>();
}
else {
string=string+ch;
}
}
tableModel.setDataVector(dataVector, vector);
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
}
}
if(event.getSource()==b){
for(int i=0;i<table.getRowCount();i++) {
Object s = table.getValueAt(i, 1);
double m = Double.parseDouble(s.toString());
m *= 1.10;
BigDecimal bg = new BigDecimal(m);
double f1 = bg.setScale(2, RoundingMode.HALF_UP).doubleValue();
table.setValueAt(Double.toString(f1), i, 1);
}
}
}
public static void main(String args[]) {
new test02();
}
}