...内容显示在当前的界面上面,而不是新创建一个窗口来显示?
发布网友
发布时间:2024-10-02 22:23
我来回答
共4个回答
热心网友
时间:2024-10-19 15:37
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);
}
}
希望这段代码可以帮到你
热心网友
时间:2024-10-19 15:31
希望这个代码对你有帮助!:
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];
}
}
热心网友
时间:2024-10-19 15:30
理解不能
你把JTable放到哪,不就在哪显示吗?
热心网友
时间:2024-10-19 15:35
可以添加一个新的JPanel,然后在根据不同的用途调用设置不同的控件,这样就不用新建那么多界面了,一个就能够完成所有的功能!使用的时候就像windows下的不同选项卡一样,但是还是公用同一个窗口!
热心网友
时间:2024-10-19 15:35
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);
}
}
希望这段代码可以帮到你
热心网友
时间:2024-10-19 15:32
可以添加一个新的JPanel,然后在根据不同的用途调用设置不同的控件,这样就不用新建那么多界面了,一个就能够完成所有的功能!使用的时候就像windows下的不同选项卡一样,但是还是公用同一个窗口!
热心网友
时间:2024-10-19 15:29
理解不能
你把JTable放到哪,不就在哪显示吗?
热心网友
时间:2024-10-19 15:34
希望这个代码对你有帮助!:
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];
}
}