Java JTable-won';不显示

Java JTable-won';不显示,java,swing,jtable,awt,Java,Swing,Jtable,Awt,我知道有很多关于JTables没有显示的问题,但是我找不到任何与我当前问题相关的东西。我正在使用JCreator Pro作为IDE和MS access的数据库。这是我的密码 import javax.swing.*; import javax.swing.table.*; import java.awt.event.*; import java.awt.*; import java.io.*; import java.sql.*; import java.util.*; public class

我知道有很多关于JTables没有显示的问题,但是我找不到任何与我当前问题相关的东西。我正在使用JCreator Pro作为IDE和MS access的数据库。这是我的密码

import javax.swing.*;
import javax.swing.table.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import java.sql.*;
import java.util.*;
public class JCashiering extends JFrame implements ActionListener,FocusListener {
Container c;
JLabel lblProductCode,lblProductName,lblUnit,lblPrice,lblQuantity,lblAmountTendered,lblTotalAmountDue,lblPayment,lblChange,lblFind;
JTextField txtProductCode,txtProductName,txtPrice,txtQuantity,txtAmountTendered,txtTotalAmountDue,txtPayment,txtChange,txtFind,txtUnit;
JTable dgvProductInformation;
JPanel buttonPanel;
JButton btnFind,btnAdd,btnRemove,btnProceed,btnCancel,btnExit;
JScrollPane scrollPane;
Vector columnNames;
Vector data;

static Connection con;
static ResultSet rs;
static Statement stmt;

static int Quantity=0;
static double Change=0.0;

public JCashiering() {
    c=getContentPane();
    setLayout(null);

    //addlbl
    add(lblProductCode = new JLabel("Product Code:"));
    add(lblProductName = new JLabel("Product Name:"));
    add(lblUnit = new JLabel("Unit:"));
    add(lblPrice = new JLabel("Price:"));
    add(lblQuantity = new JLabel("Quantity:"));
    add(lblAmountTendered = new JLabel("Amount tendered:"));
    add(lblTotalAmountDue = new JLabel("Total amout due:"));
    add(lblPayment = new JLabel("Payment:"));
    add(lblChange = new JLabel("Change:"));
    //setboundslbl1st
    lblProductCode.setBounds(20,250,100,50);
    lblProductName.setBounds(20,280,100,50);
    lblUnit.setBounds(20,310,100,50);
    lblPrice.setBounds(20,340,100,50);
    lblQuantity.setBounds(20,370,100,50);
    //setboundslbl2nd
    lblAmountTendered.setBounds(300,250,130,50);
    lblTotalAmountDue.setBounds(300,280,120,50);
    lblPayment.setBounds(300,310,100,50);
    lblChange.setBounds(300,340,100,50);
    //addtxt
    add(txtProductCode=new JTextField());
    add(txtProductName=new JTextField());
    add(txtPrice=new JTextField());
    add(txtUnit=new JTextField());
    add(txtQuantity =new JTextField());
    add(txtAmountTendered = new JTextField());
    add(txtTotalAmountDue = new JTextField());
    add(txtPayment = new JTextField());
    add(txtChange = new JTextField());
    //settxt&cbo1st
    txtProductCode.setBounds(110,265,150,20);
    txtProductName.setBounds(110,295,150,20);
    txtUnit.setBounds(110,325,150,20);
    txtPrice.setBounds(110,355,150,20);
    txtQuantity.setBounds(110,385,150,20);
    //settxt2nd
    txtAmountTendered.setBounds(410,265,150,20);
    txtTotalAmountDue.setBounds(410,295,150,20);
    txtPayment.setBounds(410,325,150,20);
    txtChange.setBounds(410,355,150,20);
    //btnadd
    add(btnAdd = new JButton("Add to cart"));
    add(btnFind = new JButton("Find"));
    add(btnRemove = new JButton("Remove"));
    add(btnProceed = new JButton("Proceed"));
    add(btnCancel = new JButton("Cancel"));
    add(btnExit = new JButton("Exit"));
    //find
    add(lblFind = new JLabel("Find:"));
    lblFind.setBounds(20,230,150,20);
    add(txtFind = new JTextField());
    txtFind.setBounds(60,230,500,20);
    //btnset
    btnAdd.setBounds(110,420,100,25);
    btnAdd.addActionListener(this);
    btnRemove.setBounds(110,450,100,25);
    btnRemove.addActionListener(this);
    btnProceed.setBounds(410,385,150,25);
    btnProceed.addActionListener(this);
    btnCancel.setBounds(300,420,100,25);
    btnCancel.addActionListener(this);
    btnExit.setBounds(300,450,100,25);
    btnExit.addActionListener(this);
    //focuslistener
    txtQuantity.addFocusListener(this);
    txtPayment.addFocusListener(this);
    //findlistener
    txtFind.addActionListener(this);
    //Disablebtns
    setDisabled(txtProductCode,txtPrice,txtAmountTendered,txtTotalAmountDue,txtChange,txtUnit,txtProductName);
    //dgv
    columnNames = new Vector();
    data = new Vector();

    try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection connect =DriverManager.getConnection("jdbc:odbc:dbCashiering");

            String sql = "Select * from tblProduct";
            Statement stmt = connect.createStatement();
            ResultSet rs = stmt.executeQuery( sql );
            ResultSetMetaData md = rs.getMetaData();
            int columns = md.getColumnCount();
            for (int i = 1; i <= columns; i++) {
            columnNames.addElement( md.getColumnName(i) );
        }
            while (rs.next()) {
            Vector row = new Vector(columns);
            for (int i = 1; i <= columns; i++) {
            row.addElement( rs.getObject(i) );
        }
        data.addElement( row );
        }
        rs.close();
        stmt.close();
        }
        catch(Exception e) {
        JOptionPane.showMessageDialog(null,"Connection failed!");
        }
        //adddgv
    add(dgvProductInformation =new JTable(data, columnNames));
    dgvProductInformation.setBounds(20,20,545,200);
    add(scrollPane = new JScrollPane( dgvProductInformation ));
    add(buttonPanel = new JPanel());
    //frm
    setSize(600,600);
    setVisible(true);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//connection method
/*public static void connect(){
    try{
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            con = DriverManager.getConnection("jdbc:odbc:dbCashiering");
            stmt = con.createStatement();
            //JOptionPane.showMessageDialog(null,"Connection successful!");
        }
    catch(Exception ex){
            JOptionPane.showMessageDialog(null,"Connection failed!");
        }
}*/

//event
        //actionEventdgv
public void actionPerformed(ActionEvent e){
    if(e.getSource()==btnExit){
        System.exit(0);
    }
    if(e.getSource()==btnCancel){
        Clear(txtProductCode,txtProductName,txtPrice,txtQuantity,txtAmountTendered,txtTotalAmountDue,txtPayment,txtChange,txtFind,txtUnit);
    }
        //Event for Find
    if(e.getSource()==txtFind){
        //find action here
        JOptionPane.showMessageDialog(null,"wow");
    }
}
    //focusEvent
public void focusGained(FocusEvent f){
}
public void focusLost(FocusEvent f){
    //validation for field empty
    if(f.getSource()==txtQuantity){
        if(isEmpty(txtQuantity));
    }
    if(f.getSource()==txtPayment){
        if(isEmpty(txtQuantity));
    }
    //validation for correct format
    if(f.getSource()==txtQuantity){
        if(isCorrectFormat(txtQuantity)==false);
    }
    if(f.getSource()==txtPayment){
        if(isCorrectFormat(txtPayment)==false);
    }
}

//main
    public static void main(String[] args) {
    //connect();
    new JCashiering();
}




//method
    public static void setDisabled(JTextField txt1,JTextField txt2,JTextField txt3,JTextField txt4,
    JTextField txt5,JTextField txt6,JTextField txt7){
    txt1.setEnabled(false);
    txt2.setEnabled(false);
    txt3.setEnabled(false);
    txt4.setEnabled(false);
    txt5.setEnabled(false);
    txt6.setEnabled(false);
    txt7.setEnabled(false);
    }
    public static void Clear(JTextField txt1,JTextField txt2,JTextField txt3,JTextField txt4,
    JTextField txt5,JTextField txt6,JTextField txt7,JTextField txt8,JTextField txt9,JTextField txt10){
        txt1.setText("");
        txt2.setText("");
        txt3.setText("");
        txt4.setText("");
        txt5.setText("");
        txt6.setText("");
        txt7.setText("");
        txt8.setText("");
        txt9.setText("");
        txt10.setText("");
    }
    public static boolean isEmpty(JTextField txt){
        if(txt.getText().trim().length()==0){
        JOptionPane.showMessageDialog(null,"Field cannot be empty!");
        txt.requestFocus(true);
        return true;
    }
    else return false;
    }
    public static boolean isCorrectFormat(JTextField txt){
        char temp= ' ';
        for(int i=0;i<txt.getText().length();i++){
        temp=txt.getText().charAt(i);
        if(Character.isDigit(temp)){
        return true;
        }
        else{
            JOptionPane.showMessageDialog(null,"Invalid number format!");
        txt.requestFocus(true);
        }
        return false;
        }
        return true;
    }
import javax.swing.*;
导入javax.swing.table.*;
导入java.awt.event.*;
导入java.awt.*;
导入java.io.*;
导入java.sql.*;
导入java.util.*;
公共类JCashing扩展了JFrame,实现了ActionListener、FocusListener{
容器c;
JLabel lblProductCode、lblProductName、lblUnit、lblPrice、lblQuantity、lblAmountTendered、lbltatalamountdue、lblPayment、lblChange、lblFind;
JTextField txtProductCode、txtProductName、txtPrice、txtQuantity、txtAmountTendered、txtTotalAmountDue、txtPayment、txtChange、txtFind、txtnit;
JTable DGVP产品信息;
JPanel按钮面板;
JButton btnFind、btnAdd、btnRemove、btnproced、btnCancel、btnExit;
JScrollPane滚动窗格;
向量列名称;
矢量数据;
静态连接con;
静态结果集;
静态语句stmt;
静态整数数量=0;
静态双变=0.0;
公共卫生服务(){
c=getContentPane();
setLayout(空);
//addlbl
添加(lblProductCode=newjlabel(“产品代码”);
添加(lblProductName=newjlabel(“产品名称”);
添加(lblUnit=新的JLabel(“单位:”);
添加(lblPrice=newjlabel(“价格:”);
添加(lblQuantity=新的JLabel(“数量”);
添加(lblAmountTendered=新JLabel(“投标金额”);
添加(lblTotalAmountDue=新JLabel(“到期总金额:”);
添加(lblPayment=newjlabel(“付款:”);
添加(lblChange=newjlabel(“更改:”);
//挫折1BL1ST
lblProductCode.setBounds(20250100,50);
lblProductName.setBounds(20280100,50);
lblUnit.setBounds(20310100,50);
lblPrice.setBounds(20340100,50);
LBL立根数(20370100,50);
//挫折第二
LBLAmounted.setBounds(300250130,50);
lblTotalAmountDue.立根(300280120,50);
lblPayment.setBounds(300310100,50);
lblChange.setBounds(300340100,50);
//addtxt
添加(txtProductCode=newjtextfield());
添加(txtProductName=new JTextField());
添加(txtPrice=newjtextfield());
添加(txtUnit=newjtextfield());
添加(txtQuantity=newjtextfield());
添加(txtamountdended=new JTextField());
添加(txtTotalAmountDue=new JTextField());
添加(txtPayment=newjtextfield());
添加(txtChange=newjtextfield());
//settxt和cbo1st
txtProductCode.setBounds(110265150,20);
txtProductName.setBounds(110295150,20);
txtUnit.立根(110325150,20);
txtPrice.setBounds(110355150,20);
TXT立根数(110385150,20);
//SETTXT2
txtamounted.setBounds(410265150,20);
txtTotalAmountDue.setBounds(410295150,20);
txtPayment.setBounds(410325150,20);
txtChange.setBounds(410355150,20);
//btnadd
添加(btnAdd=新的JButton(“添加到购物车”);
添加(btnFind=newjbutton(“Find”);
添加(btnRemove=newjbutton(“删除”);
添加(BTNProced=新的JButton(“继续”);
添加(btnCancel=新的JButton(“取消”);
添加(btnExit=newjbutton(“退出”);
//发现
添加(lblFind=newjlabel(“Find:”);
lblFind.立根(20230150,20);
添加(txtFind=newjtextfield());
txtFind.setBounds(60230500,20);
//btnset
btnAdd.立根(110420100,25);
btnAdd.addActionListener(此);
b删除退步(110450100,25);
btnRemove.addActionListener(此);
BTN加工立根(410385150,25);
btnproced.addActionListener(此);
BTNCEL.立根(300420100,25);
btnCancel.addActionListener(此);
btnExit.setBounds(300450100,25);
btnExit.addActionListener(这个);
//焦点侦听器
txtQuantity.addFocusListener(此);
txtPayment.addFocusListener(此);
//findlistener
txtFind.addActionListener(这个);
//禁用的
setDisabled(txtProductCode、txtPrice、txtAmountTendered、txtTotalAmountDue、txtChange、txtnit、txtProductName);
//dgv
columnNames=新向量();
数据=新向量();
试一试{
forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
Connection connect=DriverManager.getConnection(“jdbc:odbc:dbCashiering”);
String sql=“从TBLPProduct中选择*”;
语句stmt=connect.createStatement();
结果集rs=stmt.executeQuery(sql);
ResultSetMetaData md=rs.getMetaData();
int columns=md.getColumnCount();

对于(inti=1;i您的问题是关于一个表的。发布的代码中95%与表无关

我们对你的程序的核心转储不感兴趣。当你作为一个问题发表一篇适当的文章来说明问题,因为我们没有时间阅读数百行与问题无关的代码

dgvProductInformation.setBounds(20,20,545,200);
add(scrollPane = new JScrollPane( dgvProductInformation ));
作为一个疯狂的问题,我想说的是,你从来没有设置滚动窗格的边界。设置表的边界没有任何作用,因为滚动窗格使用它自己的布局管理器

不要使用空布局!!!不要使用setBounds()


使用布局管理器。Swing设计用于布局管理器,因此您不必担心设置组件的大小和位置。了解如何正确使用Swing。

您的问题是关于表格的。发布的95%代码与表格无关

我们对你的程序的核心转储不感兴趣。当你作为一个问题发表一篇适当的文章来说明问题,因为我们没有时间阅读数百行与问题无关的代码

dgvProductInformation.setBounds(20,20,545,200);
add(scrollPane = new JScrollPane( dgvProductInformation ));
作为一个疯狂的问题,我想说的是,你从来没有设置滚动窗格的边界。设置表的边界没有任何作用,因为滚动窗格使用它自己的布局管理器

不要使用空布局!!!不要使用setBounds()

使用布局管理器。Swing设计用于布局管理器,因此您不必担心设置组件的大小和位置。了解如何使用Swin