不会起名字的我 发表于 2021-3-29 21:29:36

JTable 组件

每当添加表格是就报数组下标越界,请问是什么问题???
import javax.swing.*;
import java.awt.*;

public class Demo extends JFrame{
        public Demo() {
                setVisible(true);
                setBounds(500, 200, 200, 300);
                setDefaultCloseOperation(EXIT_ON_CLOSE);
                Container c = getContentPane();
               
                String[] clumnname = {"A","B","C"};
                String[][] into = {{"A1","A2"},{"B1","B2"},{"C1","C2"}};
               
                JTable table = new JTable(into, clumnname);
                JScrollPane jp = new JScrollPane(table);
                c.add(jp);
        }
        public static void main(String[] args) {
                new Demo();
        }
}
错误信息
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 2
        at java.desktop/javax.swing.JTable$1.getValueAt(Unknown Source)
        at java.desktop/javax.swing.JTable.getValueAt(Unknown Source)
        at java.desktop/javax.swing.JTable.prepareRenderer(Unknown Source)
        at java.desktop/javax.swing.plaf.basic.BasicTableUI.paintCell(Unknown Source)
        at java.desktop/javax.swing.plaf.basic.BasicTableUI.paintCells(Unknown Source)
        at java.desktop/javax.swing.plaf.basic.BasicTableUI.paint(Unknown Source)
        at java.desktop/javax.swing.plaf.ComponentUI.update(Unknown Source)
        at java.desktop/javax.swing.JComponent.paintComponent(Unknown Source)
        at java.desktop/javax.swing.JComponent.paint(Unknown Source)
        at java.desktop/javax.swing.JComponent.paintChildren(Unknown Source)
        at java.desktop/javax.swing.JComponent.paint(Unknown Source)
        at java.desktop/javax.swing.JViewport.paint(Unknown Source)
        at java.desktop/javax.swing.JComponent.paintChildren(Unknown Source)
        at java.desktop/javax.swing.JComponent.paint(Unknown Source)
        at java.desktop/javax.swing.JComponent.paintChildren(Unknown Source)
        at java.desktop/javax.swing.JComponent.paint(Unknown Source)
        at java.desktop/javax.swing.JComponent.paintChildren(Unknown Source)
        at java.desktop/javax.swing.JComponent.paint(Unknown Source)
        at java.desktop/javax.swing.JLayeredPane.paint(Unknown Source)
        at java.desktop/javax.swing.JComponent.paintChildren(Unknown Source)
        at java.desktop/javax.swing.JComponent.paintToOffscreen(Unknown Source)
        at java.desktop/javax.swing.RepaintManager$PaintManager.paintDoubleBufferedImpl(Unknown Source)
        at java.desktop/javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
        at java.desktop/javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
        at java.desktop/javax.swing.RepaintManager.paint(Unknown Source)
        at java.desktop/javax.swing.JComponent.paint(Unknown Source)
        at java.desktop/java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
        at java.desktop/sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
        at java.desktop/sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
        at java.desktop/java.awt.Container.paint(Unknown Source)
        at java.desktop/java.awt.Window.paint(Unknown Source)
        at java.desktop/javax.swing.RepaintManager$4.run(Unknown Source)
        at java.desktop/javax.swing.RepaintManager$4.run(Unknown Source)
        at java.base/java.security.AccessController.doPrivileged(Native Method)
        at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
        at java.desktop/javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
        at java.desktop/javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
        at java.desktop/javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
        at java.desktop/javax.swing.RepaintManager.access$1200(Unknown Source)
        at java.desktop/javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
        at java.desktop/java.awt.event.InvocationEvent.dispatch(Unknown Source)
        at java.desktop/java.awt.EventQueue.dispatchEventImpl(Unknown Source)
        at java.desktop/java.awt.EventQueue.access$600(Unknown Source)
        at java.desktop/java.awt.EventQueue$4.run(Unknown Source)
        at java.desktop/java.awt.EventQueue$4.run(Unknown Source)
        at java.base/java.security.AccessController.doPrivileged(Native Method)
        at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
        at java.desktop/java.awt.EventQueue.dispatchEvent(Unknown Source)
        at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
        at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
        at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
        at java.desktop/java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.desktop/java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.desktop/java.awt.EventDispatchThread.run(Unknown Source)

夏雨莲 发表于 2021-4-5 21:20:05

本帖最后由 夏雨莲 于 2021-4-5 21:23 编辑

你的表头的列数和表中的数据的列数不同,你的表头是三列,表中的数据是2列,你把它们改成相同的列数就行了。
页: [1]
查看完整版本: JTable 组件