使用java设计程序并给出具体详细代码。要求如下1.输入同学的年龄,计算该同学的生肖; 2.输入同学的出生年份,计算该同学的生肖; 3.将多位同学的年龄存入一个txt文档。软件可以读取该文档,软件将所有的同学生肖计算出; 4.将多位同学的出生年份存入一个txt文档。软件可以读取文档,软件将所有的同学生肖计算出; 5.文件的文本格式如下(也可以增加更多信息):学号 姓名 班级 年龄或者出生年份 6.UI界面仅供参考(如果出生日期计算不方便,可以分成 年 月 日 三列) 手工在table里面输入信息,然后点击按钮,生成生肖信息; 读取一个文本,将信息展现在table里面,点击按钮,生成生肖信息; 7.具体要求: UI界面上要有输入框和表格以容纳输入信息,需要有按钮等激活计算功能; UI界面上有区域显示计算结果; UI界面的布局尽可能美观,大方;
时间: 2024-03-19 20:46:17 浏览: 78
以下是一个基于Swing库的Java程序设计方案,实现输入同学的年龄或出生年份,计算该同学的生肖,以及将多位同学的年龄或出生年份存入txt文档并计算所有同学的生肖。同时,提供UI界面方便用户操作。
```
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
public class ZodiacCalculator extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel panel;
private JLabel label;
private JTextField textField;
private JButton button;
private JTable table;
private DefaultTableModel model;
public ZodiacCalculator() {
initComponents();
}
private void initComponents() {
panel = new JPanel();
label = new JLabel("请输入年龄或出生年份:");
textField = new JTextField(10);
button = new JButton("计算生肖");
model = new DefaultTableModel(new Object[] {"学号", "姓名", "班级", "年龄或出生年份", "生肖"}, 0);
table = new JTable(model);
JScrollPane scrollPane = new JScrollPane(table);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
buttonActionPerformed(evt);
}
});
panel.add(label);
panel.add(textField);
panel.add(button);
this.getContentPane().add(panel, BorderLayout.NORTH);
this.getContentPane().add(scrollPane, BorderLayout.CENTER);
this.pack();
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void buttonActionPerformed(ActionEvent evt) {
String input = textField.getText();
if (input.isEmpty()) {
return;
}
String zodiac = calculateZodiac(input);
if (zodiac != null) {
model.addRow(new Object[] {"", "", "", input, zodiac});
}
textField.setText("");
}
private String calculateZodiac(String input) {
try {
int year = Integer.parseInt(input);
int zodiacIndex = (year - 4) % 12;
String[] zodiacs = {"鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪"};
return zodiacs[zodiacIndex];
} catch (NumberFormatException e) {
return null;
}
}
public static void main(String[] args) {
ZodiacCalculator calculator = new ZodiacCalculator();
// 读取年龄信息并计算所有同学的生肖
ArrayList<Student> ageStudents = readStudentsFromFile("age.txt");
for (Student student : ageStudents) {
String zodiac = calculator.calculateZodiac(student.getAge());
student.setZodiac(zodiac);
calculator.model.addRow(student.toArray());
}
// 读取出生年份信息并计算所有同学的生肖
ArrayList<Student> yearStudents = readStudentsFromFile("year.txt");
for (Student student : yearStudents) {
String zodiac = calculator.calculateZodiac(student.getYear());
student.setZodiac(zodiac);
calculator.model.addRow(student.toArray());
}
calculator.setVisible(true);
}
private static ArrayList<Student> readStudentsFromFile(String filename) {
ArrayList<Student> students = new ArrayList<Student>();
File file = new File(filename);
Scanner scanner;
try {
scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] fields = line.split(" ");
if (fields.length >= 4) {
Student student = new Student(fields[0], fields[1], fields[2], fields[3]);
students.add(student);
}
}
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return students;
}
}
class Student {
private String id;
private String name;
private String className;
private String age;
private String year;
private String zodiac;
public Student(String id, String name, String className, String ageOrYear) {
this.id = id;
this.name = name;
this.className = className;
if (ageOrYear.length() == 4) {
this.year = ageOrYear;
this.age = String.valueOf(2021 - Integer.parseInt(year));
} else {
this.age = ageOrYear;
this.year = String.valueOf(2021 - Integer.parseInt(age));
}
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getClassName() {
return className;
}
public String getAge() {
return age;
}
public String getYear() {
return year;
}
public String getZodiac() {
return zodiac;
}
public void setZodiac(String zodiac) {
this.zodiac = zodiac;
}
public Object[] toArray() {
return new Object[] {id, name, className, age + " / " + year, zodiac};
}
}
```
以上是一个基于Swing库的Java程序设计方案,实现输入同学的年龄或出生年份,计算该同学的生肖,以及将多位同学的年龄或出生年份存入txt文档并计算所有同学的生肖。同时,提供UI界面方便用户操作。
阅读全文