我有一个问题,它已经困扰我很长时间了......
我有一个带有 gridlayout(6,7) 的 JPanel,我的问题是正确更新数据。我正在制作一个简单的日历软件,当然我正在使用日历。问题是当我更改日历对象 MONTH 字段并将新月份数据获取到此 JPanel 网格布局但数据未正确显示时。
我是在计算我的数据字段不正确还是应该如何更新 MONTH 字段。我试过 set(int,int,int),add(CalendarObject.MONTH,+/-1),roll(CalendarObject.MONTH,true/false)。
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cal.set(cal.YEAR,cal.MONTH-1,1);
cal.add(Calendar.DATE, -cal.get(Calendar.DAY_OF_WEEK));
Component[] comps = JavaCalendar.panel.getComponents();
for (Component comp : comps) {
JPanel p = (JPanel) comp;
JLabel c = (JLabel) p.getComponent(0);
c.setText(day.format(cal.getTime()));
cal.add(Calendar.DATE, +1);
}
}
});
是因为 for 循环大约进行了 42 次并且它更新每个字段并每次调用 add() ......?数据更新有点不正确...示例 5 月 1 日是 Wensday,6 月 1 日是星期六,但是当调用此 actionPerfomed() 时,它显示 6 月 1 日是星期日。
SSCE 代码
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Calendar;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.*;
import java.text.SimpleDateFormat;
import java.util.Locale;
public class JavaCalendar extends JFrame {
private Calendar cal;
public static JPanel paneeli;
public static SimpleDateFormat month = new SimpleDateFormat("M");
private SimpleDateFormat year = new SimpleDateFormat("y");
private SimpleDateFormat day = new SimpleDateFormat("d");
private SimpleDateFormat Day = new SimpleDateFormat("E");
public JavaCalendar() {
super("JavaCalendar");
cal = Calendar.getInstance(Locale.getDefault());
cal.setFirstDayOfWeek(Calendar.MONDAY);
setSize(500,400);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new GridLayout(2,1));
paneeli = paneeli(cal);
add(paneeli);
add(buttons(cal));
setLocationRelativeTo(null);
setVisible(true);
}
public JPanel paneeli(Calendar cal) {
cal.set(Calendar.DATE, 1);
cal.add(Calendar.DATE, -cal.get(Calendar.DAY_OF_WEEK)+2);
JPanel paneeli = new JPanel();
paneeli.setLayout(new GridLayout(6,7));
for(int i = 0;i<6;++i) {
for(int j = 0;j<7;++j) {
JPanel label = new JPanel();
label.setBackground(Color.RED);
if (i == 0) {
if (j == 0) {
// Top left corner, draw all sides
label.setBorder(BorderFactory.createLineBorder(Color.BLACK));
} else {
// Top edge, draw all sides except left edge
label.setBorder(BorderFactory.createMatteBorder(1,
0,
1,
1,
Color.BLACK));
}
} else {
if (j == 0) {
// Left-hand edge, draw all sides except top
label.setBorder(BorderFactory.createMatteBorder(0,
1,
1,
1,
Color.BLACK));
} else {
// Neither top edge nor left edge, skip both top and left lines
label.setBorder(BorderFactory.createMatteBorder(0,
0,
1,
1,
Color.BLACK));
}
}
label.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
JPanel panel = (JPanel) e.getSource();
if (Color.RED.equals(panel.getBackground())) {
panel.setBackground(Color.PINK);
panel.revalidate();
panel.repaint();
}else{
panel.setBackground(Color.RED);
panel.revalidate();
panel.repaint();
}
}
});
JLabel labeli = new JLabel();
labeli.setText(day.format(cal.getTime()));
label.add(labeli);
cal.add(Calendar.DATE, +1);
paneeli.add(label);
}
}
return paneeli;
}
public JPanel buttons(Calendar cal) {
final Calendar calender = (Calendar) cal.clone();
JPanel buttonPanel = new JPanel();
JButton button = new JButton();
JButton buttoni = new JButton();
button.setText("Prev");
buttonPanel.add(button);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
calender.set(calender.YEAR,calender.MONTH-1,1);
calender.add(Calendar.DATE, -calender.get(Calendar.DAY_OF_WEEK));
Component[] comps = JavaCalendar.paneeli.getComponents();
for (Component comp : comps) {
JPanel p = (JPanel) comp;
JLabel c = (JLabel) p.getComponent(0);
c.setText(day.format(calender.getTime()));
calender.add(Calendar.DATE, +1);
}
}
});
buttoni.setText("Next");
buttonPanel.add(buttoni);
buttoni.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
calender.set(calender.YEAR,calender.MONTH+1,1);
calender.add(Calendar.DATE, -calender.get(Calendar.DAY_OF_WEEK));
Component[] comps = JavaCalendar.paneeli.getComponents();
for (Component comp : comps) {
JPanel p = (JPanel) comp;
JLabel c = (JLabel) p.getComponent(0);
c.setText(day.format(calender.getTime()));
calender.add(Calendar.DATE, +1);
}
}
});
cal = (Calendar) calender.clone();
return buttonPanel;
}
public static void main(String[] args) {
new JavaCalendar();
}
}