【发布时间】:2015-08-29 16:41:57
【问题描述】:
所以我不仅对 java 很陌生,而且对一般编程也很陌生。话虽如此,我正在尝试编写一个程序来创建一个基于日历的笔记,这样根据选择的日期,您可以做笔记并根据月份和年份将其放入文件中,然后检索该笔记和任何其他笔记日复一日。这必须以某种方式使用一个数组,我不知道如何完全实现对如何使用数组完全感到困惑(教程没有帮助)。这就是我目前所拥有的。
首先是 UserInterface.java 文件:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class UserInterface extends JFrame implements ActionListener {
private String[] months = {"January","February","March","April","May","June","July","August","September","October","November","December"};
private String[] days = {"1", "2", "3", "4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"};
private JButton save, retrieve;
private JTextField year;
private JTextArea entry;
private JComboBox month = new JComboBox(months);
private JComboBox day = new JComboBox(days);
public UserInterface() {
JPanel mPanel = new JPanel(new GridLayout(2,1));
mPanel.add(new JLabel("Month"));
mPanel.add(month);
month.addActionListener(this);
JPanel dPanel = new JPanel(new GridLayout(2,1));
dPanel.add(new JLabel("Day"));
dPanel.add(day);
month.addActionListener(this);
JPanel yPanel = new JPanel(new GridLayout(2,1));
yPanel.add(new JLabel("Year"));
yPanel.add(year = new JTextField(4));
month.addActionListener(this);
JPanel p1 = new JPanel(new FlowLayout(FlowLayout.LEFT, 30, 10));
p1.add(new JLabel("Set Date for Entry:"));
p1.add(mPanel);
p1.add(dPanel);
p1.add(yPanel);
JPanel p2 = new JPanel(new FlowLayout(FlowLayout.LEFT, 60, 10));
p2.add(save = new JButton("Save"));
save.addActionListener(this);
p2.add(retrieve = new JButton("Retrieve"));
retrieve.addActionListener(this);
JPanel full = new JPanel(new BorderLayout());
full.add(p1, BorderLayout.NORTH);
full.add(p2, BorderLayout.SOUTH);
full.add(entry = new JTextArea(10, 10), BorderLayout.CENTER);
JFrame frame = new JFrame();
frame.setTitle("Calendar Manager");
frame.setSize(500, 400);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable( false );
frame.setVisible(true);
frame.add(full);
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("Save")) {
String m = (String)month.getSelectedItem();
String d = (String)day.getSelectedItem();
String y = year.getText();
String data = entry.getText();
CalendarManager.save(m, d, y, data);
entry.setText("Data written successfully to file with name "+ month+" "+year+".txt");
} else if(e.getActionCommand().equals("Retrieve")){
String m = (String)month.getSelectedItem();
String d = (String)day.getSelectedItem();
String y = year.getText();
String data = "";
String result = CalendarManager.retrieve(m, d, y, data);
entry.setText(result);
}
}
}
然后是 CalendarManager.java 文件:
import java.io.*;
public class CalendarManager {
private String[] calObject = new String[31];
public static boolean save(String month, String day, String year, String data) {
String fileName = month+" "+year+".dat";
int daynum = Integer.parseInt(day);
try {
File file = new File(fileName);
if(!file.exists()) {
file.createNewFile();
}
ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream(file));
for(int i=0; i<31; i++){
output.writeUTF(month+"-"+day+"-"+year+": "+data);
}
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
public static String retrieve(String month, String day, String year, String data) {
String fileName = month+" "+year+".dat";
int daynum = Integer.parseInt(day);
try {
File file = new File(fileName);
if(!file.exists()) {
return "File not found";
}
ObjectInputStream input = new ObjectInputStream(new FileInputStream(file));
for(int i=0; i<31; i++){
if(input == null){
return "Entry not found";
}
else{
data = input.readUTF();
}
}
} catch (IOException e) {
e.printStackTrace();
}
return data;
}
}
最后是 CalendarTest.java 文件:
public class CalendarTest {
public static void main(String[] args) {
UserInterface calendar = new UserInterface();
}
}
如果有人能告诉我我做错了什么,而且我的代码必须尽可能简单,没有任何我可能没有学过的东西,所以想想初学者的 java 类材料。
【问题讨论】:
-
您在这里面临的确切问题是什么——您在哪里需要帮助?
-
你的“retrieve”方法应该返回一个数组,而不是一个字符串,然后从那里,根据一个月中的哪一天,你可以将那天的文本加载到 JTextArea 中。每次更改日期时,您需要从 JTextArea 中获取值,将值放入数组中的正确位置,然后再更新新日期的文本
-
另外,JTextArea 是一个多行组件,这意味着 Yu 需要考虑这样一个事实,即任何给定条目在您的文件中可能有多行...
-
所以使用代码示例我将如何将 JTextArea 中的行保存到数组中,然后使用另一个代码示例我将如何将其恢复,因为我认为这可能就是我需要的全部知道该怎么做,还是我错了,需要更多帮助。
标签: java arrays jtextarea objectinputstream objectoutputstream