Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 3
at sun.font.FontDesignMetrics.charsWidth(FontDesignMetrics.java:492)
at javax.swing.text.Utilities.getTabbedTextOffset(Utilities.java:381)
at javax.swing.text.Utilities.getTabbedTextOffset(Utilities.java:302)
at javax.swing.text.Utilities.getTabbedTextOffset(Utilities.java:286)
at javax.swing.text.PlainView.viewToModel(PlainView.java:403)
at javax.swing.text.FieldView.viewToModel(FieldView.java:263)
at javax.swing.plaf.basic.BasicTextUI$RootView.viewToModel(BasicTextUI.java:1540)
at javax.swing.plaf.basic.BasicTextUI.viewToModel(BasicTextUI.java:1089)
at javax.swing.text.DefaultCaret.moveCaret(DefaultCaret.java:311)
at javax.swing.text.DefaultCaret.mouseDragged(DefaultCaret.java:565)
at java.awt.AWTEventMulticaster.mouseDragged(AWTEventMulticaster.java:303)
at java.awt.Component.processMouseMotionEvent(Component.java:6311)
at javax.swing.JComponent.processMouseMotionEvent(JComponent.java:3285)
at java.awt.Component.processEvent(Component.java:6032)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4255)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2475)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

解决方案 »

  1.   

    由于不知道是哪的原因,代码都贴出来:
    public class Board extends JFrame {
    Flat ft = null;
    ToolBar tb = null;
    public Board() {
    this.init();
    } private void init() {
    ft = new Flat();
    tb = new ToolBar();
    JMenuBar menu = new JMenuBar();
    addMenuBar(menu);
    BorderLayout JFrameBorderLayout = new BorderLayout();
    this.add(ft);
    this.add(tb,BorderLayout.NORTH);
    this.setBounds(300, 200, 500, 500);
    this.setTitle("画函数曲线工具(MakeFunctionCurve)");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);
    this.setJMenuBar(menu);
    } public void addMenuBar(JMenuBar menuBar) {
    JMenu game = new JMenu("工具");
    JMenuItem clear = game.add("清空");
    JMenuItem exit = game.add("退出");
    JMenu help = new JMenu("帮助");
    JMenuItem about = help.add("关于");
    menuBar.add(game);
    menuBar.add(help);
    }
    }
      

  2.   


    /**
     * 上面的工具条区
     * @author
     *
     */
    public class ToolBar extends JPanel {
    /**
     * 二次方系数
     */
    private JTextField jtf_quadraticCoefficient = null;
    /**
     *  X²
     */
    private JLabel jLb_quadraticCoefficient = null;

    /**
     * 一次方系数
     */
    private JTextField jtf_firstPowCoefficient = null;
    /**
     * X
     */
    private JLabel jLb_firstPowCoefficient = null;
    /**
     * 常数
     */
    private JTextField jtf_constant = null; public ToolBar() {
    init();
    addListener();
    } private void init() {
    jtf_quadraticCoefficient = new JTextField("1");
    jLb_quadraticCoefficient = new JLabel();
    jtf_firstPowCoefficient = new JTextField("0");
    jLb_firstPowCoefficient = new JLabel();
    jtf_constant = new JTextField("0");
    jtf_quadraticCoefficient.setColumns(3);
    jtf_firstPowCoefficient.setColumns(3);
    jtf_constant.setColumns(3);
    jLb_quadraticCoefficient.setText("X²");
    jLb_firstPowCoefficient.setText("X");
    FlowLayout ToolBarflowLayout = new FlowLayout();
    this.setLayout(ToolBarflowLayout);
    ToolBarflowLayout.setHgap(10);
    ToolBarflowLayout.setVgap(20);
    this.add(jtf_quadraticCoefficient);
    this.add(jLb_quadraticCoefficient);
    this.add(jtf_firstPowCoefficient);
    this.add(jLb_firstPowCoefficient);
    this.add(jtf_constant);
    this.setBackground(Color.pink);
    } /**
     * 监听方法二次方系数、一次放系数、常数任何一个改变了,然后就重画
     */
    private void addListener() {
    jtf_constant.getDocument().addDocumentListener(new DocumentListener() { @Override
    public void insertUpdate(DocumentEvent e) {
    Controler.b.ft.repaint();
    } @Override
    public void removeUpdate(DocumentEvent e) {
    Controler.b.ft.repaint();
    } @Override
    public void changedUpdate(DocumentEvent e) {
    Controler.b.ft.repaint();
    } });
    jtf_firstPowCoefficient.getDocument().addDocumentListener(
    new DocumentListener() { @Override
    public void insertUpdate(DocumentEvent e) {
    Controler.b.ft.repaint();
    } @Override
    public void removeUpdate(DocumentEvent e) {
    Controler.b.ft.repaint();
    } @Override
    public void changedUpdate(DocumentEvent e) {
    Controler.b.ft.repaint();
    } });
    jtf_quadraticCoefficient.getDocument().addDocumentListener(
    new DocumentListener() { @Override
    public void insertUpdate(DocumentEvent e) {
    Controler.b.ft.repaint();
    } @Override
    public void removeUpdate(DocumentEvent e) {
    Controler.b.ft.repaint();
    } @Override
    public void changedUpdate(DocumentEvent e) {
    Controler.b.ft.repaint();
    }
    });
    } /**
     * 得到JTextField的值
     * @param textField
     * @return
     */
    private Float getValueFromTextField(JTextField textField) {
    Float result = (float) 0.0;
    String result_str = textField.getText();
    try {
    result = Float.valueOf(result_str);
    } catch (Exception e) {
    return (float) 0.0;
    }
    return result;
    } /**
     * 得到二次方系数的值
     * @return
     */
    public Float getQuadraticCoefficient() {
    return getValueFromTextField(jtf_quadraticCoefficient);
    } /**
     * 得到一次放系数的值
     * @return
     */
    public Float getFirstPowCoefficient() {
    return getValueFromTextField(jtf_firstPowCoefficient);
    } /**
     * 得到常数的值
     * @return
     */
    public Float getConstant() {
    return getValueFromTextField(jtf_constant);
    }}
      

  3.   

    建议设置断点调试以下的代码:
      private Float getValueFromTextField(JTextField textField) {
            Float result = (float) 0.0;
            String result_str = textField.getText();
            try {
                result = Float.valueOf(result_str);
            } catch (Exception e) {
                return (float) 0.0;
            }
            return result;
        }
      

  4.   


    /**
     * 画图区
     * @author 
     *
     */
    public class Flat extends JPanel implements KeyListener {
    /**
     * 单位长度的像素数
     */
    public final static int UNITLENGTH = 20; @Override
    protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    int width = this.getWidth();
    int height = this.getHeight();
    // 原点
    int xOrigin = width / 2;
    int yOrigin = height / 2;
    // 划x轴
    g.setColor(Color.BLACK);
    g.drawLine(0, yOrigin, width, yOrigin);
    // 划y轴
    g.drawLine(xOrigin, 0, xOrigin, height);
    g.fillOval(xOrigin - 5, yOrigin - 5, 10, 10);
    // 画网格中x正半轴的竖线
    for (int xAxisPositive = xOrigin + this.UNITLENGTH; xAxisPositive < width; xAxisPositive += this.UNITLENGTH) {
    g.setColor(Color.LIGHT_GRAY);
    g.drawLine(xAxisPositive, 0, xAxisPositive, height);
    int scale = (xAxisPositive - xOrigin) / this.UNITLENGTH;
    if (scale % 5 == 0) {
    g.setColor(Color.BLUE);
    g.drawString(String.valueOf(scale), xAxisPositive + 2, yOrigin);
    g.drawLine(xAxisPositive, yOrigin, xAxisPositive, yOrigin - 5);
    }
    }
    // 画网格中x负半轴的竖线
    for (int xAxisNegative = xOrigin - this.UNITLENGTH; xAxisNegative > 0; xAxisNegative -= this.UNITLENGTH) {
    g.setColor(Color.LIGHT_GRAY);
    g.drawLine(xAxisNegative, 0, xAxisNegative, height);
    int scale = (xAxisNegative - xOrigin) / this.UNITLENGTH;
    if (scale % 5 == 0) {
    g.setColor(Color.BLUE);
    g.drawString(String.valueOf(scale), xAxisNegative + 2, yOrigin);
    g.drawLine(xAxisNegative, yOrigin, xAxisNegative, yOrigin - 5);
    }
    }
    // 画网格中y正半轴的横线
    for (int yAxisPositive = yOrigin - this.UNITLENGTH; yAxisPositive > 0; yAxisPositive -= this.UNITLENGTH) {
    g.setColor(Color.LIGHT_GRAY);
    g.drawLine(0, yAxisPositive, width, yAxisPositive);
    int scale = (yOrigin - yAxisPositive) / this.UNITLENGTH;
    if (scale % 5 == 0) {
    g.setColor(Color.BLUE);
    g.drawString(String.valueOf(scale), xOrigin, yAxisPositive + 2);
    g.drawLine(xOrigin, yAxisPositive, xOrigin - 5, yAxisPositive);
    }
    }
    // 画网格中y负半轴的横线
    for (int yAxisNegative = yOrigin + this.UNITLENGTH; yAxisNegative < height; yAxisNegative += this.UNITLENGTH) {
    g.setColor(Color.LIGHT_GRAY);
    g.drawLine(0, yAxisNegative, width, yAxisNegative);
    int scale = (yOrigin - yAxisNegative) / this.UNITLENGTH;
    if (scale % 5 == 0) {
    g.setColor(Color.BLUE);
    g.drawString(String.valueOf(scale), xOrigin, yAxisNegative + 2);
    g.drawLine(xOrigin, yAxisNegative, xOrigin - 5, yAxisNegative);
    }
    }
    // 画x正半轴曲线
    g.setColor(Color.red);
    /**
     * 设x=0时为起始点
     */
    Coordinate coordinate = this.getCoordinate(0);
    for (int x = xOrigin; x < width; x += 2) {
    float xCoor = (float) (x - xOrigin) / this.UNITLENGTH;
    /**
     * 在panel中的实际坐标的始点
     */
    int starX = this.getXDrawCoordinate(coordinate.getX(), xOrigin);
    int starY = this.getYDrawCoordinate(coordinate.getY(), yOrigin);
    /**
     * 在panel中的实际坐标的终点
     */
    int terminalX = this.getXDrawCoordinate(this.getCoordinate(xCoor)
    .getX(), xOrigin);
    int terminalY = this.getYDrawCoordinate(this.getCoordinate(xCoor)
    .getY(), yOrigin);
    /**
     * 如果没有超出范围
     */
    if ((this.getXDrawCoordinate(coordinate.getY(), yOrigin) < height)
    && (this.getXDrawCoordinate(coordinate.getY(), yOrigin) > 0)) {
    g.drawLine(starX, starY, terminalX, terminalY);
    }
    /**
     * 把终点设成始点
     */
    coordinate = this.getCoordinate(xCoor);
    }
    // 画x负半轴曲线
    coordinate = this.getCoordinate(0);
    for (int x = xOrigin; x > 0; x -= 2) {
    float xCoor = (float) (x - xOrigin) / this.UNITLENGTH;
    int starX = this.getXDrawCoordinate(coordinate.getX(), xOrigin);
    int starY = this.getYDrawCoordinate(coordinate.getY(), yOrigin);
    int terminalX = this.getXDrawCoordinate(this.getCoordinate(xCoor)
    .getX(), xOrigin);
    int terminalY = this.getYDrawCoordinate(this.getCoordinate(xCoor)
    .getY(), yOrigin);
    if ((this.getXDrawCoordinate(coordinate.getY(), yOrigin) < height)
    && (this.getXDrawCoordinate(coordinate.getY(), yOrigin) > 0)) {
    g.drawLine(starX, starY, terminalX, terminalY);
    }
    coordinate = this.getCoordinate(xCoor);
    }
    } /**
     * 有x的值算出y的值
     * @param x
     * @return
     */
    private Coordinate getCoordinate(float x) {
    return Algorithm.getSecondPow(x);
    } /**
     * 将由二次函数算出的坐标值转换成要画的实际的坐标值
     * 假设  二次函数 X²+X+1 x=1时 y=3 而要画到panel中的坐标是:</br> realX = 1*(单位长度的像素数)+y轴所在的x的实际坐标(panelk),
     *
     * 
     * @param codnte
     * @param radix
     * @return
     */
    private int getXDrawCoordinate(float codnte, int radix) {
    return (int) (codnte * this.UNITLENGTH + radix);
    } /**
     * 转换Y值
     * @param codnte
     * @param radix
     * @return
     */
    private int getYDrawCoordinate(float codnte, int radix) {
    return (int) (radix - codnte * this.UNITLENGTH);
    } @Override
    public void keyTyped(KeyEvent e) { } @Override
    public void keyPressed(KeyEvent e) { } @Override
    public void keyReleased(KeyEvent e) { }
    }
      

  5.   


    public class Coordinate {
    private float x, y; public Coordinate(float x, float y) {
    this.x = x;
    this.y = y;
    } public Coordinate() {
    this(0, 0);
    } public Coordinate(float x) {
    this(x, 0);
    } public float getX() {
    return x;
    } public void setX(float x) {
    this.x = x;
    } public float getY() {
    return y;
    } public void setY(float y) {
    this.y = y;
    }}
      

  6.   


    public class Algorithm {
    public static Coordinate getSecondPow(float x){
    ToolBar toolBar = Controler.b.tb;
    Float quadraticCoefficient = toolBar.getQuadraticCoefficient();
    Float firstPowCoefficient = toolBar.getFirstPowCoefficient();
    Float constant=toolBar.getConstant();
    float y = quadraticCoefficient*x*x+firstPowCoefficient*x+constant;
    return new Coordinate(x, y);
    }
    }