📄 protoframe.java
字号:
//package com.spreadsheet;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.*;
import javax.swing.table.*;
import javax.swing.event.*;
import javax.swing.*;
class ProtoFrame extends JFrame implements ActionListener, TableModelListener {
JTable table = null;
private String filename;
//private int rowcount;
//private int colcount;
String[][] value = new String[50][30];
TableSorter sorter;
ProtoFrame(String filename, int rowcount, int colcount) {
this.filename = filename;
JPanel menupanel = new JPanel(new BorderLayout());
JButton button = new JButton("SAVE");
button.addActionListener(this);
menupanel.add(button, BorderLayout.NORTH);
getContentPane().add(menupanel, BorderLayout.NORTH);
DefaultTableModel model = new DefaultTableModel(rowcount, colcount);
DefaultTableModel rowheadmodel = new DefaultTableModel(rowcount,colcount);
sorter = new TableSorter(model);
table = new JTable(sorter);
//table.getModel().addTableModelListener(this);
sorter.setTableHeader(table.getTableHeader());
sorter.getTableModel().addTableModelListener(this);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.setCellSelectionEnabled(true);
JTable rowHeaderTable = new JTable(rowheadmodel);
rowheadmodel.setColumnCount(1);
rowheadmodel.setColumnIdentifiers(new Object[] { "" });
rowHeaderTable.setEnabled(false);
rowHeaderTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
rowHeaderTable.getColumnModel().getColumn(0).setPreferredWidth(50);
//为rowHeaderTable设置预设的绘制器
rowHeaderTable.setDefaultRenderer(rowHeaderTable.getColumnClass(0),
new RowHeaderRenderer(table));
rowHeaderTable.setPreferredScrollableViewportSize(new Dimension(
rowHeaderTable.getColumnModel().getColumn(0)
.getPreferredWidth(), 0));
//将table加入JScrollPane
JScrollPane scrollPane = new JScrollPane(table);
//将rowHeaderTable作为row header加入JScrollPane的RowHeaderView区域
scrollPane.setRowHeaderView(rowHeaderTable);
//this.getContentPane().add(scrollPane);
getContentPane().add(scrollPane, BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
loadData();
}
public class RowHeaderRenderer extends JLabel implements TableCellRenderer {
JTable reftable;
public RowHeaderRenderer(JTable reftable) {
this.reftable = reftable;
}
public Component getTableCellRendererComponent(JTable table,
Object obj, boolean isSelected, boolean hasFocus, int row,
int col) {
((DefaultTableModel) table.getModel()).setRowCount(reftable
.getRowCount());
JTableHeader header = reftable.getTableHeader();
this.setOpaque(true);
/* 设为TableHeader的边框类型 */
setBorder(UIManager.getBorder("TableHeader.cellBorder"));
setHorizontalAlignment(CENTER);
/* 设置背景色为TableHeader的背景色 */
setBackground(header.getBackground());
Font font = header.getFont();
if (isSelect(row)) /* 当选取单元格时,在row header上设置成选取颜色 */
{
setForeground(Color.white);
setBackground(Color.lightGray);
setFont(font);
setText(String.valueOf(row + 1));
} else {
setForeground(header.getForeground());
setFont(font);
setText(String.valueOf(row + 1));
}
return this;
}
private boolean isSelect(int row) {
int[] sel = reftable.getSelectedRows();
for (int i = 0; i < sel.length; i++) {
if (sel[i] == row)
return true;
}
return false;
}
}
public void actionPerformed(ActionEvent ae) {
if (ae.getActionCommand() == "SAVE")
printData();
}
/**
*
*/
public void loadData() {
try {
RandomAccessFile myfilestream = new RandomAccessFile(filename, "r");
String s = "";
while ((s = myfilestream.readLine()) != null) {
StringTokenizer st = new StringTokenizer(s, ";");
while (st.hasMoreTokens()) {
try {
String sr = st.nextToken();
String sl = st.nextToken();
String svalue = st.nextToken();
int row = Integer.parseInt(sr) - 1;
int col = Integer.parseInt(sl) - 1;
table.setValueAt(svalue, row, col);
} catch (Exception e1) {
}
}
}
myfilestream.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}
public void printData() {
byte[] buff = new byte[1024];
ByteArrayOutputStream bytearrayoutputstream1 = new ByteArrayOutputStream();
int col = table.getColumnCount();
int row = table.getRowCount();
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
String s = value[i][j];
if (s != null)
try {
s = ((i + 1) + ";" + (j + 1) + ";" + s);
s = s + "\r\n";
bytearrayoutputstream1.write(s.getBytes());
} catch (IOException e1) {
//e1.printStackTrace();
}
}
}
try {
FileOutputStream outfile = new FileOutputStream(filename);
bytearrayoutputstream1.writeTo(outfile);
outfile.close();
} catch (IOException e1) {
//e1.printStackTrace();
}
}
public void tableChanged(TableModelEvent e) {
int col = e.getColumn();
int row = e.getFirstRow();
String s = (String) table.getValueAt(row, col);
boolean flag=value[row][col]==null;
value[row][col] = s;
if (s.startsWith("=")) {
if (s.startsWith("=countif")) {
char a = s.charAt(9);
char b = s.charAt(10);
char c = s.charAt(12);
char d = s.charAt(13);
int i1 = (int) a - 65;
int j1 = (int) b - 49;
int i2 = (int) c - 65;
int j2 = (int) d - 49;
String condition = s.substring(15, s.length() - 1);
int count = 0;
if (condition.startsWith("\"")) {
condition = condition.substring(1, condition.length() - 1);
for (int i = i1; i <= i2; i++)
for (int j = j1; j < j2; j++) {
try {
String ss = (String) table.getValueAt(j, i);
if (ss.equals(condition))
count++;
} catch (Exception e1) {
//e1.printStackTrace();
}
}
} else {
if (condition.startsWith(">")) {
condition = condition.substring(1);
double comparevalue = Double.parseDouble(condition);
for (int i = i1; i <= i2; i++)
for (int j = j1; j <= j2; j++) {
try {
Double ss = (Double) table.getValueAt(j, i);
if (ss.intValue() > comparevalue)
count++;
} catch (Exception e1) {
}
}
} else if (condition.startsWith("<")) {
condition = condition.substring(1);
double comparevalue = Double.parseDouble(condition);
for (int i = i1; i <= i2; i++)
for (int j = j1; j < j2; j++) {
try {
Double ss = (Double) table.getValueAt(j, i);
if (ss.intValue() < comparevalue)
count++;
} catch (Exception e1) {
//e1.printStackTrace();
}
}
} else if (condition.startsWith("=")) {
condition = condition.substring(1);
double comparevalue = Double.parseDouble(condition);
for (int i = i1; i <= i2; i++)
for (int j = j1; j < j2; j++) {
try {
Double ss = (Double) table.getValueAt(j, i);
if (ss.intValue() == comparevalue)
count++;
} catch (Exception e1) {
//e1.printStackTrace();
}
}
} else if (condition.startsWith(">=")) {
condition = condition.substring(2);
double comparevalue = Double.parseDouble(condition);
for (int i = i1; i <= i2; i++)
for (int j = j1; j < j2; j++) {
try {
Double ss = (Double) table.getValueAt(j, i);
if (ss.intValue() >= comparevalue)
count++;
} catch (Exception e1) {
//e1.printStackTrace();
}
}
} else if (condition.startsWith("<=")) {
condition = condition.substring(2);
double comparevalue = Double.parseDouble(condition);
for (int i = i1; i <= i2; i++)
for (int j = j1; j < j2; j++) {
try {
Double ss = (Double) table.getValueAt(j, i);
if (ss.intValue() <= comparevalue)
count++;
} catch (Exception e1) {
//e1.printStackTrace();
}
}
}
}
try {
table.setValueAt(new Integer(count), row, col);
} catch (Exception e1) {
}
} else if (s.charAt(1) >= 'A' && s.charAt(1) <= 'Z') {
try {
char a = s.charAt(1);
char b = s.charAt(2);
int i = (int) a - 65;
int j = (int) b - 49;
table.setValueAt(table.getValueAt(j, i), row, col);
} catch (Exception e1) {
//e1.printStackTrace();
}
} else {
try {
FormulaParser fp = new FormulaParser(s.substring(1));
Double DD = new Double(fp.getResult());
table.setValueAt(DD, row, col);
} catch (Exception e1) {
}
}
} else if (s.startsWith("'")) {
table.setValueAt(s.substring(1), row, col);
} else if (flag){
try {
double dd = Double.parseDouble(s);
Double DD = new Double(dd);
table.setValueAt(DD, row, col);
} catch (Exception ex) {
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -