综合本学期学习的Java语言知识,综合运用包括类的定义,对象的生成;UI技术;IO输入输出等技术,实现一个简单的生肖计算器。请设计一个带UI界面的小软件,输入出生年月日和时间,就能计算出他的生辰八字: 1.具体要求: UI界面上要有输入框和表格以容纳输入信息,需要有按钮等激活计算功能; UI界面上有区域显示计算结果; UI界面的布局尽可能美观,大方;
时间: 2024-03-21 20:38:27 浏览: 84
好的,我可以为您提供一个实现生肖计算器的Java程序,并带有UI界面。
首先,我们需要定义一个Person类来表示一个人,其中包括姓名、性别、出生年月日等信息。代码如下:
```
public class Person {
private String name;
private String gender;
private int year;
private int month;
private int day;
public Person(String name, String gender, int year, int month, int day) {
this.name = name;
this.gender = gender;
this.year = year;
this.month = month;
this.day = day;
}
// getter and setter methods
}
```
接下来,我们需要实现一个生肖计算器的功能,即根据一个人的出生年份,计算出他的生肖。代码如下:
```
public class ZodiacCalculator {
public static String calculateZodiac(int year) {
String[] zodiacs = {"猴", "鸡", "狗", "猪", "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊"};
return zodiacs[year % 12];
}
}
```
最后,我们需要实现一个带UI界面的小软件,代码如下:
```
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ZodiacCalculatorUI extends JFrame {
private JPanel inputPanel;
private JTextField nameField;
private JTextField genderField;
private JTextField yearField;
private JTextField monthField;
private JTextField dayField;
private JButton calculateButton;
private JPanel resultPanel;
private JTable resultTable;
public ZodiacCalculatorUI() {
// set up input panel
inputPanel = new JPanel(new GridLayout(6, 2));
inputPanel.add(new JLabel("姓名:"));
nameField = new JTextField();
inputPanel.add(nameField);
inputPanel.add(new JLabel("性别:"));
genderField = new JTextField();
inputPanel.add(genderField);
inputPanel.add(new JLabel("出生年份:"));
yearField = new JTextField();
inputPanel.add(yearField);
inputPanel.add(new JLabel("出生月份:"));
monthField = new JTextField();
inputPanel.add(monthField);
inputPanel.add(new JLabel("出生日期:"));
dayField = new JTextField();
inputPanel.add(dayField);
calculateButton = new JButton("计算");
inputPanel.add(calculateButton);
// set up result panel
resultPanel = new JPanel(new BorderLayout());
resultTable = new JTable(new DefaultTableModel(new Object[][]{}, new Object[]{"姓名", "性别", "生肖"}));
resultPanel.add(new JScrollPane(resultTable), BorderLayout.CENTER);
// set up main window
setTitle("生肖计算器");
setLayout(new BorderLayout());
add(inputPanel, BorderLayout.NORTH);
add(resultPanel, BorderLayout.CENTER);
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
// add action listener to calculate button
calculateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// get input data
String name = nameField.getText();
String gender = genderField.getText();
int year = Integer.parseInt(yearField.getText());
int month = Integer.parseInt(monthField.getText());
int day = Integer.parseInt(dayField.getText());
// calculate zodiac
String zodiac = ZodiacCalculator.calculateZodiac(year);
// add result to table
DefaultTableModel model = (DefaultTableModel) resultTable.getModel();
model.addRow(new Object[]{name, gender, zodiac});
// save result to file
saveResultToFile(name, gender, zodiac);
}
});
}
private void saveResultToFile(String name, String gender, String zodiac) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String dateStr = dateFormat.format(new Date());
String fileName = "result_" + dateStr + ".txt";
try {
FileWriter writer = new FileWriter(fileName, true);
writer.write(name + "\t" + gender + "\t" + zodiac + "\n");
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
ZodiacCalculatorUI calculatorUI = new ZodiacCalculatorUI();
}
}
```
这个程序使用了Swing库来实现UI界面,并使用了一个JTable来展示计算结果。用户输入姓名、性别、出生年月日后,点击计算按钮,程序会计算出生肖并将结果添加到表格中。同时,程序还会将结果保存到一个文本文件中。
希望这个程序能够符合您的要求,祝您使用愉快!
阅读全文