package merchant;
//import java.awt.BorderLayout;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class MainFrame extends JFrame {
private Inventory inventory=new Inventory();
private Invoice invoice =new Invoice(inventory);
final JTextArea textArea1=new JTextArea();
final JTextArea textArea2=new JTextArea();
//constructor
public MainFrame()
{
super();
setTitle("商品库存管理系统");
final JTabbedPane tabbedPane=new JTabbedPane();
final JPanel panel1=new JPanel();
final JPanel panel2=new JPanel();
final JScrollPane scrollPane1=new JScrollPane(textArea1);
final JScrollPane scrollPane2=new JScrollPane(textArea2);
final JTextField id1_field=new JTextField(4);
final JTextField number1_field=new JTextField(4);
final JTextField id2_field=new JTextField(4);
final JTextField number2_field=new JTextField(4);
final JPanel panelButton1=new JPanel();
final JPanel panelButton2=new JPanel();
final JPanel panelInput1=new JPanel();
final JPanel panelInput2=new JPanel();
final JButton inventoryAddButton=new JButton();
final JButton inventoryRemoveButton=new JButton();
final JButton invoiceAddButton=new JButton();
final JButton invoiceDeleteButton=new JButton();
getContentPane().add(tabbedPane,BorderLayout.CENTER);
tabbedPane.add("存货单",panel1);
tabbedPane.add("提货单",panel2);
panel1.setLayout(new BorderLayout());
panel1.add(scrollPane1,BorderLayout.CENTER);
panel1.add(panelButton1,BorderLayout.SOUTH);
panel1.add(panelInput1,BorderLayout.NORTH);
panelInput1.add(new JLabel("存货单某项的id:"));
panelInput1.add(id1_field);
panelInput1.add(new JLabel("商品数量:"));
panelInput1.add(number1_field);
panelButton1.add(inventoryAddButton);
inventoryAddButton.setText("增加商品数量");
panelButton1.add(inventoryRemoveButton);
inventoryRemoveButton.setText("减少商品数量");
textArea1.setEditable(false);
scrollPane2.setViewportView(textArea2);
panel2.setLayout(new BorderLayout());
panel2.add(scrollPane2,BorderLayout.CENTER);
panel2.add(panelButton2,BorderLayout.SOUTH);
panel2.add(panelInput2,BorderLayout.NORTH);
panelInput2.add(new JLabel("提货单某项id:"));
panelInput2.add(id2_field);
panelInput2.add(new JLabel("商品数量:"));
panelInput2.add(number2_field);
panelButton2.add(invoiceAddButton);
invoiceAddButton.setText("增加提货单");
panelButton2.add(invoiceDeleteButton);
invoiceDeleteButton.setText("减少提货单");
textArea2.setEditable(false);
scrollPane2.setViewportView(textArea2);
refreshInventory();
refreshInvoice();
inventoryAddButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
try{
inventory.addUnits(id1_field.getText(), Integer.parseInt(number1_field.getText()));
}catch(NumberFormatException ex){
JOptionPane.showMessageDialog(null,"输入的商品数量错误!","警告",JOptionPane.ERROR_MESSAGE);
}
refreshInventory();
}
});
inventoryRemoveButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
try{
inventory.removeUnits(id1_field.getText(), Integer.parseInt(number1_field.getText()));
}catch(NumberFormatException ex){
JOptionPane.showMessageDialog(null,"输入的商品数量错误!","警告",JOptionPane.ERROR_MESSAGE);
}
refreshInventory();
}
});
invoiceAddButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
try{
invoice.addItem(id2_field.getText(), Integer.parseInt(number2_field.getText()));
}catch(NumberFormatException ex){
JOptionPane.showMessageDialog(null,"输入的商品数量错误!","警告",JOptionPane.ERROR_MESSAGE);
}
refreshInvoice();
refreshInventory();
}
});
invoiceDeleteButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
invoice.delItem(id2_field.getText());
refreshInvoice();
}
});
}
public void refreshInventory()
{
String str=null;
for(int i=0;i<inventory.list.length;i++)
{
str=inventory.toString()+"\n";
}
textArea1.setText(str);
}
public void refreshInvoice()
{
String str=null;
for(int i=0;i<invoice.list.length;i++)
{
str=invoice.toString()+"\n";
}
textArea2.setText(str);
}
public static void main(String[] args)
{
MainFrame frame=new MainFrame();
frame.setBounds(100,100,500,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
评论11