java渣渣求救~~怎么将编写好的JTable用在JFrame面板上生成表格?
1个回答
展开全部
package bvg.winnir.demo;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;
import utils.DBHelper;
public class Reader {
private JPanel pMain;
private JPanel pUp;
private JFrame f ;
private JButton button;
private JButton button2;
private JButton button3;
private JButton button4;
private JButton button5;
private JScrollPane jsp;
private JTable table;
private JDialog dialog;
public Reader(){
pMain = new JPanel(new BorderLayout());//主面板
pUp = new JPanel(); //按钮上面板
jsp = new JScrollPane(table); //滚动面板
f = new JFrame(); //界面
button = new JButton("来按我"); //按钮
button2 = new JButton("来按我2"); //按钮
button3 = new JButton("来按我3"); //按钮
button4 = new JButton("来按我4"); //按钮
button5 = new JButton("来按我5"); //按钮
table = new JTable(5,10); //表格
dialog = new JDialog();
dialog.add(pMain);
dialog.setBounds(300,200,180,160);
pUp.setBackground(new Color(245,123,111));
table.setModel(getModel());
//table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);//关闭自动调整列宽
TableColumn column = table.getColumnModel().getColumn(1);//得到某个列
//column.setPreferredWidth(200);//设置宽度
jsp.setViewportView(table);
pUp.add(button);
pUp.add(button2);
pUp.add(button3);
pUp.add(button4);
pUp.add(button5);
pMain.add(pUp,new BorderLayout().NORTH);
pMain.add(jsp,new BorderLayout().CENTER);
f.add(pMain);
f.setBounds(10, 30, 700, 700);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
public DefaultTableModel getModel(){
//创建保存所有列名的一维数组
Vector<String> title = new Vector<String>();
//创建保存所有数据的二维数组
Vector<Vector<String>> data = new Vector<Vector<String>>();
//填入列名
title.addElement("名称");
title.addElement("类型");
title.addElement("状态");
//根据列名,填入对应的每一行数据
String sql ="SELECT * FROM admins";//+userNo;
//select t.base_name,td.DEVICE_STATE from t_base t,t_device_work td where t.BASE_ID=td.DEVICE_ID and td.DEVICE_TYPE='B';
//select t.base_name,td.DEVICE_STATE from t_base485 t,t_device_work td where t.BASE_ID=td.DEVICE_ID and td.DEVICE_TYPE='G';
//select t.EMETER_NAME,ti.ACQUISITION_TIME from t_emeter t left join t_immediate_data ti on t.EMETER_ID=ti.EMETER_ID
ResultSet rs = DBHelper.doQuery(sql);
int i = 0;
try {
while(rs.next()){
//i++;
Vector<String> line = new Vector<String>();//用来保存一行数据的一位数组
line.addElement(""+rs.getString("username"));
line.addElement(""+rs.getString("passwords"));
if(rs.getString("").equals("XXXX")){
line.addElement("正常");
}
else {
line.addElement("不正常");
}
if(rs.getTimestamp("").after(new Date(rs.getString("")))){
}
line.addElement(""+rs.getString("name"));
data.addElement(line);//将当前行数据保存到二维数组
System.out.println("-------"+i);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DefaultTableModel model = new DefaultTableModel(data,title);
return model;
}
//根据列名和数据,生成表模型
public static void main(String[] args) {
new Reader();
}
}
不知道你会不会用数据库。
String sql 后面是一句数据库查询命令,不会的再问我
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;
import utils.DBHelper;
public class Reader {
private JPanel pMain;
private JPanel pUp;
private JFrame f ;
private JButton button;
private JButton button2;
private JButton button3;
private JButton button4;
private JButton button5;
private JScrollPane jsp;
private JTable table;
private JDialog dialog;
public Reader(){
pMain = new JPanel(new BorderLayout());//主面板
pUp = new JPanel(); //按钮上面板
jsp = new JScrollPane(table); //滚动面板
f = new JFrame(); //界面
button = new JButton("来按我"); //按钮
button2 = new JButton("来按我2"); //按钮
button3 = new JButton("来按我3"); //按钮
button4 = new JButton("来按我4"); //按钮
button5 = new JButton("来按我5"); //按钮
table = new JTable(5,10); //表格
dialog = new JDialog();
dialog.add(pMain);
dialog.setBounds(300,200,180,160);
pUp.setBackground(new Color(245,123,111));
table.setModel(getModel());
//table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);//关闭自动调整列宽
TableColumn column = table.getColumnModel().getColumn(1);//得到某个列
//column.setPreferredWidth(200);//设置宽度
jsp.setViewportView(table);
pUp.add(button);
pUp.add(button2);
pUp.add(button3);
pUp.add(button4);
pUp.add(button5);
pMain.add(pUp,new BorderLayout().NORTH);
pMain.add(jsp,new BorderLayout().CENTER);
f.add(pMain);
f.setBounds(10, 30, 700, 700);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
public DefaultTableModel getModel(){
//创建保存所有列名的一维数组
Vector<String> title = new Vector<String>();
//创建保存所有数据的二维数组
Vector<Vector<String>> data = new Vector<Vector<String>>();
//填入列名
title.addElement("名称");
title.addElement("类型");
title.addElement("状态");
//根据列名,填入对应的每一行数据
String sql ="SELECT * FROM admins";//+userNo;
//select t.base_name,td.DEVICE_STATE from t_base t,t_device_work td where t.BASE_ID=td.DEVICE_ID and td.DEVICE_TYPE='B';
//select t.base_name,td.DEVICE_STATE from t_base485 t,t_device_work td where t.BASE_ID=td.DEVICE_ID and td.DEVICE_TYPE='G';
//select t.EMETER_NAME,ti.ACQUISITION_TIME from t_emeter t left join t_immediate_data ti on t.EMETER_ID=ti.EMETER_ID
ResultSet rs = DBHelper.doQuery(sql);
int i = 0;
try {
while(rs.next()){
//i++;
Vector<String> line = new Vector<String>();//用来保存一行数据的一位数组
line.addElement(""+rs.getString("username"));
line.addElement(""+rs.getString("passwords"));
if(rs.getString("").equals("XXXX")){
line.addElement("正常");
}
else {
line.addElement("不正常");
}
if(rs.getTimestamp("").after(new Date(rs.getString("")))){
}
line.addElement(""+rs.getString("name"));
data.addElement(line);//将当前行数据保存到二维数组
System.out.println("-------"+i);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DefaultTableModel model = new DefaultTableModel(data,title);
return model;
}
//根据列名和数据,生成表模型
public static void main(String[] args) {
new Reader();
}
}
不知道你会不会用数据库。
String sql 后面是一句数据库查询命令,不会的再问我
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询