1

这是一条令我困扰的消息,当我尝试grid通过按键组合shift-rightarrow或以编程方式“选择”单元格(无论是否为空)时,就会出现这种消息shift-leftarrow

Exception in thread "AWT-EventQueue-0" javax.swing.text.StateInvariantError: 
Bad caret position

(请注意,如果我通过shift-uparrow或“选择”没有问题shift-downarrow。)

当我尝试更改“选定”单元格的字体时会发生这种情况:

  static Font fontSelected = new Font("Serif", Font.BOLD , POINTSIZE);
  static Font fontNormal = new Font("Serif", Font.PLAIN, POINTSIZE);

(如果我将 Font.type 设为SAME(都是 BOLD,都是 PLAIN,都是 ITALIC),没问题。)

错误发生在我push“选择”JTextField到堆栈(名为stack)的代码附近,其定义如下:

class GenericStack<E>:
  public LinkedList <E> stack = new LinkedList<>();

这是使用堆栈和字体的类声明:

public class Grid  extends GenericStack<JTextField> implements ActionListener, KeyListener, KeyCodes, Serializable

这是推送的内容stack

 public static JTextField[][] cells = new JTextField[N][N];

以下cells是创建方式:

    guiFrame.add(textPanel);
    for (int i = 0; i < N; i++) 
      for (int j = 0; j < N; j++) 
        cells[i][j] = addCell(textPanel, i, j);

  private JTextField addCell (Container parent, int row, int col) {
    JTextField cell;
    cell = new JTextField();
    cell.setFont(fontNormal);                  // 'default' font set
    cell.setText("x");                         // for debugging
    String r, c;                               // 11x11 grid
    if(row < N-1) r = "" + row; else r = "A";  // rows  r: 0,1,2,...A
    if(col < N-1) c = "" + col; else c = "A";  // cols  c: 0,1,2,...A
    cell.setActionCommand(r + c);              // cell rc: 00..0A;10..1A;...A0..AA;
    cell.addKeyListener(this);
    cell.setHorizontalAlignment(JTextField.CENTER);
    parent.add(cell);
    return cell;
  }  

这里主要:

  public static void main(String[] args)
  {    
     javax.swing.SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run() {
        new Grid();
      }
    });
  }

这是更改字体的地方(对于任何“选定”单元格):

if(currentCell.selected){
  Grid.cells[currentCell.row][currentCell.col].setBackground(Color.RED);
  Grid.cells[currentCell.row][currentCell.col].setFont(fontSelected);
  stack.push(Grid.cells[currentCell.row][currentCell.col]);
}

错误出现在这块代码中——如果我注释掉该setFont行,没有问题;如果我改为更改字体声明以涉及相同的字体,没问题。

尤其令我困惑的是堆栈跟踪没有指定哪一行代码导致了错误。

4

2 回答 2

2

我不确定为什么会发生您的异常,但可以通过在 Swing 事件线程上排队字体更改来解决:

@Override
public void keyPressed(KeyEvent evt) {
  final JComponent comp = (JComponent) evt.getSource();
  int keyCode = evt.getKeyCode();
  boolean shiftIsDown = evt.isShiftDown();
  currentCell.selected = ((shiftIsDown & (keyCode == RIGHT | keyCode == UP
        | keyCode == LEFT | keyCode == DOWN)));
  if (currentCell.selected) {
     SwingUtilities.invokeLater(new Runnable() {
        public void run() {
           comp.setFont(fontSelected);
        }
     });
  }
}

我自己,我尽量避免在 Swing 应用程序中使用 KeyListener,而是更喜欢键绑定。例如:

import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.*;

@SuppressWarnings("serial")
public class SSCCE2 extends JPanel {
   private static final int ROW_COUNT = 11;
   private static final int colCount = 3;
   private static final Font NORMAL_FONT = new Font("Serif", Font.PLAIN, 18);
   private static final Font SELECTED_FONT = NORMAL_FONT.deriveFont(Font.BOLD);

   private JTextField[][] fields = new JTextField[ROW_COUNT][ROW_COUNT];

