① 怎樣使用java swing畫一個表格,把輸出顯示到表格上,而且每10秒自動刷新一次
使用swing的JTable作為表格顯示數據
JTable table = new JTable(new DefaultTableModel(rowData, columnNames))
在參數里填入列數據和列名稱
把table放到一個JFrame里顯示
使用線程或者Timer定時更新數據
使用((DefaultTableModel) table.getModel()).fireTableDataChanged();讓頁面刷新
import java.awt.BorderLayout;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingWorker;
import javax.swing.table.DefaultTableModel;
public class test extends JFrame {
private static final long serialVersionUID = -4892684184268025880L;
private static final Timer time = new Timer("test");
private Vector<Vector<Object>> rowData = new Vector<Vector<Object>>();
private JTable table;
private int count = 0;
public test() {
super("表格");
this.setSize(400, 400);
this.setLayout(new BorderLayout());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Vector<String> columnNames = new Vector<String>();
columnNames.add("統計項");
columnNames.add("數目");
table = new JTable(new DefaultTableModel(rowData, columnNames));
this.add(new JScrollPane(table), BorderLayout.CENTER);
time.schele(new TimerTask() {
@Override
public void run() {
update();
}
}, 0, 1000);
}
private void update() {
new SwingWorker<Object, Object>() {
protected Object doInBackground() throws Exception {
rowData.clear();
for (int i = 0; i < 4; i++) {
Vector<Object> info = new Vector<Object>();
info.add("測試");
info.add(count);
rowData.add(info);
}
count++;
return null;
}
protected void done() {
((DefaultTableModel) table.getModel()).fireTableDataChanged();
}
}.execute();
}
public static void main(String args[]) {
new test().setVisible(true);
}
}