Добрый день! У меня есть проблема с прослушкой кнопок календаря. В моем календаре я сделал все кнопки неактивными, а при нажатии на кнопку Edit то все кнопки месяцев должны стать активными, вот код:
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
class But extends Button implements ActionListener{
Color cr = Color.red;
Color cw = Color.white;
Color cb = Color.black;
But(String label){
super(label);
setSize(10,10);
setBackground(cw);
setForeground(cb);
addActionListener(this);
}
public void actionPerformed(ActionEvent ae){
if(getLabel().equals(" "))return;
if(getBackground().equals(cr)){
setBackground(cw);
setForeground(cb);
}
else{
setBackground(cr);
setForeground(cw);
}
}
}
class EmptyCheckboxPan extends Panel{
CheckBoxPane cbp = new CheckBoxPane();
EmptyCheckboxPan(){
setLayout(null);
setSize(95, 285);
setBackground(Color.darkGray);
setLocation(5, 5);
setVisible(true);
add(cbp);
}
CheckBoxPane getCheckBoxPane(){
return cbp;
}
}
class CheckBoxPane extends Panel{
Checkbox[] cb = new Checkbox[2];
CheckboxGroup cbg = new CheckboxGroup();
String[] label = {"Holidays ", "Working days "};
CheckBoxPane(){
setLayout(null);
setSize(95, 285);
setBackground(Color.darkGray);
setLocation(0, 0);
setVisible(true);
for(int i = 0, k = 90; i < label.length; i++, k = k + 50){
cb[i] = new Checkbox(label[i], cbg, false);
cb[i].setBackground(Color.darkGray);
cb[i].setForeground(Color.white);
cb[i].setSize(90,50);
cb[i].setLocation(3, k);
add(cb[i]);
}
}
}
class EditButton extends Button implements ActionListener{
//CheckBoxPane butpan;
int b;
Color cr = Color.red;
CalendarPanel calpan;
But[][] refBut;
EditButton(But[][] b){
super();
refBut = b;
//butpan = bp;
setLabel("E d i t");
setSize(50,30);
setLocation(25,105);
setBackground(Color.LIGHT_GRAY);
//addActionListener(this);
ActionListener actionlistener = new ButtonsListener();
addActionListener(actionlistener);
}
public void actionPerformed(ActionEvent ae){
//butpan.setVisible(true);
}
public class ButtonsListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
//refBut = calpan.getBut();
for(int i = 0; i < refBut.length; i++){
for(int j = 0; j < 6; j++)
refBut[i][j].setEnabled(true);
}
}
}
}
class OKButton extends Button implements ActionListener{
CheckBoxPane bp;
OKButton(CheckBoxPane bp){
super();
this.bp = bp;
setLabel("O K");
setLocation(25,150);
setSize(50,30);
setBackground(Color.LIGHT_GRAY);
addActionListener(this);
}
public void actionPerformed(ActionEvent ae){
//bp.setVisible(false);
}
}
class ButtonsPane extends Panel{
EditButton eb;
OKButton okb;
But[][] refBut = new But[7][6];
ButtonsPane(EmptyCheckboxPan ecp){
super();
setLayout(null);
setBackground(Color.darkGray);
setSize(100, 285);
setLocation(880, 5);
eb = new EditButton(refBut);
//eb = new EditButton(ecp.getCheckBoxPane());
okb = new OKButton(ecp.getCheckBoxPane());
add(eb);
add(okb);
}
}
class DaysPane{
String[] name = {"Mon","Tue","Wed","Thu","Fri","Sat","Sun"};
Panel p = new Panel();
Button[] refBut = new Button[name.length];
int setx, sety;
DaysPane(int x, int y){
super();
setx = x;
sety = y;
p.setLayout(null);
p.setBackground(Color.gray);
p.setSize(50, 250);
p.setLocation(setx, sety);
for(int i = 0, k = 5; i < name.length; i++, k += 35){
refBut[i] = new Button(name[i]);
refBut[i].setSize(40,30);
refBut[i].setLocation(5,k);
refBut[i].setBackground(Color.white);
p.add(refBut[i]);
}
}
Panel getDaysPane(){
return p;
}
}
class DaysLabel{
Label days = new Label("Days", Label.CENTER);
int setx, sety;
Font font = new Font("Verdana", Font.BOLD, 16);
DaysLabel(int x, int y){
super();
setx = x;
sety = y;
days.setBackground(Color.gray);
days.setFont(font);
days.setLocation(setx, sety);
days.setSize(new Dimension(50, 30));
}
Label getDaysLabel(){
return days;
}
}
class CalendarPanel{
int col;
int dayOfMonth = 0;
int k = 0;
int year;
String monthName;
But[][] b;
JPanel pan = new JPanel();
GregorianCalendar greg;
CalendarPanel(JPanel p, int y, String month){
super();
pan = p;
year = y;
monthName = month;
if(monthName == "September"){
greg = new GregorianCalendar(year,8,1);
k = greg.get(Calendar.DAY_OF_WEEK);
dayOfMonth = greg.getActualMaximum(Calendar.DAY_OF_MONTH);
}
if(monthName == "October"){
greg = new GregorianCalendar(year,9,1);
k = greg.get(Calendar.DAY_OF_WEEK);
dayOfMonth = greg.getActualMaximum(Calendar.DAY_OF_MONTH);
}
if(monthName == "November"){
greg = new GregorianCalendar(year,10,1);
k = greg.get(Calendar.DAY_OF_WEEK);
dayOfMonth = greg.getActualMaximum(Calendar.DAY_OF_MONTH);
}
if(monthName == "December"){
greg = new GregorianCalendar(year,11,1);
k = greg.get(Calendar.DAY_OF_WEEK);
dayOfMonth = greg.getActualMaximum(Calendar.DAY_OF_MONTH);
}
if(monthName == "January"){
greg = new GregorianCalendar(year+1,0,1);
k = greg.get(Calendar.DAY_OF_WEEK);
dayOfMonth = greg.getActualMaximum(Calendar.DAY_OF_MONTH);
}
if(monthName == "February"){
greg = new GregorianCalendar(year+1,1,1);
k = greg.get(Calendar.DAY_OF_WEEK);
dayOfMonth = greg.getActualMaximum(Calendar.DAY_OF_MONTH);
}
if(monthName == "March"){
greg = new GregorianCalendar(year+1,2,1);
k = greg.get(Calendar.DAY_OF_WEEK);
dayOfMonth = greg.getActualMaximum(Calendar.DAY_OF_MONTH);
}
if(monthName == "April"){
greg = new GregorianCalendar(year+1,3,1);
k = greg.get(Calendar.DAY_OF_WEEK);
dayOfMonth = greg.getActualMaximum(Calendar.DAY_OF_MONTH);
}
if(monthName == "May"){
greg = new GregorianCalendar(year+1,4,1);
k = greg.get(Calendar.DAY_OF_WEEK);
dayOfMonth = greg.getActualMaximum(Calendar.DAY_OF_MONTH);
}
//calendar, 31 days
//dates
int[] d = new int[dayOfMonth+1];
for(int i = 1; i < d.length; i++)
d[i] = i;
//determine the day of the week in range 0-7
if(k == 1)
k = 6;
else
k -= 2;
//formation of the blank calendar
int col;
col = k <= 4?5:6;
b = new But[7][col];
for(int i = 0; i < b.length; i++)
for(int j = 0; j < col; j++)
b[i][j] = new But(" ");
//set the days of the week in the panel
pan.setBackground(Color.gray);
pan.setBorder(new EmptyBorder(5,5,5,5));
pan.setLayout(new GridLayout(7,col+1,5,5));
for(int i = 0; i < b.length; i++){
for(int j = 0; j < col; j++){
b[i][j].setEnabled(false);
pan.add(b[i][j]);
}
}
//create the calendar
int n;
n = 1;
for(int j = 0; j < col; j++)
for(int i = j == 0?k:0; (i < b.length) && (n < d.length); i++){
b[i][j].setLabel("" + d[n++]);
}
//Mark the Saturdays and Sundays
for(int i = 5; i < 7; i++)
for(int j = 0; j < col; j++){
if(b[i][j].getLabel().equals(" "))
b[i][j].setBackground(Color.lightGray);
else{
b[i][j].setBackground(Color.red);
b[i][j].setForeground(Color.white);
}
}
}
JPanel getPanel(){
return pan;
}
But[][] getBut(){
return b;
}
}
public class ColorButton {
private static void createAndShowGUI(){
CalendarPanel cp1;
CalendarPanel cp2;
Frame f = new Frame("Teacher's Calendar");
f.setBackground(Color.lightGray);
f.setSize(991,613);
f.setResizable(false);
f.setLayout(new GridLayout(2,1,5,5));
//close the application
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
Panel TopPanel = new Panel();
TopPanel.setBackground(Color.lightGray);
TopPanel.setLayout(null);
Panel BottomPanel = new Panel();
BottomPanel.setBackground(Color.lightGray);
BottomPanel.setLayout(null);
f.add(TopPanel);
f.add(BottomPanel);
//add a empty darkgray panel
EmptyCheckboxPan eckbp = new EmptyCheckboxPan();
TopPanel.add(eckbp);
//add a buttons panel
ButtonsPane buttons = new ButtonsPane(eckbp);
TopPanel.add(buttons);
//add a days button top panel
DaysPane topdays = new DaysPane(105,40);
TopPanel.add(topdays.getDaysPane());
//add a days button bottom panel
DaysPane bottomdays = new DaysPane(5,35);
BottomPanel.add(bottomdays.getDaysPane());
//add a top label DAYS
DaysLabel days = new DaysLabel(105, 5);
TopPanel.add(days.getDaysLabel());
//add a bottom label DAYS
DaysLabel days1 = new DaysLabel(5, 0);
BottomPanel.add(days1.getDaysLabel());
String[] mName1 = {"September","October","November","December"};
Label[] lab = new Label[mName1.length];
Font font = new Font("Verdana", Font.BOLD, 16);
for(int i = 0, k = 160; i < lab.length; i++, k = k + 180){
lab[i] = new Label(mName1[i], Label.CENTER);
lab[i].setBackground(Color.gray);
lab[i].setFont(font);
lab[i].setLocation(k, 5);
lab[i].setSize(new Dimension(175, 30));
TopPanel.add(lab[i]);
}
JPanel[] calendarPanel1 = new JPanel[4];
for(int i = 0, k = 160; i < calendarPanel1.length; i++, k = k + 180){
calendarPanel1[i] = new JPanel();
cp1 = new CalendarPanel(calendarPanel1[i], 2015, mName1[i]);
calendarPanel1[i] = cp1.getPanel();
calendarPanel1[i].setLocation(k, 40);
calendarPanel1[i].setSize(new Dimension(175, 250));
TopPanel.add(calendarPanel1[i]);
}
String[] mName2 = {"January","February","March","April","May"};
Label[] lab2 = new Label[mName2.length];
for(int i = 0, k = 60; i < lab2.length; i++, k = k + 185){
lab2[i] = new Label(mName2[i], Label.CENTER);
lab2[i].setBackground(Color.gray);
lab2[i].setFont(font);
lab2[i].setLocation(k, 0);
lab2[i].setSize(new Dimension(180, 30));
BottomPanel.add(lab2[i]);
}
JPanel[] calendarPanel2 = new JPanel[5];
for(int i = 0, k = 60; i < calendarPanel2.length; i++, k = k + 185){
calendarPanel2[i] = new JPanel();
cp2 = new CalendarPanel(calendarPanel2[i], 2015, mName2[i]);
calendarPanel2[i] = cp2.getPanel();
calendarPanel2[i].setLocation(k, 35);
calendarPanel2[i].setSize(new Dimension(180, 250));
BottomPanel.add(calendarPanel2[i]);
}
f.setLocation(50,50);
f.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
но при нажатии на кнопку Edit ничего не происходит и компилятор выдает ошибку
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at md.iic.EditButton$ButtonsListener.actionPerformed(ColorButton.java:99)
at java.awt.Button.processActionEvent(Unknown Source)
at java.awt.Button.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Помогите пожалуйста решить эту проблему. Спасибо заранее.