📄 mainframe.java
字号:
package chapter1;
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 textArea_1 = new JTextArea(); //显示存货单的信息
final JTextArea textArea_2 = new JTextArea(); //显示提货单的信息
public static void main(String args[]) {
MainFrame frame = new MainFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public MainFrame() {
super();
setTitle("商品库存管理系统");
final JTabbedPane tabbedPane = new JTabbedPane();
final JPanel panel_1 = new JPanel();
final JPanel panel_2 = new JPanel();
final JScrollPane scrollPane_1 = new JScrollPane(textArea_1);
final JScrollPane scrollPane_2 = new JScrollPane(textArea_2);
final JTextField id_1 = new JTextField(4); //存货单某项的ID
final JTextField number_1 = new JTextField(4); //欲修改的存货单某项的数量
final JTextField id_2 = new JTextField(4); //提货单某项的ID
final JTextField number_2 = new JTextField(4); //欲修改的提货单某项的数量
final JPanel panelButton_1 = new JPanel();
final JPanel panelButton_2 = new JPanel();
final JPanel panelInput_1 = new JPanel();
final JPanel panelInput_2 = 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("存货单", panel_1);
tabbedPane.add("提货单", panel_2);
panel_1.setLayout(new BorderLayout());
panel_1.add(scrollPane_1, BorderLayout.CENTER);
panel_1.add(panelButton_1, BorderLayout.SOUTH);
panel_1.add(panelInput_1, BorderLayout.NORTH);
panelInput_1.add(new JLabel("存货单某项的ID:"));
panelInput_1.add(id_1);
panelInput_1.add(new JLabel("商品数量:"));
panelInput_1.add(number_1);
panelButton_1.add(inventoryAddButton);
inventoryAddButton.setText("增加商品数量");
panelButton_1.add(inventoryRemoveButton);
inventoryRemoveButton.setText("减少商品数量");
textArea_1.setEditable(false);
scrollPane_1.setViewportView(textArea_1);
panel_2.setLayout(new BorderLayout());
panel_2.add(scrollPane_2, BorderLayout.CENTER);
panel_2.add(panelButton_2, BorderLayout.SOUTH);
panel_2.add(panelInput_2, BorderLayout.NORTH);
panelInput_2.add(new JLabel("提货单某项的ID:"));
panelInput_2.add(id_2);
panelInput_2.add(new JLabel("商品数量:"));
panelInput_2.add(number_2);
panelButton_2.add(invoiceAddButton);
invoiceAddButton.setText("增加提货单");
panelButton_2.add(invoiceDeleteButton);
invoiceDeleteButton.setText("删除提货单");
textArea_2.setEditable(false);
scrollPane_2.setViewportView(textArea_2);
refreshInventory();
refreshInvoice();
inventoryAddButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
inventory.addUnits(id_1.getText(), Integer
.parseInt(number_1.getText()));
} catch (NumberFormatException e1) {
JOptionPane.showMessageDialog(null, "输入的商品数量错误!", "警告",
JOptionPane.ERROR_MESSAGE);
}
refreshInventory();
}
});
inventoryRemoveButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
inventory.removeUnits(id_1.getText(), Integer
.parseInt(number_1.getText()));
} catch (NumberFormatException e1) {
JOptionPane.showMessageDialog(null, "输入的商品数量错误!", "警告",
JOptionPane.ERROR_MESSAGE);
}
refreshInventory();
}
});
invoiceAddButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
invoice.addItem(id_2.getText(), Integer.parseInt(number_2
.getText()));
} catch (NumberFormatException e1) {
JOptionPane.showMessageDialog(null, "输入的商品数量错误!", "警告",
JOptionPane.ERROR_MESSAGE);
}
refreshInvoice();
refreshInventory();
}
});
invoiceDeleteButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
invoice.delItem(id_2.getText());
refreshInvoice();
}
});
}
//刷新存货单的显示结果
public void refreshInventory() {
String str = null;
for (int i = 0; i < inventory.list.length; i++) {
str = inventory.toString() + "\n";
}
textArea_1.setText(str);
}
//刷新提货单的显示结果
public void refreshInvoice() {
String str = null;
for (int i = 0; i < invoice.list.length; i++) {
str = invoice.toString() + "\n";
}
textArea_2.setText(str);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -