用java帮我写一个程序,叫做简易菜谱的设计与实现,要求有图形用户界面,就是操作界面
时间: 2024-03-22 18:37:35 浏览: 21
好的,以下是使用Java Swing实现的简易菜谱程序的代码。该程序实现了添加、删除和修改菜谱的基本功能,并使用图形用户界面进行操作。
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
public class RecipeApp extends JFrame implements ActionListener {
// 定义菜谱列表和当前选中的菜谱
private ArrayList<Recipe> recipeList;
private Recipe currentRecipe;
// 定义GUI组件
private JLabel nameLabel, ingredientLabel, stepsLabel;
private JTextField nameField, ingredientField;
private JTextArea stepsArea;
private JButton addButton, deleteButton, saveButton;
public RecipeApp() {
// 初始化菜谱列表和当前选中的菜谱
recipeList = new ArrayList<>();
currentRecipe = null;
// 设置GUI组件
nameLabel = new JLabel("菜谱名称:");
nameField = new JTextField(20);
ingredientLabel = new JLabel("菜谱材料:");
ingredientField = new JTextField(20);
stepsLabel = new JLabel("菜谱步骤:");
stepsArea = new JTextArea(10, 20);
addButton = new JButton("添加菜谱");
deleteButton = new JButton("删除菜谱");
saveButton = new JButton("保存修改");
// 设置GUI布局
JPanel inputPanel = new JPanel(new GridLayout(3, 2));
inputPanel.add(nameLabel);
inputPanel.add(nameField);
inputPanel.add(ingredientLabel);
inputPanel.add(ingredientField);
inputPanel.add(stepsLabel);
inputPanel.add(stepsArea);
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(addButton);
buttonPanel.add(deleteButton);
buttonPanel.add(saveButton);
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(inputPanel, BorderLayout.NORTH);
mainPanel.add(new JScrollPane(stepsArea), BorderLayout.CENTER);
mainPanel.add(buttonPanel, BorderLayout.SOUTH);
// 添加事件监听器
addButton.addActionListener(this);
deleteButton.addActionListener(this);
saveButton.addActionListener(this);
// 设置窗口属性
setTitle("简易菜谱");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 400);
setLocationRelativeTo(null);
setContentPane(mainPanel);
setVisible(true);
}
public static void main(String[] args) {
new RecipeApp();
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addButton) {
// 添加菜谱
String name = nameField.getText();
String ingredient = ingredientField.getText();
String steps = stepsArea.getText();
recipeList.add(new Recipe(name, ingredient, steps));
JOptionPane.showMessageDialog(this, "菜谱已添加");
} else if (e.getSource() == deleteButton) {
// 删除菜谱
if (currentRecipe != null) {
recipeList.remove(currentRecipe);
currentRecipe = null;
nameField.setText("");
ingredientField.setText("");
stepsArea.setText("");
JOptionPane.showMessageDialog(this, "菜谱已删除");
} else {
JOptionPane.showMessageDialog(this, "请选择要删除的菜谱");
}
} else if (e.getSource() == saveButton) {
// 保存修改
if (currentRecipe != null) {
currentRecipe.setName(nameField.getText());
currentRecipe.setIngredient(ingredientField.getText());
currentRecipe.setSteps(stepsArea.getText());
JOptionPane.showMessageDialog(this, "修改已保存");
} else {
JOptionPane.showMessageDialog(this, "请选择要修改的菜谱");
}
}
}
private class Recipe {
private String name;
private String ingredient;
private String steps;
public Recipe(String name, String ingredient, String steps) {
this.name = name;
this.ingredient = ingredient;
this.steps = steps;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIngredient() {
return ingredient;
}
public void setIngredient(String ingredient) {
this.ingredient = ingredient;
}
public String getSteps() {
return steps;
}
public void setSteps(String steps) {
this.steps = steps;
}
@Override
public String toString() {
return name;
}
}
}
```
使用以上代码,你可以创建一个简单的菜谱程序,实现添加、删除和修改菜谱的功能,并使用图形用户界面进行操作。