我有一个datepicker程序,它允许我选择日期。我想突出显示当前月份和当前年份的当前日期。所有其他人不得突出显示。我有按钮,我在一个月内显示日期。我能够突出显示当前月份和当前年份的当前日期,但所有年份的所有月份都会突出显示相同的单元格。我怎么能避免这个?
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import java.awt.Component;
import java.awt.event.*;
import java.awt.*;
import java.awt.Toolkit;
import java.awt.Component.*;
import java.awt.Graphics;
import java.awt.Cursor.*;
import java.text.*;
public class DatePicker
{
int month = Calendar.getInstance().get(Calendar.MONTH);
int year = Calendar.getInstance().get(Calendar.YEAR);
JLabel l = new JLabel("", JLabel.CENTER);
String day = "";
JDialog d;
JButton[] button = new JButton[49];
public DatePicker(JFrame parent)
{
d = new JDialog();
d.setModal(true);
String[] header = {"Sun","Mon","Tue","Wed","Thur","Fri","Sat"};
JPanel p1 = new JPanel(new GridLayout(7, 7));
p1.setPreferredSize(new Dimension(430, 120));
for(int x = 0; x < button.length; x++)
{
final int selection = x;
button[x] = new JButton();
button[x].setFocusPainted(false);
button[x].setBackground(Color.white);
if(x > 6)
button[x].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
day = button[selection].getActionCommand();
d.dispose();
}
});
if(x < 7)
{
button[x].setText(header[x]);
button[x].setForeground(Color.red);
}
p1.add(button[x]);
}
JPanel p2 = new JPanel(new GridLayout(1, 3));
JButton previous = new JButton("<< Previous");
previous.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
month--;
displayDate();
}
});
p2.add(previous);
p2.add(l);
JButton next = new JButton("Next >>");
next.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
month++;
displayDate();
}
});
p2.add(next);
d.add(p1, BorderLayout.CENTER);
d.add(p2, BorderLayout.SOUTH);
d.pack();
d.setLocationRelativeTo(parent);
displayDate();
d.setVisible(true);
}
public void displayDate()
{
for(int x = 7; x < button.length; x++)
button[x].setText("");
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("MMMM yyyy");
Calendar cal = Calendar.getInstance();
cal.set(year, month, 1);
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
int daysInMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
Font font = new Font("Courier", Font.BOLD, 12);
Calendar curr = new GregorianCalendar();
int currdate = curr.get(Calendar.DAY_OF_MONTH);
int currmon = curr.get(Calendar.MONTH);
int curryear = curr.get(Calendar.YEAR);
int date = cal.get(Calendar.DAY_OF_MONTH);
int mon = cal.get(Calendar.MONTH);
int year = cal.get(Calendar.YEAR);
int day1 = cal.get(Calendar.DAY_OF_WEEK);
int start = (7 - (date - day1) % 7) % 7;
int days = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
System.out.println("currdate : " + currdate);
System.out.println("currmon : " + currmon);
System.out.println("mon : " + mon);
System.out.println("curryear : " + curryear);
System.out.println("year : " + year);
for(int x = 6 + dayOfWeek, day = 1; day <= daysInMonth; x++, day++)
{
button[x].setText("" + day);
System.out.println("x : " + x);
System.out.println("day : " + day);
if(currdate == (x - 12) && currmon == mon && curryear == year)
{
// button[x].setFont(font);
button[day].setBackground(Color.GREEN);
}
else
button[x].setBackground(Color.white);
}
// for(int i = 1; i <= days; i++)
l.setText(sdf.format(cal.getTime()));
d.setTitle("Date Picker");
}
public String setPickedDate()
{
if(day.equals(""))
return day;
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("MM-dd-yyyy");
Calendar cal = Calendar.getInstance();
cal.set(year, month, Integer.parseInt(day));
return sdf.format(cal.getTime());
}
}
更新我的帖子
答案 0 :(得分:3)
你错过了其他阻止:
if(currdate == (x - 12) && currmon == mon && curryear == year) {
button[x].setFont(font);
button[x].setBackground(Color.GREEN);
} else {
button[x].setFont(?); //<-- replace ? with desired font
button[x].setBackground(?); //<-- replace ? with desired color
}