坏插入位置消息

时间:2013-10-13 00:03:05

标签: java swing fonts jtextfield getcaretpos

这是我受到困扰的消息,当我尝试通过按键组合gridshift-rightarrow以编程方式“选择”shift-leftarrow中的单元格(无论是否为空)时发生:

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

(请注意,如果我通过shift-uparrowshift-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),没问题。) < / p>

错误发生在我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行,没问题;如果我改为将字体声明改为涉及相同的字体,没问题。

特别令我感到困惑的是,堆栈跟踪没有指定导致错误的代码行。

2 个答案:

答案 0 :(得分: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应用程序的KeyListeners,而是更喜欢键绑定。例如:

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();
         }
      });
   }
}

答案 1 :(得分:0)

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

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){}
}