   public SSCCE2() {
      FontAction fontAction = new FontAction();
      int condition = WHEN_FOCUSED;

      setLayout(new GridLayout(ROW_COUNT, ROW_COUNT));
      for (int i = 0; i < fields.length; i++) {
         for (int j = 0; j < fields[i].length; j++) {
            JTextField cell = new JTextField(colCount);
            InputMap inputMap = cell.getInputMap(condition);
            ActionMap actionMap = cell.getActionMap();
            int[] arrowKeyCodes = {KeyEvent.VK_UP, KeyEvent.VK_DOWN, 
                  KeyEvent.VK_LEFT, KeyEvent.VK_RIGHT};
            for (int keyCode : arrowKeyCodes) {
               KeyStroke keyStroke = KeyStroke.getKeyStroke(keyCode, 
                     KeyEvent.SHIFT_DOWN_MASK);
               inputMap.put(keyStroke, keyStroke.toString());
               actionMap.put(keyStroke.toString(), fontAction);
            }
            cell.setFont(NORMAL_FONT);
            cell.setHorizontalAlignment(JTextField.CENTER);
            add(cell);
            fields[i][j] = cell;
         }
      }


   }

   private class FontAction extends AbstractAction {
      @Override
      public void actionPerformed(ActionEvent evt) {
         for (JTextField[] row : fields) {
            for (JTextField textField : row) {
               if (textField.hasFocus()) {
                  textField.setFont(SELECTED_FONT);
               } else {
                  textField.setFont(NORMAL_FONT);
               }
            }
         }
      }
   }

   private static void createAndShowGui() {
      SSCCE2 mainPanel = new SSCCE2();

      JFrame frame = new JFrame("SSCCE2");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}
于 2013-10-13T04:46:24.963 回答
0

我删除了所有不影响获取消息的内容,包括光标移动代码,所以当它运行时,会出现一个网格,要获取错误,请按住 shift 键并按右箭头键。(这是我应该做的吗??)(PS--我什至取出了所有通用链表(堆栈)的东西,事实证明,这与问题无关。)

package sscce;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

class Cell 
{
  int row, col;
  boolean selected;

  Cell(int row, int col){
    this.row = row;
    this.col = col;  
  } 
}

public class SSCCE implements ActionListener, KeyListener
{
  public static final int LEFT = 37, UP = 38, RIGHT = 39, DOWN = 40;
  public static final int N = 11;
  JFrame guiFrame;
  JPanel textPanel;
  public static JTextField[][] cells = new JTextField[N][N];
  public static Cell currentCell = new Cell(0,0);

  static Font fontSelected = new Font("Serif", Font.BOLD , 12);
  static Font fontNormal = new Font("Serif", Font.PLAIN, 12);

  public SSCCE(){ 
    textPanel = new JPanel();
    textPanel.setLayout(new GridLayout(N, N));
    guiFrame = new JFrame();
    guiFrame.setMinimumSize(new Dimension(400, 400));
    guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    guiFrame.setLocationRelativeTo(null);    
    guiFrame.add(textPanel);
    for (int i = 0; i < N; i++) 
      for (int j = 0; j < N; j++) 
        cells[i][j] = addCell(textPanel, i, j);
    guiFrame.setVisible(true);
  }  

  private JTextField addCell (Container parent, int row, int col) {
    JTextField cell;
    cell = new JTextField();
    cell.setFont(fontNormal);
    cell.addKeyListener((KeyListener) this);
    cell.setHorizontalAlignment(JTextField.CENTER);
    parent.add(cell);
    return cell;
  }  

  public static void main(String[] args)
  {    
     javax.swing.SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run() {
        new SSCCE();
      }
    });
  }

  @Override
  public void keyPressed(KeyEvent evt) { 
    int keyCode = evt.getKeyCode();
    boolean shiftIsDown = evt.isShiftDown();
    currentCell.selected = ((shiftIsDown & (keyCode == RIGHT | keyCode == UP | keyCode == LEFT | keyCode == DOWN)));
    if(currentCell.selected ){
      SSCCE.cells[currentCell.row][currentCell.col].setFont(fontSelected);
    }
   }
  @Override
  public void keyTyped(KeyEvent e){     }
  @Override
  public void keyReleased(KeyEvent e){  }
  @Override
  public void actionPerformed(ActionEvent e){}
}
于 2013-10-13T01:27:55.217 回答