gpt4 book ai didi

java - 在java中创建表

转载 作者:行者123 更新时间:2023-12-01 09:54:06 24 4
gpt4 key购买 nike

我正在尽力在 java 应用程序中的列表旁边创建只读表(空白,但准备获取列内容和行名称),但我仍然无法弄清楚这些表有什么问题

<小时/>

我得到了异常(exception)

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0 at examples.TableData.getColumnCount(TableData.java:25) at javax.swing.JTable.createDefaultColumnsFromModel(JTable.java:1285) at javax.swing.JTable.tableChanged(JTable.java:4386) at javax.swing.JTable.setModel(JTable.java:3688) at javax.swing.JTable.(JTable.java:632) at javax.swing.JTable.(JTable.java:573) at examples.Main$2.run(Main.java:77) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756) at java.awt.EventQueue.access$500(EventQueue.java:97) at java.awt.EventQueue$3.run(EventQueue.java:709) at java.awt.EventQueue$3.run(EventQueue.java:703) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75) at java.awt.EventQueue.dispatchEvent(EventQueue.java:726) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) at java.awt.EventDispatchThread.run(EventDispatchThread.java:82) BUILD SUCCESSFUL (total time: 5 seconds)


这是主文件:

/* Програма с графичен интерфейс (swing/awt), която
* извежда продуктовите категории от базата данни на "northwind"
* и изкарва информация за продуктите в нея
*/

package examples;

/* Компоненти на swing */
import javax.swing.SwingUtilities;
import javax.swing.JList;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.JTable;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
/* Компоненти на awt */
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Container;
import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
/* Компоненти на SQL ?? */

public class Main
{
public static void main(String[] args)
{
/* ЗАГЛАВИЕ на прозорец, добавяне на КОНТЕЙНЪР в прозореца */
JFrame frame = new JFrame("Product Exmplorer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = frame.getContentPane();

/* Използване на JList ЛИСТ */
String labels[] = { "Test1", "Test2" };
JList jlist = new JList(labels);

/* Слагане на ПЛЪЗГАЧИ в контейнъра */
JScrollPane scrollPane1 = new JScrollPane(jlist);
contentPane.add(scrollPane1, BorderLayout.EAST);

/**********************************************************************/
/* Дефиниране на ЛИСЪНЪР за клик */
MouseListener mouseListener = new MouseAdapter()
{
public void mouseClicked(MouseEvent mouseEvent)
{
JList theList = (JList) mouseEvent.getSource();
if (mouseEvent.getClickCount() == 2)
{
int index = theList.locationToIndex(mouseEvent.getPoint());
if (index >= 0)
{
Object o = theList.getModel().getElementAt(index);
System.out.println("Double-clicked on: " + o.toString());
}
}
}
};
jlist.addMouseListener(mouseListener);

/**********************************************************************/

Runnable runner = new Runnable() // анонимен клас
{
@Override
public void run()
{
JFrame frame = new JFrame("Button model");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// създаване на таблицата
TableData td = new TableData();
JTable table = new JTable(td);
table.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);

JScrollPane scrollPane = new JScrollPane(table);
frame.add(scrollPane , BorderLayout.CENTER);

frame.pack();
frame.setVisible(true);
}
};

SwingUtilities.invokeLater(runner);
}
}
<小时/>

这是所需的源包:

package examples;

import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.table.AbstractTableModel;

public class TableData extends AbstractTableModel
{
//Row & Column Data
private Object[][] values =
{
};
private final String[] columnHeaders = {
"Име", "Фамилия", "Роден(а) на", "Сума", "Пол"
};

@Override
public int getRowCount()
{
return values.length;
}
@Override
public int getColumnCount()
{
return values[0].length;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex)
{
return values[rowIndex][columnIndex];
}
//--------------------------------------------------------------------------
}
<小时/>

我即将做最可怕的噩梦

最佳答案

TableData extends AbstractTableModel
{
//Row & Column Data
private Object[][] values =

在哪里初始化“值”?它看起来是空的。

public int getColumnCount()
{
return values[0].length;
}

您正在尝试访问值[0],而不检查是否存在“值[0]”。

关于java - 在java中创建表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37373306/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com