java gui宠物饲养系统
时间: 2023-08-05 07:07:42 浏览: 159
基于Java的宠物管理系统
5星 · 资源好评率100%
下面是一个简单的Java GUI宠物饲养系统的示例代码,仅供参考:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class PetSystem extends JFrame implements ActionListener {
private JTextField nameField;
private JTextField typeField;
private JTextField ageField;
private JTextField weightField;
private JButton addButton;
private JTextArea petListArea;
public PetSystem() {
super("宠物饲养系统");
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 创建添加宠物的组件
JLabel nameLabel = new JLabel("宠物名称:");
nameField = new JTextField(20);
JLabel typeLabel = new JLabel("宠物种类:");
typeField = new JTextField(20);
JLabel ageLabel = new JLabel("宠物年龄:");
ageField = new JTextField(20);
JLabel weightLabel = new JLabel("宠物体重:");
weightField = new JTextField(20);
addButton = new JButton("添加");
addButton.addActionListener(this);
// 将添加宠物的组件添加到面板中
JPanel addPanel = new JPanel(new GridLayout(5, 2));
addPanel.add(nameLabel);
addPanel.add(nameField);
addPanel.add(typeLabel);
addPanel.add(typeField);
addPanel.add(ageLabel);
addPanel.add(ageField);
addPanel.add(weightLabel);
addPanel.add(weightField);
addPanel.add(addButton);
// 创建宠物列表的组件
JLabel petListLabel = new JLabel("宠物列表:");
petListArea = new JTextArea(15, 30);
petListArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(petListArea);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
// 将宠物列表的组件添加到面板中
JPanel petListPanel = new JPanel(new BorderLayout());
petListPanel.add(petListLabel, BorderLayout.NORTH);
petListPanel.add(scrollPane, BorderLayout.CENTER);
// 将面板添加到窗口中
JPanel panel = new JPanel(new BorderLayout());
panel.add(addPanel, BorderLayout.NORTH);
panel.add(petListPanel, BorderLayout.CENTER);
add(panel);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// 获取用户输入的宠物信息
String name = nameField.getText();
String type = typeField.getText();
int age = Integer.parseInt(ageField.getText());
double weight = Double.parseDouble(weightField.getText());
// 创建宠物对象并添加到列表中
Pet pet = new Pet(name, type, age, weight);
petListArea.append(pet.toString() + "\n");
// 清空用户输入的宠物信息
nameField.setText("");
typeField.setText("");
ageField.setText("");
weightField.setText("");
}
public static void main(String[] args) {
new PetSystem();
}
}
class Pet {
private String name;
private String type;
private int age;
private double weight;
public Pet(String name, String type, int age, double weight) {
this.name = name;
this.type = type;
this.age = age;
this.weight = weight;
}
public String getName() {
return name;
}
public String getType() {
return type;
}
public int getAge() {
return age;
}
public double getWeight() {
return weight;
}
@Override
public String toString() {
return "名称:" + name + ",种类:" + type + ",年龄:" + age + "岁,体重:" + weight + "kg";
}
}
```
在这个例子中,用户可以在添加宠物的面板中输入宠物的名称、种类、年龄和体重,并点击“添加”按钮将宠物添加到宠物列表中。宠物列表使用JTextArea组件实现,并使用JScrollPane组件添加滚动条。当用户点击“添加”按钮后,程序会创建一个Pet对象并将它添加到宠物列表中,并清空用户输入的宠物信息。
阅读全文