饲料添加剂信息管理系统
【问题描述】
某饲料添加剂公司需要对所生产的饲料添加剂进行管理。【实现要求】
能够对饲料添加剂(包括编号,名称、主要成分、功效、使用方法和适用对象、单价)
进行增删查改。还应该能对具体的应用情况(客户信息,喂食及使用效果)进行跟踪反馈。
【实现提示】
略名
【选作内容】
GUI 界面实现,其他不在实现要求范围内的功能。
饲料添加剂信息管理系统
【问题描述】
某饲料添加剂公司需要对所生产的饲料添加剂进行管理。【实现要求】
能够对饲料添加剂(包括编号,名称、主要成分、功效、使用方法和适用对象、单价)
进行增删查改。还应该能对具体的应用情况(客户信息,喂食及使用效果)进行跟踪反馈。
【实现提示】
略名
【选作内容】
GUI 界面实现,其他不在实现要求范围内的功能。
回答:在上个答主的基础上修改了一下;效果如下:
package manage;
/**
* @author bbyh
* @date 2022/12/29 0029 17:25
* @description
*/
public class Fodder {
private String id;
private String name;
private String ingredients;
private String effects;
private String usage;
private String target;
private double price;
public Fodder(String id, String name, String ingredients, String effects, String usage, String target, double price) {
this.id = id;
this.name = name;
this.ingredients = ingredients;
this.effects = effects;
this.usage = usage;
this.target = target;
this.price = price;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIngredients() {
return ingredients;
}
public void setIngredients(String ingredients) {
this.ingredients = ingredients;
}
public String getEffects() {
return effects;
}
public void setEffects(String effects) {
this.effects = effects;
}
public String getUsage() {
return usage;
}
public void setUsage(String usage) {
this.usage = usage;
}
public String getTarget() {
return target;
}
public void setTarget(String target) {
this.target = target;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
package manage;
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
/**
* @author bbyh
* @date 2022/12/29 0029 17:48
* @description
*/
public class FodderWindow extends JFrame {
private final JTextField idField = new JTextField();
private final JTextField nameField = new JTextField();
private final JTextField ingredientsField = new JTextField();
private final JTextField effectsField = new JTextField();
private final JTextField usageField = new JTextField();
private final JTextField targetField = new JTextField();
private final JTextField priceField = new JTextField();
private final JTable table = new JTable();
private final List<Fodder> additives = new ArrayList<>();
public FodderWindow() {
setLayout(new BorderLayout());
JPanel southPanel = new JPanel();
southPanel.setLayout(new GridLayout(2, 1));
add(southPanel, BorderLayout.SOUTH);
JPanel filedPanel = new JPanel();
filedPanel.setLayout(new GridLayout(7, 2));
southPanel.add(filedPanel);
// 添加Label和TextField
JLabel idLabel = new JLabel("编号");
JLabel nameLabel = new JLabel("名称");
JLabel ingredientsLabel = new JLabel("主要成分");
JLabel effectsLabel = new JLabel("功效");
JLabel usageLabel = new JLabel("使用方法");
JLabel targetLabel = new JLabel("适用对象");
JLabel priceLabel = new JLabel("单价");
filedPanel.add(idLabel);
filedPanel.add(idField);
filedPanel.add(nameLabel);
filedPanel.add(nameField);
filedPanel.add(ingredientsLabel);
filedPanel.add(ingredientsField);
filedPanel.add(effectsLabel);
filedPanel.add(effectsField);
filedPanel.add(usageLabel);
filedPanel.add(usageField);
filedPanel.add(targetLabel);
filedPanel.add(targetField);
filedPanel.add(priceLabel);
filedPanel.add(priceField);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
southPanel.add(buttonPanel);
JButton addButton = new JButton("添加");
addButton.addActionListener(e -> {
String id = idField.getText();
String name = nameField.getText();
String ingredients = ingredientsField.getText();
String effects = effectsField.getText();
String usage = usageField.getText();
String target = targetField.getText();
double price;
try {
price = Double.parseDouble(priceField.getText());
} catch (NumberFormatException ex) {
throw new UnsupportedOperationException("单价需要输入数字");
}
Fodder additive = new Fodder(id, name, ingredients, effects, usage, target, price);
additives.add(additive);
updateTable();
});
JButton deleteButton = new JButton("删除");
deleteButton.addActionListener(e -> {
String id = idField.getText();
for (int i = 0; i < additives.size(); i++) {
if (additives.get(i).getId().equals(id)) {
additives.remove(i);
break;
}
}
updateTable();
});
JButton updateButton = new JButton("修改");
updateButton.addActionListener(e -> {
String id = idField.getText();
for (Fodder additive : additives) {
if (additive.getId().equals(id)) {
additive.setName(nameField.getText());
additive.setIngredients(ingredientsField.getText());
additive.setEffects(effectsField.getText());
additive.setUsage(usageField.getText());
additive.setTarget(targetField.getText());
additive.setPrice(Double.parseDouble(priceField.getText()));
break;
}
}
updateTable();
});
JButton searchButton = new JButton("查询");
searchButton.addActionListener(e -> {
String id = idField.getText();
for (Fodder additive : additives) {
if (additive.getId().equals(id)) {
nameField.setText(additive.getName());
ingredientsField.setText(additive.getIngredients());
effectsField.setText(additive.getEffects());
usageField.setText(additive.getUsage());
targetField.setText(additive.getTarget());
priceField.setText(Double.toString(additive.getPrice()));
break;
}
}
});
buttonPanel.add(addButton);
buttonPanel.add(deleteButton);
buttonPanel.add(updateButton);
buttonPanel.add(searchButton);
Font font = new Font("楷体", Font.BOLD, 18);
idLabel.setFont(font);
idField.setFont(font);
nameLabel.setFont(font);
nameField.setFont(font);
ingredientsLabel.setFont(font);
ingredientsField.setFont(font);
effectsLabel.setFont(font);
effectsField.setFont(font);
usageLabel.setFont(font);
usageField.setFont(font);
targetLabel.setFont(font);
targetField.setFont(font);
priceLabel.setFont(font);
priceField.setFont(font);
addButton.setFont(font);
deleteButton.setFont(font);
updateButton.setFont(font);
searchButton.setFont(font);
// 添加表格
updateTable();
table.getTableHeader().setFont(font);
table.setFont(font);
DefaultTableCellRenderer dc = new DefaultTableCellRenderer();
dc.setHorizontalAlignment(JLabel.CENTER);
table.setDefaultRenderer(Object.class, dc);
add(new JScrollPane(table), BorderLayout.CENTER);
}
private void updateTable() {
// 创建表格模型
DefaultTableModel model = new DefaultTableModel();
model.addColumn("编号");
model.addColumn("名称");
model.addColumn("主要成分");
model.addColumn("功效");
model.addColumn("使用方法");
model.addColumn("适用对象");
model.addColumn("单价");
// 遍历列表,将饲料添加剂信息添加到表格中
for (Fodder additive : additives) {
model.addRow(new Object[]{additive.getId(), additive.getName(), additive.getIngredients(),
additive.getEffects(), additive.getUsage(), additive.getTarget(), additive.getPrice()});
}
// 设置表格的模型
table.setModel(model);
}
}
package manage;
import javax.swing.*;
/**
* @author bbyh
* @date 2022/12/29 0029 18:00
* @description
*/
public class MainFrame {
public static void main(String[] args) {
FodderWindow frame = new FodderWindow();
frame.setTitle("饲料添加剂信息管理系统");
frame.setSize(1200, 700);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}