Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中的datepicker上仅突出显示月份中的当前日期_Java_Date_Datepicker - Fatal编程技术网

在java中的datepicker上仅突出显示月份中的当前日期

在java中的datepicker上仅突出显示月份中的当前日期,java,date,datepicker,Java,Date,Datepicker,我有一个日期选择器程序,它允许我选择日期。我只想突出显示当前月份和当前年份中的当前日期。所有其他内容不需要突出显示。我有显示一个月内日期的按钮。我可以在当前月份和当前年份中突出显示当前日期,但在所有年份的所有月份中突出显示相同的单元格。我怎样才能避免这种情况 import java.util.*; import javax.swing.*; import javax.swing.table.*; import javax.swing.event.*; import javax.swing.te

我有一个日期选择器程序,它允许我选择日期。我只想突出显示当前月份和当前年份中的当前日期。所有其他内容不需要突出显示。我有显示一个月内日期的按钮。我可以在当前月份和当前年份中突出显示当前日期,但在所有年份的所有月份中突出显示相同的单元格。我怎样才能避免这种情况

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());
        }
}  
import java.util.*;
导入javax.swing.*;
导入javax.swing.table.*;
导入javax.swing.event.*;
导入javax.swing.text.*;
导入javax.swing.event.ListSelectionEvent;
导入javax.swing.ListSelectionModel;
导入javax.swing.event.ListSelectionListener;
导入javax.swing.event.TableModelEvent;
导入javax.swing.event.TableModelListener;
导入javax.swing.table.DefaultTableModel;
导入java.awt.Component;
导入java.awt.event.*;
导入java.awt.*;
导入java.awt.Toolkit;
导入java.awt.Component.*;
导入java.awt.Graphics;
导入java.awt.Cursor.*;
导入java.text.*;
公共类日期选择器
{
int month=Calendar.getInstance().get(Calendar.month);
int year=Calendar.getInstance().get(Calendar.year);
JLabel l=新JLabel(“,JLabel.CENTER”);
字符串日期=”;
JDialog d;
JButton[]按钮=新JButton[49];
公共日期选择器(JFrame父级)
{
d=新JDialog();
d、 setModal(真);
字符串[]头={“Sun”、“Mon”、“Tue”、“Wed”、“Thur”、“Fri”、“Sat”};
JPanel p1=新JPanel(新网格布局(7,7));
p1.设置首选尺寸(新尺寸(430120));
用于(int x=0;x6)
按钮[x].addActionListener(新建ActionListener())
{
已执行的公共无效行动(行动事件ae)
{
day=按钮[选择]。getActionCommand();
d、 处置();
}
});
if(x<7)
{
按钮[x].setText(标题[x]);
按钮[x]。设置前景(颜色为红色);
}
p1.添加(按钮[x]);
}
JPanel p2=新的JPanel(新的网格布局(1,3));
JButton previous=新JButton(“>”);
next.addActionListener(新ActionListener()
{
已执行的公共无效行动(行动事件ae)
{
月份++;
显示日期();
}
});
p2.添加(下一步);
d、 添加(p1,BorderLayout.CENTER);
d、 添加(p2,南部边界布局);
d、 包装();
d、 setLocationRelativeTo(父级);
显示日期();
d、 setVisible(真);
}
公开作废显示日期()
{
用于(int x=7;x对于(int x=6+dayOfWeek,day=1;day您缺少else块:

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
}
if(currdate==(x-12)和&currmon==mon和&curryear==year){
按钮[x]。设置字体(字体);
按钮[x]。挫折背景(颜色.绿色);
}否则{

按钮[x]。setFont(?);//缺少else块:

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
}
if(currdate==(x-12)和&currmon==mon和&curryear==year){
按钮[x]。设置字体(字体);
按钮[x]。挫折背景(颜色.绿色);
}否则{

按钮[x]。setFont(?);//缺少else块:

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
}
if(currdate==(x-12)和&currmon==mon和&curryear==year){
按钮[x]。设置字体(字体);
按钮[x]。挫折背景(颜色.绿色);
}否则{

按钮[x]。setFont(?);//缺少else块:

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
}
if(currdate==(x-12)和&currmon==mon和&curryear==year){
按钮[x]。设置字体(字体);
按钮[x]。挫折背景(颜色.绿色);
}否则{

button[x].setFont(?);//嘿,真是个愚蠢的错误……我从没想过。它现在运行得很好。谢谢你的快速回复。我会接受答案的。谢谢……)我无法在中突出显示当前日期