我尝试使用Java创建日历,该日历在打开时显示当前月份,然后按下按钮时将显示上个月或下个月。似乎底层代码是正确的,因为我能够在命令行中看到月份发生变化,但框架中没有任何变化。我相信这是由于我的ActionListener的实现方式,但我已经尝试了其他一些方法(有些可以看出被注释掉),似乎没有什么可以做到的。
这是我的完整代码,任何帮助都将不胜感激。
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class CalendarHack extends JPanel {
SimpleDateFormat month = new SimpleDateFormat("MMMM");
SimpleDateFormat year = new SimpleDateFormat("yyyy");
SimpleDateFormat day = new SimpleDateFormat("d");
Date date = new Date();
private static JButton back = new JButton("Back");
private static JButton next = new JButton("Next");
public CalendarHack()
{
}
public void paintComponent(Graphics g) {
((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(Color.BLACK);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.WHITE);
g.drawString(month.format(date), 110, 36);
g.drawString(year.format(date), 310, 36);
Calendar today = Calendar.getInstance();
today.setTime(date);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.DATE, 1);
cal.add(Calendar.DATE, -cal.get(Calendar.DAY_OF_WEEK) + 1);
String days = "Sun Mon Tue Wed Thur Fri Sat";
g.drawString(days,50, 90);
g.setFont(new Font("SansSerif", Font.PLAIN, 40));
for (int week = 0; week < 6; week++) {
for (int d = 0; d < 7; d++) {
if(cal.get(Calendar.MONTH) == today.get(Calendar.MONTH))
{
g.drawString(day.format(cal.getTime()) , d * 30 + 50 + ((25 * d)) , week * 29 + 81 + 20 + 30 + (week * 10) );
}
cal.add(Calendar.DATE, +1);
}
}
}
public void paint()
{
JFrame frame = new JFrame();
frame.setPreferredSize(new Dimension(500, 500));
frame.setTitle("Event Calendar");
CalendarHack ch = new CalendarHack();
JPanel panel = new JPanel();
panel.setBounds(135, 350, 200, 200);
ActionListener monthListen = new MonthListener();
// back.addActionListener(new ActionListener()
// {
// public void actionPerformed(ActionEvent e)
// {
// Calendar c = new GregorianCalendar();
// c.setTime(date);
// c.add(Calendar.MONTH, -1);
// date = c.getTime();
// System.out.println("Repainting...");
// repaint();
// System.out.println(date.toString());
// }
// });
back.addActionListener(monthListen);
next.addActionListener(monthListen);
panel.add(back);
panel.add(next);
panel.setBackground(Color.BLACK);
frame.add(panel);
frame.getContentPane().add(ch);
//frame.setUndecorated(true);
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args)
{
CalendarHack cal = new CalendarHack();
cal.paint();
}
class MonthListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == back)
{
Calendar c = new GregorianCalendar();
c.setTime(date);
c.add(Calendar.MONTH, -1);
date = c.getTime();
repaint();
System.out.println(date.toString());
}
else if(e.getSource() == next)
{
Calendar c = new GregorianCalendar();
c.setTime(date);
c.add(Calendar.MONTH, 1);
date = c.getTime();
repaint();
System.out.println(date.toString());
}
//CalendarHack.repaint();
repaint();
}
}
}