请帮我调试代码。我正在使用 JTable 制作日历,但是每当我按下下一个或上一个按钮时,日历都无法正确重新绘制。它被“切成两半”,然后再按一次它就消失了。我尝试了不同的方法来处理这个问题,但我真的不知道。
这是代码:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.util.*;
import java.lang.*;
public class NewCalendar extends JFrame
{
private JFrame frame = new JFrame();
private Object[] colNames = {"Sun","Mon","Tue","Wed","Thurs","Fri","Sat"};
private Object[][] days = new Object[6][7];
private JLabel lblMonth, lblYear;
private JButton btnPrev, btnNext, pinBoard, addEntry;
private Container pane;
private JPanel pnlCalendar;
private JComboBox cmbYear;
private int realYear, realMonth, realDay, currYear, currMonth;
GregorianCalendar calx = new GregorianCalendar();
GregorianCalendar cal;
JScrollPane scroll;
JTable table;
TableModel model;
int row;
int column;
public NewCalendar()
{
realDay = calx.get(GregorianCalendar.DAY_OF_MONTH);
realMonth = calx.get(GregorianCalendar.MONTH);
realYear = calx.get(GregorianCalendar.YEAR);
//cal = new GregorianCalendar(2012, 10, 27);
//cal = new GregorianCalendar(realYear, 9, realDay);
cal = new GregorianCalendar(realYear, realMonth+1, realDay);
GregorianCalendar cald = new GregorianCalendar(realYear, realMonth, realDay);
int nod = cald.getActualMaximum(GregorianCalendar.DAY_OF_MONTH);
int som = cal.get(GregorianCalendar.DAY_OF_WEEK);
System.out.println(realYear + " " + realMonth + " " + realDay);
currYear = realYear;
currMonth = realMonth;
for( int i=1; i<=nod; i++ )
{
row = new Integer((i+som-2)/7);
column = (i+som-2)%7;
days[row][column] = new Integer(i);
}
TableModel model = new DefaultTableModel(days, colNames)
{
private static final long serialVersionUID = 1L;
public boolean isCellEditable(int row, int column)
{
return days[row][column] != null;
}
};
table = new JTable(model);
table.setRowHeight(38);
table.setColumnSelectionAllowed(true);
table.setRowSelectionAllowed(true);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
for(int i=0; i<7; i++)
{
table.getColumnModel().getColumn(i).setCellEditor(new ClientsTableRenderer(new JCheckBox()));
}
table.getTableHeader().setReorderingAllowed(false);
table.setPreferredScrollableViewportSize(table.getPreferredSize());
table.setShowHorizontalLines(true);
table.setShowVerticalLines(true);
scroll = new JScrollPane(table);
/*frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout( new BorderLayout() );
frame.pack();
frame.add(scroll);
frame.setLocation(150,150);
frame.setVisible(true);
frame.setSize(325,450);
frame.setResizable(false);*/
this.setTitle("Calendar");
this.setSize(325,450);
pane = this.getContentPane();
pane.setLayout(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setResizable(false);
//create controls
lblMonth = new JLabel("January");
lblYear = new JLabel("Change year: ");
cmbYear = new JComboBox();
btnPrev = new JButton("<<");
btnNext = new JButton(">>");
pinBoard = new JButton("Go to Pin Board");
addEntry = new JButton("Add Entry");
pnlCalendar = new JPanel(null);
for( int i=realYear-100; i<=realYear+100; i++)
{
cmbYear.addItem(String.valueOf(i));
}
//register action listeners
btnPrev.addActionListener( new btnPrev_Action() );
btnNext.addActionListener( new btnNext_Action() );
cmbYear.addActionListener( new cmbYear_Action() );
//add controls to pane
pane.add(pnlCalendar);
pnlCalendar.add(scroll);
pnlCalendar.add(lblMonth);
pnlCalendar.add(lblYear);
pnlCalendar.add(cmbYear);
pnlCalendar.add(btnPrev);
pnlCalendar.add(btnNext);
pnlCalendar.add(pinBoard);
pnlCalendar.add(addEntry);
//set bounds
pnlCalendar.setBounds(0,0,500,500);
lblMonth.setBounds(160-lblMonth.getPreferredSize().width/2, 25, 100,25);
lblYear.setBounds(10,305,80,20);
cmbYear.setBounds(230,305,80,20);
btnPrev.setBounds(10,25,50,25);
btnNext.setBounds(260,25,50,25);
pinBoard.setBounds(10,340,300,25);
addEntry.setBounds(10,380, 300, 25);
scroll.setBounds(10,50,300,250);
refreshCalendar( currMonth, currYear); //refresh calendar
}
public void refreshCalendar( int month, int year )
{
String[] months = {"January","February","March","April","May","June","July","August","September","October","November","December"};
int nod, som; //number of days, start of month
btnPrev.setEnabled(true);
btnNext.setEnabled(true);
days = new Object[6][7];
if( month == 0 && year <= realYear-100)
btnPrev.setEnabled(false);
if( month == 11 && year >= realYear+100 )
btnNext.setEnabled(false);
lblMonth.setText(months[month]); //refresh month label
lblMonth.setBounds(160-lblMonth.getPreferredSize().width/2, 25,180,25); //re-align label with calendar
cmbYear.setSelectedItem(String.valueOf(year));
cal = new GregorianCalendar(year, month, 1);
System.out.println("refreshed: " + year + " " + month + " 1");
nod = cal.getActualMaximum(GregorianCalendar.DAY_OF_MONTH);
som = cal.get(GregorianCalendar.DAY_OF_WEEK);
for( int i=1; i<=nod; i++ )
{
row = new Integer((i+som-2)/7);
column = (i+som-2)%7;
days[row][column] = new Integer(i);
}
for(int i = 0; i < 6; i++)
{
for(int j = 0; j < 7; j++)
{
if(days[i][j] == null) System.out.print(" ");
else System.out.print(days[i][j]+"");
System.out.print("\t");
}
System.out.println();
}
TableModel model1 = new DefaultTableModel(days, colNames)
{
private static final long serialVersionUID = 1L;
public boolean isCellEditable(int row, int column)
{
return days[row][column] != null;
}
};
JTable table1 = new JTable(model1);
table1.setRowHeight(38);
table1.setColumnSelectionAllowed(true);
table1.setRowSelectionAllowed(true);
table1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
for(int i=0; i<7; i++)
{
table1.getColumnModel().getColumn(i).setCellEditor(new ClientsTableRenderer(new JCheckBox()));
}
table1.getTableHeader().setReorderingAllowed(false);
table1.setPreferredScrollableViewportSize(table.getPreferredSize());
table1.setShowHorizontalLines(true);
table1.setShowVerticalLines(true);
JScrollPane scroll1 = new JScrollPane(table1);
// scroll = scroll1;
pnlCalendar.removeAll();
pnlCalendar.add(scroll1);
pnlCalendar.add(lblMonth);
pnlCalendar.add(lblYear);
pnlCalendar.add(cmbYear);
pnlCalendar.add(btnPrev);
pnlCalendar.add(btnNext);
pnlCalendar.add(pinBoard);
pnlCalendar.add(addEntry);
pnlCalendar.revalidate();
// pnlCalendar.repaint();
// pane.repaint();
}
public static void main( String[] args ) throws Exception
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
/*EventQueue.invokeLater(new Runnable()
{
public void run()
{
new NewCalendar();
}
});*/
new NewCalendar();
}
class ClientsTableRenderer extends DefaultCellEditor
{
private JButton button;
private String label;
private boolean clicked;
private int row, col;
private JTable table;
public ClientsTableRenderer(JCheckBox checkBox)
{
super(checkBox);
button = new JButton();
button.setOpaque(true);
button.addActionListener(new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
fireEditingStopped();
}
});
}
public Component getTableCellEditorComponent( JTable table, Object value, boolean isSelected, int row, int column)
{
this.table = table;
this.row = row;
this.col = column;
button.setForeground(Color.black);
button.setBackground(UIManager.getColor("Button background"));
label = (value == null) ? "" : value.toString();
button.setText(label);
clicked = true;
return button;
}
public Object getCellEditorValue()
{
if(clicked)
{
//this is what happens when a cell is clicked
JOptionPane.showMessageDialog( button, "Column with Value: "+table.getValueAt(row,col)+" - Clicked!\n Row: "+row+" Column: "+col);
}
clicked = false;
return new String(label);
}
public boolean stopCellEditing()
{
clicked = false;
return super.stopCellEditing();
}
protected void fireEditingStopped()
{
super.fireEditingStopped();
}
}
class btnPrev_Action implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if( currMonth == 0 )
{
currMonth = 11;
currYear -= 1;
}
else
{
currMonth -= 1;
}
refreshCalendar(currMonth, currYear);
}
}
class btnNext_Action implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if( currMonth == 11 )
{
currMonth = 0;
currYear += 1;
}
else
{
currMonth += 1;
}
refreshCalendar(currMonth, currYear);
}
}
class cmbYear_Action implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if( cmbYear.getSelectedItem() != null )
{
String b = cmbYear.getSelectedItem().toString();
currYear = Integer.parseInt(b);
//ssrefreshCalendar(currMonth, currYear);
}
else
{
currMonth -= 1;
}
refreshCalendar(currMonth, currYear);
}
}
}