我是 Java 新手,我正在尝试制作一个带有表格视图的窗口。在这个窗口中,我想删除、更新和插入数据等。
最后它看起来像这样:
我从来没有在任何人提示或链接到好的教程之前这样做过。我不是要您为我编写代码,而是要学习如何制作一个动态表,我可以在执行操作后重新加载该表。我找不到任何好的教程。我的 Java 书也没有给出很好的例子。
在 PHP 中我可以在 5 分钟内完成,但在 JAVA 中我不知道!无论如何,我会尝试用我所拥有的知识弄清楚如何做到这一点。
我现在有这个 Java 代码:
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.ResultSet;
import java.sql.SQLException;
public class test extends JPanel {
private boolean DEBUG = false;
public test() {
super(new GridLayout(1,0));
Database db = new Database();
String tabel = "testtabel";
ResultSet res = db.query("SELECT COUNT( * ) FROM `"+tabel+"`");
ResultSet res2 = db.query("SELECT COUNT( * ) FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '"+tabel+"'");
int rows = 0;
int collums = 0;
try {
res.next();
res2.next();
rows = res.getInt(1);
collums = res2.getInt(1);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
ResultSet clom = db.query("DESCRIBE `"+tabel+"`");
String[] columnNames = new String[collums];
int s = 0;
try {
while(clom.next()){
columnNames[s] = clom.getString(1);
s++;
}
} catch (SQLException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
Object[][] data = new Object[rows][collums];
ResultSet result = db.query("SELECT * FROM `"+tabel+"`");
int q = 0;
try {
while(result.next()){
for(int a=0; a<= (collums - 1); a++){
data[q][a] = result.getString(a + 1);
System.out.println(q + " - " + a);
}
q++;
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
final JTable table = new JTable(data, columnNames);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
if (DEBUG) {
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
printDebugData(table);
}
});
}
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
//Add the scroll pane to this panel.
add(scrollPane);
}
private void printDebugData(JTable table) {
int numRows = table.getRowCount();
int numCols = table.getColumnCount();
javax.swing.table.TableModel model = table.getModel();
System.out.println("Value of data: ");
for (int i=0; i < numRows; i++) {
System.out.print(" row " + i + ":");
for (int j=0; j < numCols; j++) {
System.out.print(" " + model.getValueAt(i, j));
}
System.out.println();
}
System.out.println("--------------------------");
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("SimpleTableDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
test newContentPane = new test();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
我怎样才能让这更简单?