📄 calendarpanel.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class CalendarPanel extends JPanel
implements ActionListener
{
public CalendarPanel()
{
l = new JLabel[40];
l1 = new JLabel("Select the Month ");
l2 = new JLabel("Of The Year ");
l3 = new JLabel(" ");
setLayout(new BorderLayout(4, 4));
d = new JLabel[str.length];
p1 = new JPanel();
p2 = new JPanel();
p3 = new JPanel();
p4 = new JPanel();
p1.setLayout(new FlowLayout());
p2.setLayout(new GridLayout(7, 7));
p3.setLayout(new FlowLayout());
p4.setLayout(new BorderLayout());
p4.add(p1, "North");
p4.add(p3, "South");
add(p4, "North");
add(p2, "Center");
setBackground(new Color(128,128,255));
for(int i = 0; i < str.length; i++)
{
d[i] = new JLabel(str[i]);
p2.add(d[i]);
}
for(int j = 1; j < 40; j++)
{
l[j] = new JLabel();
p2.add(l[j]);
}
add(l3, "West");
Font font = new Font("Sanserif", 1, 20);
Font font1 = new Font("Sanserif", 2, 20);
p1.setFont(font);
p3.setFont(font);
setFont(font1);
b2 = new JButton("Exit");
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionevent)
{
System.exit(0);
}
});
b2.setToolTipText("Press To Exit");
b2.setBackground(Color.red);
b2.setMnemonic('e');
tf = new JTextField(5);
ch1 = new JComboBox(mon);
ch1.setBackground(new Color(128,0,255));
ch2 = new JComboBox(per);
ch2.setBackground(new Color(128,0,255));
ch1.setMaximumRowCount(5);
ch2.setMaximumRowCount(2);
ch2.setToolTipText("The Zero Year Works For both A.D & B.C");
ch1.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent itemevent)
{
if(itemevent.getStateChange() == 1)
m = ch1.getSelectedIndex();
}
});
ch2.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent itemevent)
{
if(itemevent.getStateChange() == 1)
period = ch2.getSelectedIndex();
}
});
b = new JButton("Show");
b.setBackground(Color.green);
p1.add(l1);
p1.add(ch1);
p1.add(l2);
p1.add(tf);
p1.add(ch2);
p3.add(b);
p3.add(b2);
p1.setBackground(new Color(128,128,255));
p2.setBackground(new Color(128,128,255));
p3.setBackground(new Color(128,128,255));
b.setMnemonic('s');
b.setToolTipText("Press this button to show the Calendar");
b.addActionListener(this);
}
static boolean isLeap(int i)
{
return i % 400 == 0 || i % 100 != 0 && i % 4 == 0;
}
static int yearStart(int i)
{
int j = 6;
if(i >= 0)
{
for(int k = 0; k <= i; k++)
if(isLeap(k - 1))
{
if((j += 2) > 7)
j -= 7;
} else
if(++j > 7)
j -= 7;
} else
if(i < 0)
{
j++;
for(int i1 = i; i1 < 0; i1++)
if(isLeap(i1))
{
if((j -= 2) < 0)
j += 7;
} else
if(--j < 0)
j += 7;
}
return j;
}
static int monthStart(int i, int j, int k)
{
int i1 = 0;
if(k == 0)
i1 = yearStart(j);
if(k == 1)
i1 = yearStart(-j);
for(int j1 = 1; j1 <= i; j1++)
if(j1 == 4 || j1 == 6 || j1 == 9 || j1 == 11)
{
if((i1 += 2) > 7)
i1 -= 7;
} else
if(j1 == 1 || j1 == 3 || j1 == 5 || j1 == 7 || j1 == 8 || j1 == 10)
{
if((i1 += 3) > 7)
i1 -= 7;
} else
if(j1 == 2)
if(isLeap(j))
i1++;
else
i1 += 0;
return i1;
}
static int countDays(int i, int j)
{
byte byte0 = 0;
if(i == 0 || i == 2 || i == 4 || i == 6 || i == 7 || i == 9 || i == 11)
byte0 = 31;
else
if(i == 3 || i == 5 || i == 8 || i == 10)
byte0 = 30;
else
if(i == 1)
if(isLeap(j))
byte0 = 29;
else
byte0 = 28;
return byte0;
}
public void showCal(int i, int j, int k)
{
day = monthStart(i, j, k);
days = countDays(i, j);
for(int i1 = 1; i1 <= days;)
{
for(int j1 = day; j1 <= day + days;)
{
l[j1].setText(String.valueOf(i1));
j1++;
i1++;
}
for(int k1 = 1; k1 < day; k1++)
l[k1].setText("");
for(int i2 = day + days; i2 < 40; i2++)
l[i2].setText("");
}
}
public void actionPerformed(ActionEvent actionevent)
{
if(actionevent.getSource() == b)
try
{
y = Integer.parseInt(tf.getText());
showCal(m, y, period);
}
catch(NumberFormatException numberformatexception)
{
JOptionPane.showMessageDialog(null, "The Year Field Is Compulsory!", "Attention", 0);
}
catch(NullPointerException nullpointerexception) { }
}
JButton b;
JButton b2;
String str[] = {
"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"
};
JComboBox ch1;
JComboBox ch2;
JLabel l1;
JLabel l2;
JLabel l3;
JLabel l[];
JLabel d[];
JPanel p1;
JPanel p2;
JPanel p3;
JPanel p4;
int year;
int period;
int m;
int y;
int days;
int day;
JTextField tf;
String mon[] = {
"January", "February", "March", "April", "May", "June", "July", "August", "September", "October",
"November", "December"
};
String per[] = {
"A.D", "B.C"
};
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -