import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class TableTest1 {
public static void main(String[] args) {
JFrame frame = new JFrame("Test Table");
String[] titles = { "姓名", "年龄", "性别", "数学成绩", "英语成绩", "总分", "是否及格" };
Object userInfo[][] = { { "李兴华", 30, "男", 100, 99, 199, true },
{ "张三", 29, "女", 99, 88, false } };
JTable table = new JTable(userInfo, titles);
JScrollPane scr = new JScrollPane(table);
frame.add(scr);
frame.setSize(370, 90);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
报错为
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 6
at javax.swing.JTable$1.getValueAt(Unknown Source)
at javax.swing.JTable.getValueAt(Unknown Source)
at javax.swing.JTable.prepareRenderer(Unknown Source)
at javax.swing.plaf.basic.BasicTableUI.paintCell(Unknown Source)
at javax.swing.plaf.basic.BasicTableUI.paintCells(Unknown Source)
at javax.swing.plaf.basic.BasicTableUI.paint(Unknown Source)
at javax.swing.plaf.ComponentUI.update(Unknown Source)
at javax.swing.JComponent.paintComponent(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JViewport.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JLayeredPane.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paintWithOffscreenBuffer(Unknown Source)
at javax.swing.JComponent.paintDoubleBuffered(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
at java.awt.Container.paint(Unknown Source)
at sun.awt.RepaintArea.paintComponent(Unknown Source)
at sun.awt.RepaintArea.paint(Unknown Source)
at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class TableTest1 {
public static void main(String[] args) {
JFrame frame = new JFrame("Test Table");
String[] titles = { "姓名", "年龄", "性别", "数学成绩", "英语成绩", "总分", "是否及格" };
Object userInfo[][] = { { "李兴华", 30, "男", 100, 99, 199, true },
{ "张三", 29, "女", 99, 88, false } };
JTable table = new JTable(userInfo, titles);
JScrollPane scr = new JScrollPane(table);
frame.add(scr);
frame.setSize(370, 90);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
报错为
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 6
at javax.swing.JTable$1.getValueAt(Unknown Source)
at javax.swing.JTable.getValueAt(Unknown Source)
at javax.swing.JTable.prepareRenderer(Unknown Source)
at javax.swing.plaf.basic.BasicTableUI.paintCell(Unknown Source)
at javax.swing.plaf.basic.BasicTableUI.paintCells(Unknown Source)
at javax.swing.plaf.basic.BasicTableUI.paint(Unknown Source)
at javax.swing.plaf.ComponentUI.update(Unknown Source)
at javax.swing.JComponent.paintComponent(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JViewport.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JLayeredPane.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paintWithOffscreenBuffer(Unknown Source)
at javax.swing.JComponent.paintDoubleBuffered(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
at java.awt.Container.paint(Unknown Source)
at sun.awt.RepaintArea.paintComponent(Unknown Source)
at sun.awt.RepaintArea.paint(Unknown Source)
at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
{ "张三", 29, "女", 99, 88, false } };
第二个变量少了一个参数。
Object userInfo[][] = { { "李兴华", 30, "男", 100, 99, 199, true },
{ "张三", 29, "女", 99, 88,187, false } };
看看这个:
import java.awt.event.WindowAdapter;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.MouseEvent;import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;class MouseHandle extends JFrame implements MouseListener {
private JTextArea text = new JTextArea(); public MouseHandle() {
super.setTitle("鼠标事件");
JScrollPane scr = new JScrollPane(text);
scr.setBounds(5, 5, 300, 200);
super.add(scr);
text.addMouseListener(this);
super.setSize(310, 210);
super.setVisible(true);
super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} public void mouseClicked(MouseEvent e) {
int c = e.getButton();
String mouseInfo = null;
if (c == MouseEvent.BUTTON1) {
mouseInfo = "左键";
} else if (c == MouseEvent.BUTTON3) {
mouseInfo = "右键";
} else {
mouseInfo = "滚轮";
}
text.append("鼠标单机:" + mouseInfo + "。\n");
} public void mouseEntered(MouseEvent e) {
text.append("鼠标进入组建");
} public void mouseExited(MouseEvent e) {
text.append("鼠标离开了组建");
} public void mousePressed(MouseEvent e) {
text.append("鼠标按下");
} public void mouseReleased(MouseEvent e) {
text.append("鼠标释放了");
}
}public class MouseEvent {
public static void main(String[] args)
{
new MouseHandle();
}
}
报错为
Exception in thread "main" java.lang.Error: Unresolved compilation problem: at GuiSwing.MouseEvent.main(MouseEvent.java:57)
如:public class MouseEvent1 {//not MouseEvent
public static void main(String[] args)
{
new MouseHandle();
}
}