我已经开发了一个java代码,它在秋千中显示日历...我想要一些特定的日期以不同的颜色...怎么做...请帮助我... 我正在使用setvalueAt()函数将日期插入表中... 我使用了某种" \ 033 [31; 1m%1 $ d \ 033 [0m"改变颜色...但它不起作用......
package project1_swings;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
public class CalendarProgram{
static JLabel lblMonth, lblYear;
static JButton btnPrev, btnNext;
static JTable tblCalendar;
static JComboBox cmbYear;
static JFrame frmMain;
static Container pane;
static DefaultTableModel mtblCalendar;
static JScrollPane stblCalendar;
static JPanel pnlCalendar;
static int realYear, realMonth, realDay, currentYear, currentMonth;
public static void main (String args[])
{
try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}
catch (ClassNotFoundException e) {}
catch (InstantiationException e) {}
catch (IllegalAccessException e) {}
catch (UnsupportedLookAndFeelException e) {}
frmMain = new JFrame ("Calendar of events");
frmMain.setSize(330, 375);
pane = frmMain.getContentPane();
pane.setLayout(null);
frmMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
lblMonth = new JLabel ("July");
lblYear = new JLabel ("Year: 2014");
cmbYear = new JComboBox();
btnPrev = new JButton ("<<");
btnNext = new JButton (">>");
mtblCalendar = new DefaultTableModel(){public boolean isCellEditable(int rowIndex, int mColIndex){return false;}};
tblCalendar = new JTable(mtblCalendar);
stblCalendar = new JScrollPane(tblCalendar);
pnlCalendar = new JPanel(null);
pnlCalendar.setBorder(BorderFactory.createTitledBorder("Events shown in brackets"));
pane.add(pnlCalendar);
pnlCalendar.add(lblMonth);
pnlCalendar.add(lblYear);
pnlCalendar.add(stblCalendar);
pnlCalendar.setBounds(0, 0, 320, 335);
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);
stblCalendar.setBounds(10, 50, 300, 250);
frmMain.setResizable(false);
frmMain.setVisible(true);
GregorianCalendar cal = new GregorianCalendar();
realDay = cal.get(GregorianCalendar.DAY_OF_MONTH);
realMonth = cal.get(GregorianCalendar.MONTH);
realYear = cal.get(GregorianCalendar.YEAR);
currentMonth = realMonth;
currentYear = realYear;
String[] headers = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
for (int i=0; i<7; i++){
mtblCalendar.addColumn(headers[i]);
}
tblCalendar.getParent().setBackground(tblCalendar.getBackground());
tblCalendar.getTableHeader().setResizingAllowed(false);
tblCalendar.getTableHeader().setReorderingAllowed(false);
tblCalendar.setColumnSelectionAllowed(true);
tblCalendar.setRowSelectionAllowed(true);
tblCalendar.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
tblCalendar.setRowHeight(38);
mtblCalendar.setColumnCount(7);
mtblCalendar.setRowCount(6);
for (int i=realYear-100; i<=realYear+100; i++){
cmbYear.addItem(String.valueOf(i));
}
refreshCalendar (realMonth, realYear);
}
public static void refreshCalendar(int month, int year){
try
{
String[] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
int nod, som; //Number Of Days, Start Of Month
//Allow/disallow buttons
btnPrev.setEnabled(true);
btnNext.setEnabled(true);
month=6;
year=2014;
//if (month == 0 && year <= realYear-10){btnPrev.setEnabled(false);} //Too early
//if (month == 11 && year >= realYear+100){btnNext.setEnabled(false);} //Too late
lblMonth.setText(months[month]); //Refresh the month label (at the top)
// lblMonth.setBounds(160-lblMonth.getPreferredSize().width/2, 25, 180, 25); //Re-align label with calendar
//cmbYear.setSelectedItem(String.valueOf(year)); //Select the correct year in the combo box
//Clear table
// for (int i=0; i<6; i++){
// for (int j=0; j<7; j++){
// mtblCalendar.setValueAt(null, i, j);
//}
//}
//Get first day of month and number of days
GregorianCalendar cal = new GregorianCalendar(year, month, 1);
nod = cal.getActualMaximum(GregorianCalendar.DAY_OF_MONTH);
som = cal.get(GregorianCalendar.DAY_OF_WEEK);
String fname,line;
//Draw calendar
for (int i=1; i<=nod; i++){
try
{
fname="F:/Task Manager/"+i+".txt";
File fn=new File(fname);
if(!fn.exists())
{
int row = new Integer((i+som-2)/7);
int column = (i+som-2)%7;
mtblCalendar.setValueAt(i, row, column);
}
else
{
try
{
int count=0;
BufferedReader br=new BufferedReader(new FileReader(fname));
while((line=br.readLine())!=null)
{
count++;
}
int row = new Integer((i+som-2)/7);
int column = (i+som-2)%7;
mtblCalendar.setValueAt(i+"("+count+")", row, column);
}
catch(NumberFormatException m)
{
System.err.println(m);
}
}
}
catch(Exception e)
{
System.out.println(e);
}
}
//Apply renderers
tblCalendar.setDefaultRenderer(tblCalendar.getColumnClass(0), new tblCalendarRenderer());
}
catch(Exception n)
{
System.out.println(n);
}
}
static class tblCalendarRenderer extends DefaultTableCellRenderer{
public Component getTableCellRendererComponent (JTable table, Object value, boolean selected, boolean focused, int row, int column){
super.getTableCellRendererComponent(table, value, selected, focused, row, column);
if (column == 0 || column == 6){ //Week-end
setBackground(new Color(255, 220, 220));
}
else{ //Week
setBackground(new Color(255, 255, 255));
}
if (value != null){
if (Integer.parseInt(value.toString()) == realDay && currentMonth == realMonth && currentYear == realYear){ //Today
setBackground(new Color(220, 220, 255));
}
}
setBorder(null);
setForeground(Color.black);
return this;
}
}
}
答案 0 :(得分:0)
您的代码
if (column == 0 || column == 6){ //Week-end
setBackground(new Color(255, 220, 220));
}
else{ //Week
setBackground(new Color(255, 255, 255));
}
改为使用此功能。
if(row == 0 && column == 6)
setBackground(new java.awt.Color(0, 0, 255));
else
setBackground(table.getBackground());
}