public class paymentSystemGUI extends javax.swing.JFrame {
private JScrollPane jScrollListInventory;
private JScrollPane jScrollCart;
private JList cartList;
private DefaultListModel stock = new DefaultListModel();
private JList inventList;
private inventoryList stockInst;
private inventItem invent1;
private DefaultListModel checkoutBasket = new DefaultListModel();
private JButton addBtn;
private JLabel priceLbl;
private JLabel idLabel;
private JLabel nameLabel;
private JTextField itemNameField;
private JTextField itemIdField;
private JTextField pricefld;
/**
* Auto-generated main method to display this JFrame
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
paymentSystemGUI inst = new paymentSystemGUI();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
}
public paymentSystemGUI() {
super();
initGUI();
}
private void initGUI() {
try {
BorderLayout thisLayout = new BorderLayout();
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
getContentPane().setLayout(null);
{
jScrollListInventory = new JScrollPane();
getContentPane().add(jScrollListInventory);
jScrollListInventory.setBounds(47, 33, 347, 304);
{
ListModel inventListModel =
new DefaultComboBoxModel(
new String[] { "Item One", "Item Two" });
inventList = new JList();
jScrollListInventory.setViewportView(inventList);
inventList.setModel(stock);
inventList.setBounds(267, 33, 352, 314);
}
}
{
jScrollCart = new JScrollPane();
getContentPane().add(jScrollCart);
jScrollCart.setBounds(439, 44, 280, 293);
{
ListModel cartListModel =
new DefaultComboBoxModel(
new String[] { "Item One", "Item Two" });
cartList = new JList();
jScrollCart.setViewportView(cartList);
cartList.setModel(checkoutBasket);
cartList.setBounds(454, 58, 296, 340);
}
}
{
itemNameField = new JTextField();
getContentPane().add(itemNameField);
itemNameField.setBounds(34, 359, 113, 23);
}
{
itemIdField = new JTextField();
getContentPane().add(itemIdField);
itemIdField.setBounds(159, 359, 105, 23);
}
{
nameLabel = new JLabel();
getContentPane().add(nameLabel);
nameLabel.setText("Name of item");
nameLabel.setBounds(32, 394, 115, 16);
}
{
idLabel = new JLabel();
getContentPane().add(idLabel);
idLabel.setText("Id number");
idLabel.setBounds(164, 394, 105, 16);
}
{
priceLbl = new JLabel();
getContentPane().add(priceLbl);
priceLbl.setText("Price");
priceLbl.setBounds(297, 394, 76, 16);
}
{
addBtn = new JButton();
getContentPane().add(addBtn);
addBtn.setText("Add Item to Stock");
addBtn.setBounds(38, 432, 109, 23);
addBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent addItem) {
addButtonPressed();
}
});
}
{
pricefld = new JTextField();
getContentPane().add(pricefld);
pricefld.setBounds(286, 359, 94, 23);
}
pack();
this.setSize(788, 521);
} catch (Exception e) {
//add your error handling code here
e.printStackTrace();
}
}
private void addButtonPressed() {
String newid = itemIdField.getText();
String newitemName = itemNameField.getText();
String newprice = pricefld.getText();
if (newid.equals("") || newitemName.equals("") || newprice.equals("")) {
JOptionPane.showMessageDialog(this, "Please Enter Full Details");
} else {
stockInst.addInventItem(newid, newitemName, newprice);
inventItem newBasket = stockInst.findItemByName(newid);
inventList.setSelectedValue(newBasket, true);
clearAllTextFields();
}
}
private void clearAllTextFields() {
itemIdField.setText("");
itemNameField.setText("");
pricefld.setText("");
}
}
大家好。 addButtonPressed方法的例外是链接到GUI中的一个按钮。我正在尝试将一组详细信息添加到我的应用程序中的jlist中。点到第164行:
stockInst.addInventItem(newid, newitemName, newprice);
感谢您的帮助。
答案 0 :(得分:2)
在使用之前,您必须将inventoryList
的实例分配给stockInst
:
stockInst = new inventoryList ();
...
stockInst.addInventItem(newid, newitemName, newprice);
inventItem newBasket = stockInst.findItemByName(newid);