我正在GridLayout中创建基于JTextFields的excel电子表格。我希望能够在单元格中键入内容,按Enter键,并根据输入字符串将单元格设置为适当的子类。
Cell I唯一正在工作的子类是DateCell。我可以成功地将子类设置在2d Cell数组中的正确位置;但是,这不会显示在GUI中,并且我当前的刷新方法不起作用。我希望它更新JFrame以在JTextFields中显示新文本。
任何关于为什么刷新方法不起作用的建议,刷新方法如何工作,或者在没有刷新方法的情况下执行程序的方法都将深表赞赏。谢谢!
这是GUI类:
package spreadsheet;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JTextField;
import cell.*;
public class GUI extends JFrame {
public static final int ASCII_SHIFT = 64;
private JTextField[][] grid;
private Cell[][] sheet;
public GUI(int height, char letter) {
//sets the title to "TextExcel"
super("TextExcel");
//creates multidimensional Cell array
sheet = new Cell[height][letter - ASCII_SHIFT + 1];
//creates an array of text fields
grid = new JTextField[sheet.length + 1][sheet[0].length];
//creates a grid layout
setLayout(new GridLayout(grid.length, grid[0].length));
//create event handler
Handler handler = new Handler();
//makes the corner box blank
grid[0][0] = new JTextField();
grid[0][0].setEditable(false);
//numbers the rows
for (int i = 1; i < grid.length; i++) {
grid[i][0] = new JTextField(Integer.toString(i));
grid[i][0].setEditable(false);
}
//letters the columns
for (int j = 1; j < grid[0].length; j++) {
byte[] character = {(byte) (j + ASCII_SHIFT)};
grid[0][j] = new JTextField(new String(character));
grid[0][j].setEditable(false);
}
//initializes the cells
for (int row = 1; row < grid.length; row++) {
for (int col = 1; col < grid[0].length; col++) {
if (sheet[row-1][col-1] == null) {
grid[row][col] = new JTextField();
} else {
grid[row][col] = new JTextField(sheet[row-1][col-1].toString());
}
grid[row][col].addActionListener(handler);
}
}
//adds the boxes to the GUI
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
grid[i][j].setHorizontalAlignment(JTextField.CENTER);
add(grid[i][j]);
}
}
}
public GUI() {
this(10, 'G');
}
//will eventually return proper subclass
public Cell parse(String s) {
Cell output = new DateCell(s);
return output;
}
public void refresh() {
for (int row = 1; row < grid.length; row++) {
for (int col = 1; col < grid[0].length; col++) {
if (sheet[row - 1][col - 1] != null && !grid[row][col].getText().equals(sheet[row - 1][col - 1].toString())) {
grid[row][col] = new JTextField(sheet[row - 1][col - 1].toString());
System.out.println(sheet[row - 1][col - 1].toString());
}
}
}
}
private class Handler implements ActionListener {
public void actionPerformed(ActionEvent e) {
for (int row = 1; row < grid.length; row++) {
for (int col = 1; col < grid[0].length; col++) {
if (grid[row][col] == e.getSource()) {
sheet[row - 1][col - 1] = parse(e.getActionCommand());
refresh();
}
}
}
}
}
}
以下是客户端代码:
package client;
import javax.swing.JFrame;
import spreadsheet.GUI;
public class Program {
public static void main(String[] args) {
GUI gui = new GUI();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setSize(1000, 618);
gui.setVisible(true);
}
}
Cell类就是这样:
package cell;
abstract public class Cell {
}
和DateCell类:
package cell;
public class DateCell extends Cell{
private int month, day, year;
public static final String[] MONTH = {"January", "February", "March", "April", "May",
"June", "July", "August", "September", "October", "November", "December"};
public DateCell(int month, int day, int year) {
this.month = month;
this.day = day;
this.year = year;
}
public DateCell(String s) {
int firstSlash = s.indexOf('/');
int secondSlash = s.lastIndexOf('/');
month = Integer.parseInt(s.substring(0, firstSlash));
day = Integer.parseInt(s.substring(firstSlash + 1, secondSlash));
if (s.substring(secondSlash + 1).length() < 4) {
year = Integer.parseInt(s.substring(secondSlash + 1)) + 2000;
} else {
year = Integer.parseInt(s.substring(secondSlash + 1));
}
}
public String toString() {
return MONTH[month-1] + " " + day + ", " + year;
}
}