JAVA Swing 如何将JTable的内容显示在当前的界面上面,而不是新创建一个窗口来显示?
这是我的代码,大概意思就是我在做一个学生信息管理系统,我在MainGUI里面做了一个下拉菜单,里面有一个“信息游览”选项,我想实现的功能是当我选择“信息游览”这个下拉菜单...
这是我的代码,大概意思就是我在做一个学生信息管理系统,我在MainGUI里面做了一个下拉菜单,里面有一个“信息游览”选项,我想实现的功能是当我选择“信息游览”这个下拉菜单时,把数据库里面的学生信息调出来,显示在当前的MainGUI上面,而不是新调用一个窗口出来显示学生信息,下面是我写的部分代码:
package StudentsInformationManageSystem;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.TextArea;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.Statement;
import javax.swing.*;
public class MainGUI extends JFrame{
public MainGUI(){
super("学生信息管理系统");
JMenu backMenu = new JMenu("返回主界面(B)");
backMenu.setMnemonic(KeyEvent.VK_B);
JMenu informationManage= new JMenu("学生信息管理(M)");
informationManage.setMnemonic(KeyEvent.VK_M);
JMenuItem veiwMenuItem = new JMenuItem("信息游览(V) ");
veiwMenuItem.setAccelerator(KeyStroke.getKeyStroke('V',Event.CTRL_MASK));
informationManage.add(veiwMenuItem);
JMenuItem inputMenuItem = new JMenuItem("信息录入(I) ");
inputMenuItem.setAccelerator(KeyStroke.getKeyStroke('I',Event.CTRL_MASK));
informationManage.add(inputMenuItem);
JMenuItem searchMenuItem = new JMenuItem("信息查询(S) ");
searchMenuItem.setAccelerator(KeyStroke.getKeyStroke('S',Event.CTRL_MASK));
informationManage.add(searchMenuItem);
JMenuItem modifyMenuItem = new JMenuItem("信息修改(M) ");
modifyMenuItem.setAccelerator(KeyStroke.getKeyStroke('M',Event.CTRL_MASK));
informationManage.add(modifyMenuItem);
JMenuItem deleteMenuItem = new JMenuItem("信息删除(D) ");
deleteMenuItem.setAccelerator(KeyStroke.getKeyStroke('D',Event.CTRL_MASK));
informationManage.add(deleteMenuItem);
JMenuBar menuBar = new JMenuBar();
this.setJMenuBar(menuBar);
menuBar.add(backMenu);
menuBar.add(informationManage);
veiwMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
String[] columnNames = { "姓名", "性别", "年龄", "籍贯", "婚否" };
//声明数据二维数组
Object[][] data = {
{ "赵刚", "男", new Integer(18), "北京", new Boolean(false) } };
//创建JTable对象t
final JTable t = new JTable(data, columnNames);
//设置JTable对象的可视区,超出部分会用下面的滚动条来显示
t.setPreferredScrollableViewportSize(new Dimension(1000, 700));
//定义一个容器
JPanel p = new JPanel();
//在容器中加入表格和滚动条
p.add(t);
JScrollPane s = new JScrollPane(t);
p.add(s);
//设置容器为透明的
p.setOpaque(true);
//以下代码在前几个Java Swing教程中已经讲过,这里不重复!
MainGUI f = new MainGUI();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(p,BorderLayout.CENTER);
f.pack();
f.setVisible(true);}});}} 展开
package StudentsInformationManageSystem;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.TextArea;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.Statement;
import javax.swing.*;
public class MainGUI extends JFrame{
public MainGUI(){
super("学生信息管理系统");
JMenu backMenu = new JMenu("返回主界面(B)");
backMenu.setMnemonic(KeyEvent.VK_B);
JMenu informationManage= new JMenu("学生信息管理(M)");
informationManage.setMnemonic(KeyEvent.VK_M);
JMenuItem veiwMenuItem = new JMenuItem("信息游览(V) ");
veiwMenuItem.setAccelerator(KeyStroke.getKeyStroke('V',Event.CTRL_MASK));
informationManage.add(veiwMenuItem);
JMenuItem inputMenuItem = new JMenuItem("信息录入(I) ");
inputMenuItem.setAccelerator(KeyStroke.getKeyStroke('I',Event.CTRL_MASK));
informationManage.add(inputMenuItem);
JMenuItem searchMenuItem = new JMenuItem("信息查询(S) ");
searchMenuItem.setAccelerator(KeyStroke.getKeyStroke('S',Event.CTRL_MASK));
informationManage.add(searchMenuItem);
JMenuItem modifyMenuItem = new JMenuItem("信息修改(M) ");
modifyMenuItem.setAccelerator(KeyStroke.getKeyStroke('M',Event.CTRL_MASK));
informationManage.add(modifyMenuItem);
JMenuItem deleteMenuItem = new JMenuItem("信息删除(D) ");
deleteMenuItem.setAccelerator(KeyStroke.getKeyStroke('D',Event.CTRL_MASK));
informationManage.add(deleteMenuItem);
JMenuBar menuBar = new JMenuBar();
this.setJMenuBar(menuBar);
menuBar.add(backMenu);
menuBar.add(informationManage);
veiwMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
String[] columnNames = { "姓名", "性别", "年龄", "籍贯", "婚否" };
//声明数据二维数组
Object[][] data = {
{ "赵刚", "男", new Integer(18), "北京", new Boolean(false) } };
//创建JTable对象t
final JTable t = new JTable(data, columnNames);
//设置JTable对象的可视区,超出部分会用下面的滚动条来显示
t.setPreferredScrollableViewportSize(new Dimension(1000, 700));
//定义一个容器
JPanel p = new JPanel();
//在容器中加入表格和滚动条
p.add(t);
JScrollPane s = new JScrollPane(t);
p.add(s);
//设置容器为透明的
p.setOpaque(true);
//以下代码在前几个Java Swing教程中已经讲过,这里不重复!
MainGUI f = new MainGUI();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(p,BorderLayout.CENTER);
f.pack();
f.setVisible(true);}});}} 展开
4个回答
展开全部
package com.gxa.Export;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.gxa.Main.Main;
import com.gxa.Pub.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class ExportSelectPanel implements ActionListener{
JPanel pSelect;
JComboBox cb;
JTextField tfd;
JLabel lb;
JButton btn1,btn2;
public void actionPerformed(ActionEvent e) {
Object btn = e.getSource();
PreparedStatement prep = null;
ResultSet rs;
if(btn==cb && cb.getSelectedItem().equals("入库日期")){
pSelect.add(lb);
}
if(btn==btn1){
Object cbName = cb.getSelectedItem();
String[] col = { "iExportId", "cEmployeeId", "dShipmentDate",
"cGoodId", "mPrice", "iAmount", "cStorageCode" };
DefaultTableModel mm = new DefaultTableModel(col, 0); // 定义一个表的模板
String str = tfd.getText();
try{
Connection conn = Conn.getCon().con;
if(tfd.getText().length()==0){
prep = conn.prepareStatement("select * from Export");
}
else if(cbName.equals("入库单号")){
prep = conn.prepareStatement("select * from Export where iExportId= ?");
prep.setString(1,str);
if(str.equals("")){
JOptionPane.showMessageDialog(ExportMainPanel.getExportMainPanel(), "请输入入库单号!");
return;
}else if(!str.matches("^\\d+$")){
JOptionPane.showMessageDialog(ExportMainPanel.getExportMainPanel(), "入库单号只能为数字!");
return;
}
}
else if(cbName.equals("经手人ID")){
prep = conn.prepareStatement("select * from Export where cEmployeeId= ?");
prep.setString(1,str);
if(str.equals("")){
JOptionPane.showMessageDialog(ExportMainPanel.getExportMainPanel(), "请输入经手人!");
return;
}else if(!str.matches("^\\d+$")){
JOptionPane.showMessageDialog(ExportMainPanel.getExportMainPanel(), "经手人只能为数字!");
return;
}
}
else if(cbName.equals("入库日期")){
prep = conn.prepareStatement("select * from Export where dShipmentDate = '"+str+"'");
}
else if(cbName.equals("货品编号")){
prep = conn.prepareStatement("select * from Export where cCommodityId= ?");
prep.setString(1,str);
if(str.equals("")){
JOptionPane.showMessageDialog(ExportMainPanel.getExportMainPanel(), "请输入货品编号!");
return;
}else if(!str.matches("^\\d+$")){
JOptionPane.showMessageDialog(ExportMainPanel.getExportMainPanel(), "货品编号只能为数字!");
return;
}
}
else if(cbName.equals("仓库编号")){
prep = conn.prepareStatement("select * from Export where cStorageCode= ?");
prep.setString(1,str);
if(str.equals("")){
JOptionPane.showMessageDialog(ExportMainPanel.getExportMainPanel(), "请输入仓库编号!");
return;
}else if(!str.matches("^\\d+$")){
JOptionPane.showMessageDialog(ExportMainPanel.getExportMainPanel(), "仓库编号只能为数字!");
return;
}
}
rs = prep.executeQuery();// 将查询得到的结果集给rs
while (rs.next()) {
String iExportId = rs.getString(1);
String cEmployeeId = rs.getString(2);
String dShipmentDate = rs.getString(3);
String cCommodityId = rs.getString(4);
String mPrice = rs.getString(5);
String iAmount = rs.getString(6);
String cStorageCode = rs.getString(7);
String[] str_row = { iExportId, cEmployeeId, dShipmentDate,
cCommodityId, mPrice, iAmount, cStorageCode }; // 将一行的数据存在str_row
// 字符串数组里
mm.addRow(str_row);// 添加在表模板中
}
rs.close();
prep.close();
ExportMainPanel.getExportMainPanel().jtable.setModel(mm);
ExportMainPanel.getExportMainPanel().scp.add(ExportMainPanel.getExportMainPanel().jscrollpane);
// 将加载了表的滚动条在JFrame中显示
}catch(Exception ee){
ee.printStackTrace();
}
}
if(btn==btn2){
Main.getMain().setVisible(true);
ExportMainPanel.getExportMainPanel().setVisible(false);
}
}
public ExportSelectPanel(){
cb = new JComboBox();
tfd = new JTextField(10);
lb = new JLabel("日期格式2008-01-01 00:00:00");
pSelect = new JPanel();
btn1 = new JButton("确定查询");
btn2 = new JButton("返回主界面");
cb.addItem("入库单号");
cb.addItem("经手人ID");
cb.addItem("入库日期");
cb.addItem("货品编号");
cb.addItem("仓库编号");
cb.addActionListener(this);
btn1.addActionListener(this);
btn2.addActionListener(this);
pSelect.add(cb);
pSelect.add(tfd);
pSelect.add(btn1);
pSelect.add(btn2);
}
}
希望这段代码可以帮到你
import java.awt.event.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.gxa.Main.Main;
import com.gxa.Pub.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class ExportSelectPanel implements ActionListener{
JPanel pSelect;
JComboBox cb;
JTextField tfd;
JLabel lb;
JButton btn1,btn2;
public void actionPerformed(ActionEvent e) {
Object btn = e.getSource();
PreparedStatement prep = null;
ResultSet rs;
if(btn==cb && cb.getSelectedItem().equals("入库日期")){
pSelect.add(lb);
}
if(btn==btn1){
Object cbName = cb.getSelectedItem();
String[] col = { "iExportId", "cEmployeeId", "dShipmentDate",
"cGoodId", "mPrice", "iAmount", "cStorageCode" };
DefaultTableModel mm = new DefaultTableModel(col, 0); // 定义一个表的模板
String str = tfd.getText();
try{
Connection conn = Conn.getCon().con;
if(tfd.getText().length()==0){
prep = conn.prepareStatement("select * from Export");
}
else if(cbName.equals("入库单号")){
prep = conn.prepareStatement("select * from Export where iExportId= ?");
prep.setString(1,str);
if(str.equals("")){
JOptionPane.showMessageDialog(ExportMainPanel.getExportMainPanel(), "请输入入库单号!");
return;
}else if(!str.matches("^\\d+$")){
JOptionPane.showMessageDialog(ExportMainPanel.getExportMainPanel(), "入库单号只能为数字!");
return;
}
}
else if(cbName.equals("经手人ID")){
prep = conn.prepareStatement("select * from Export where cEmployeeId= ?");
prep.setString(1,str);
if(str.equals("")){
JOptionPane.showMessageDialog(ExportMainPanel.getExportMainPanel(), "请输入经手人!");
return;
}else if(!str.matches("^\\d+$")){
JOptionPane.showMessageDialog(ExportMainPanel.getExportMainPanel(), "经手人只能为数字!");
return;
}
}
else if(cbName.equals("入库日期")){
prep = conn.prepareStatement("select * from Export where dShipmentDate = '"+str+"'");
}
else if(cbName.equals("货品编号")){
prep = conn.prepareStatement("select * from Export where cCommodityId= ?");
prep.setString(1,str);
if(str.equals("")){
JOptionPane.showMessageDialog(ExportMainPanel.getExportMainPanel(), "请输入货品编号!");
return;
}else if(!str.matches("^\\d+$")){
JOptionPane.showMessageDialog(ExportMainPanel.getExportMainPanel(), "货品编号只能为数字!");
return;
}
}
else if(cbName.equals("仓库编号")){
prep = conn.prepareStatement("select * from Export where cStorageCode= ?");
prep.setString(1,str);
if(str.equals("")){
JOptionPane.showMessageDialog(ExportMainPanel.getExportMainPanel(), "请输入仓库编号!");
return;
}else if(!str.matches("^\\d+$")){
JOptionPane.showMessageDialog(ExportMainPanel.getExportMainPanel(), "仓库编号只能为数字!");
return;
}
}
rs = prep.executeQuery();// 将查询得到的结果集给rs
while (rs.next()) {
String iExportId = rs.getString(1);
String cEmployeeId = rs.getString(2);
String dShipmentDate = rs.getString(3);
String cCommodityId = rs.getString(4);
String mPrice = rs.getString(5);
String iAmount = rs.getString(6);
String cStorageCode = rs.getString(7);
String[] str_row = { iExportId, cEmployeeId, dShipmentDate,
cCommodityId, mPrice, iAmount, cStorageCode }; // 将一行的数据存在str_row
// 字符串数组里
mm.addRow(str_row);// 添加在表模板中
}
rs.close();
prep.close();
ExportMainPanel.getExportMainPanel().jtable.setModel(mm);
ExportMainPanel.getExportMainPanel().scp.add(ExportMainPanel.getExportMainPanel().jscrollpane);
// 将加载了表的滚动条在JFrame中显示
}catch(Exception ee){
ee.printStackTrace();
}
}
if(btn==btn2){
Main.getMain().setVisible(true);
ExportMainPanel.getExportMainPanel().setVisible(false);
}
}
public ExportSelectPanel(){
cb = new JComboBox();
tfd = new JTextField(10);
lb = new JLabel("日期格式2008-01-01 00:00:00");
pSelect = new JPanel();
btn1 = new JButton("确定查询");
btn2 = new JButton("返回主界面");
cb.addItem("入库单号");
cb.addItem("经手人ID");
cb.addItem("入库日期");
cb.addItem("货品编号");
cb.addItem("仓库编号");
cb.addActionListener(this);
btn1.addActionListener(this);
btn2.addActionListener(this);
pSelect.add(cb);
pSelect.add(tfd);
pSelect.add(btn1);
pSelect.add(btn2);
}
}
希望这段代码可以帮到你
展开全部
希望这个代码对你有帮助!:
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
public class TableModelTest extends JFrame {
private JTable table;
private AbstractTableModel myModel;
public TableModelTest(){
myModel=new MyTableModel();
table=new JTable(myModel);
JScrollPane jsp=new JScrollPane(table);
Container cp=getContentPane();
cp.add(jsp,BorderLayout.CENTER);
}
public static void main(String[] args) {
TableModelTest tmt=new TableModelTest();
tmt.setTitle("表模型测试");
tmt.setSize(800,600);
tmt.setVisible(true);
}
}
class MyTableModel extends AbstractTableModel{
Object[][] rowData={
{"e01","郭旭东","男","1986-5-26","5000","d01"},
{"e01","郭旭东","男","1986-5-26","5000","d01"},
{"e01","郭旭东","男","1986-5-26","5000","d01"},
{"e01","郭旭东","男","1986-5-26","5000","d01"},
};
String[] columnName={"职工工号","职工姓名","性别","出生年月","工资","部门编号"};
public int getRowCount(){
return rowData.length;
}
public int getColumnCount(){
return rowData[0].length;
}
public String getColumnName(int c){
return columnName[c];
}
public Object getValueAt(int row,int column){
return rowData[row][column];
}
}
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
public class TableModelTest extends JFrame {
private JTable table;
private AbstractTableModel myModel;
public TableModelTest(){
myModel=new MyTableModel();
table=new JTable(myModel);
JScrollPane jsp=new JScrollPane(table);
Container cp=getContentPane();
cp.add(jsp,BorderLayout.CENTER);
}
public static void main(String[] args) {
TableModelTest tmt=new TableModelTest();
tmt.setTitle("表模型测试");
tmt.setSize(800,600);
tmt.setVisible(true);
}
}
class MyTableModel extends AbstractTableModel{
Object[][] rowData={
{"e01","郭旭东","男","1986-5-26","5000","d01"},
{"e01","郭旭东","男","1986-5-26","5000","d01"},
{"e01","郭旭东","男","1986-5-26","5000","d01"},
{"e01","郭旭东","男","1986-5-26","5000","d01"},
};
String[] columnName={"职工工号","职工姓名","性别","出生年月","工资","部门编号"};
public int getRowCount(){
return rowData.length;
}
public int getColumnCount(){
return rowData[0].length;
}
public String getColumnName(int c){
return columnName[c];
}
public Object getValueAt(int row,int column){
return rowData[row][column];
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
可以添加一个新的JPanel,然后在根据不同的用途调用设置不同的控件,这样就不用新建那么多界面了,一个就能够完成所有的功能!使用的时候就像windows下的不同选项卡一样,但是还是公用同一个窗口!
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
理解不能
你把JTable放到哪,不就在哪显示吗?
你把JTable放到哪,不就在哪显示吗?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询