package test;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.Window;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class QueryResult extends JFrame {
private JLabel resultLbl = null;
private JTable resultTbl = null;
private DefaultTableModel resultTblModel = null;
public QueryResult() {
super("查询结果");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 300);
setResizable(false);
initGUI();
centerWindow(this);
}
private void initGUI() {
JPanel contentPnl = new JPanel();
contentPnl.setLayout(new BorderLayout(5, 5));
resultLbl = new JLabel("查询结果如下:");
resultTblModel = new DefaultTableModel()
{
//设置表格不可编辑
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
resultTbl = new JTable(resultTblModel);
JScrollPane tablePane = new JScrollPane(resultTbl);
contentPnl.add(resultLbl, BorderLayout.PAGE_START);
contentPnl.add(tablePane, BorderLayout.CENTER);
this.setContentPane(contentPnl);
pack();
}
/**
* 由GaojiChaxun调用来根据查询结果刷新表格
* @param value 表格中显示的内容,二维数组表示行列
* @param header 表格的表头
*/
public void refreshTable(Object[][] value, Object[] header)
{
resultTblModel.setDataVector(value, header);
}
private void centerWindow(Window w) {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension windowSize = w.getSize();
int x = (screenSize.width - windowSize.width) / 2;
int y = (screenSize.height - windowSize.height) / 2;
w.setLocation(new Point(x, y));
}
public static void main(String[] args) {
QueryResult queryResult = new QueryResult();
String[] header = {"姓名", "性别", "年龄"};
String[][] value = {{"zhangsan", "男", "12"},{"lisi", "男", "23"},{"wangwu", "男", "44"}};
queryResult.refreshTable(value, header);
queryResult.setVisible(true);
}
}