我在这里有一个表单,我一直在尝试将Global.table1添加到滚动窗格中,然后将其添加到我的contentpane Global.uniConPane中。使用此当前代码,它仅显示没有列名称的表。当我尝试在下面添加以下代码时:
Global.uniConPane.add(new JScrollPane(Global.table1));
它不会显示任何内容。这些代码集的目的是在销售新商品时添加新行。我希望能够添加滚动窗格但我无法使其工作。
private static void addSalesForm() throws IOException {
Global.UniFrameTitle = "Clinic Inventory Manager";
Global.uniConPane.setOpaque(true);
Global.uniConPane.setBackground(Color.WHITE);
Global.uniConPane.setLayout(null);
Global.chosen="no";
headerFunction();
loggedIn();
menuFunction();
Border border = BorderFactory.createTitledBorder("Select Product");
Border border1 = BorderFactory.createLineBorder(Color.black);
Global.superGroup.setBorder(border);
Global.superGroup.setSize(740,80);
Global.superGroup.setLocation(20,92);
Global.model1 = new DefaultTableModel();
Global.model1.addColumn("Product Name");
Global.model1.addColumn("Price");
Global.model1.addColumn("Quantity");
Global.model1.addColumn("Subtotal");
Global.table1 = new JTable(Global.model1);
Global.table1.setSize(490,350);
Global.table1.setLocation(22,190);
Global.table1.setBorder(border1);
JLabel pro = new JLabel("<html><font color='black'>Product Name<br /></font></html>");
pro.setSize(200,15);
pro.setLocation(50,117);
JLabel pri = new JLabel("<html><font color='black'>Price<br /></font></html>");
pri.setSize(200,15);
pri.setLocation(265,117);
JLabel sto = new JLabel("<html><font color='black'>Item On Hand<br /></font></html>");
sto.setSize(200,15);
sto.setLocation(385,117);
JLabel count = new JLabel("<html><font color='black'>Quantity<br /></font></html>");
count.setSize(200,15);
count.setLocation(505,117);
itemDropDownList();
Global.itemList.setEditable(true);
Global.itemList.setSize(200, 20);
Global.itemList.setLocation(50,135);
JButton addProduct = new JButton("Add Item");
addProduct.setSize(100,20);
addProduct.setLocation(621,135);
new clinicInventorySystem2(Global.itemList);
Global.unitPrice = new JTextField();
Global.unitPrice.setBorder(BorderFactory.createLineBorder(Color.black));
Global.unitPrice.setSize(100,20);Global.unitPrice.setLocation(265,135);
Global.unitPrice.setHorizontalAlignment(JTextField.CENTER);
Global.quantity = new JTextField();
Global.quantity.setBorder(BorderFactory.createLineBorder(Color.black));
Global.quantity.setSize(100,20);Global.quantity.setLocation(385,135);
Global.quantity.setHorizontalAlignment(JTextField.CENTER);
Global.quan = new JTextField();
Global.quan.setBorder(BorderFactory.createLineBorder(Color.black));
Global.quan.setSize(100,20);Global.quan.setLocation(505,135);
Global.quan.setHorizontalAlignment(JTextField.CENTER);
Global.uniConPane.add(Global.unitPrice);
Global.uniConPane.add(Global.quantity);
Global.uniConPane.add(Global.quan);
Global.uniConPane.add(pro);
Global.uniConPane.add(pri);
Global.uniConPane.add(sto);
Global.uniConPane.add(count);
Global.uniConPane.add(addProduct);
Global.uniConPane.add(Global.superGroup);
Global.uniConPane.add(Global.itemList);
Global.uniConPane.add(Global.table1);
addProduct.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
Global.t2 = Integer.parseInt(Global.quan.getText());
Global.subt = Global.t1 * Global.t2;
String temp = ""+Global.subt;
String[] itemize = { Global.valueItem, Global.unitPrice.getText(), Global.quan.getText(),temp };
Global.model1.addRow(itemize);
}
});
Global.itemList.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
JComboBox cb = (JComboBox)e.getSource();
Global.valueItem = (String)cb.getSelectedItem();
Global.chosen = "yes";
Global.valueItemPass = Global.valueItem;
System.out.println(Global.valueItem);
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Global.database =
"jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=D:\\floresClinic.accdb;";
Global.conn = DriverManager.getConnection(Global.database, "", "");
Global.s = Global.conn.createStatement();
Global.rs = Global.s.executeQuery("SELECT * FROM medItem WHERE productID = '" + Global.valueItem+"' ");
int cou = 0;
Integer tempVar=0;
while(Global.rs.next())
{
tempVar=Integer.parseInt(Global.rs.getString("itemID"));
String s1 = Global.rs.getString("unitPrice");
Global.t1=Integer.parseInt(s1);
Global.unitPrice.setText(s1);
Global.unitPrice.setEditable(false);
cou++;
}
Global.rs = Global.s.executeQuery("SELECT * FROM medInventory WHERE itemID = " + tempVar);
cou = 0;
while(Global.rs.next())
{
Global.quantity.setText(Global.rs.getString("qty"));
Global.quantity.setEditable(false);
cou++;
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
});
Global.uniConPane.add(Global.logLabel);
Global.uniConPane.add(Global.bg);
Global.uniFrame.setJMenuBar(Global.menuBar);
Global.uniFrame.setTitle(Global.UniFrameTitle);
Global.uniFrame.setSize(800, 650);
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
int x = (int) ((dimension.getWidth() - Global.uniFrame.getWidth()) / 2);
int y = (int) ((dimension.getHeight() - Global.uniFrame.getHeight()) / 2);
Global.uniFrame.setLocation(x, y-10);
Global.uniFrame.setContentPane(Global.uniConPane);
Global.uniFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Global.uniFrame.setVisible(true);
}
如果我的代码有点难以理解,我真的很抱歉。但我希望有人可以帮我制作滚动窗格。