JAVA风景日历,运行时,日历是有了,但风景没有,这是为什么?

运行是这样
图片说明
但是想要这样图片说明

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.*;
import java.util.Timer;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;

//这是一个用JList和JTable完成的简单日历
public class calender
{
public static void main(String[] args)
{
    EventQueue.invokeLater(new Runnable()
    {
     public void run()
     {
      JFrame frame = new CalendarFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
     }
    });
}
}

class CalendarFrame extends JFrame
{
private static final long serialVersionUID = 8067844586793608064L;
JPanel listPanel; // 显示月份的列表面板
JPanel tablePanel; // 显示日期的表格面板
JPanel textPanel; // 文本面板
JMenuBar menuBar; // JMenuBar,JMenu定义的变量都没有实现功能
JMenu fileMenu;
JMenu editMenu;
JMenu formatMenu;
JMenu lookMenu;

JMenu helpMenu;


JEditorPane text; // 自带复制,剪切和删除快捷键功能的面板
JScrollPane textscrollpane;
JScrollPane tablescrollpane;
@SuppressWarnings("rawtypes")
JList list; // 显示月份的列表
JTable table; // 显示日期的表格
JPanel yearPanel; // 显示年份的面板
JSpinner spinner; // JSpinner空间
JLabel label = new JLabel();
public static boolean flag = true;


@SuppressWarnings({ "unchecked", "rawtypes" })
public CalendarFrame()
{
    table = new JTable(new MyTableModel());
    tablescrollpane = new JScrollPane(table);
    for (int i = 0; i < 7; i++)
    {
     TableColumn column = table.getColumnModel().getColumn(i);// 获取表格每一列
     column.setResizable(false);// 不允许改变列的大小
    }

    table.setBackground(Color.GREEN);
    table.setRowHeight(72); // 设定每列的大小
    table.setRowSelectionAllowed(false);// 设定行的选择模式
    table.setDefaultRenderer(String.class, new MyTableRenderer());

    list = new JList(new AbstractListModel() // 匿名的list模型类,提供list显示的月份数据
      {
       private static final long serialVersionUID = -3703164069291737586L;

       public Object getElementAt(int index)
       {
        String obj = null;
        switch (index)
        {
        case 0:
         obj = "Jan";
         break;
        case 1:
         obj = "Feb";
         break;
        case 2:
         obj = "Mar";
         break;
        case 3:
         obj = "Apr";
         break;
        case 4:
         obj = "May";
         break;
        case 5:
         obj = "Jun";
         break;
        case 6:
         obj = "Jul";
         break;
        case 7:
         obj = "Aug";
         break;
        case 8:
         obj = "Sep";
         break;
        case 9:
         obj = "Oct";
         break;
        case 10:
         obj = "Nov";
         break;
        case 11:
         obj = "Dec";
         break;
        }
        return obj;
       }

       @Override
       public int getSize()
       {
        return 12;
       }

      });
    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); // 设定列表的选择模式
    list.setCellRenderer(new MyListCellRenderer()); // 列表元素的绘制类MyListCellRenderer
    list.setSelectedIndex(Calendar.getInstance().get(Calendar.MONTH));// JList默认的月份为现实的月份
    list.addListSelectionListener(new ListSelectionListener()// 当某个列表被选中时,更新日期table控件
      {



       public void valueChanged(ListSelectionEvent e)
       {
        flag = true;
        table.updateUI();
       }

      });
    list.addMouseListener(new MouseAdapter()
    {
     public void mouseClicked(MouseEvent evt)
     {
      if (evt.getClickCount() == 2)
      {
       String s = (String) list.getSelectedValue();
       JOptionPane.showMessageDialog(list, s);
      }
     }
    });
    listPanel = new JPanel();
    listPanel.setLayout(new BorderLayout());
    listPanel.add(list);

    tablePanel = new JPanel();
    tablePanel.setBackground(Color.white);
    tablePanel.setLayout(new BorderLayout());
    tablePanel.add(tablescrollpane);
    // table.setBackground(Color.green);

    yearPanel = new JPanel();// 将label控件和JSpinner控件加入年份面板上
    yearPanel.setLayout(new GridLayout(0, 4));
    JLabel gongyuan = new JLabel("公元", JLabel.RIGHT);
    JLabel year = new JLabel("年");
    spinner = new JSpinner(new SpinnerNumberModel(Calendar.getInstance()
      .get(Calendar.YEAR), null, null, 1));
    spinner.addChangeListener(new ChangeListener() // 为JSpinner空间添加事件监听器
      {

       @Override
       public void stateChanged(ChangeEvent e)
       {
        flag = true;
        table.updateUI();
       }

      });
    yearPanel.add(gongyuan);
    yearPanel.add(spinner);
    yearPanel.add(year);
    yearPanel.add(label);
    showTime();
    new Timer().schedule(new TimerTask()
    {
     @Override
     public void run()
     {
      while (true)
      {
       showTime();
      }
     }

    }, 1000);
    tablePanel.add(yearPanel, BorderLayout.NORTH);// 将年份面板加入到表格面板的北部

    textPanel = new JPanel();




    textPanel.setLayout(new BorderLayout());
    text = new JEditorPane();
    textscrollpane = new JScrollPane(text);
    textPanel.add(textscrollpane);
    text.setFont(new Font("SansSerif", Font.PLAIN, 40));
    tablePanel.add(textPanel, BorderLayout.SOUTH);
    menuBar = new JMenuBar();// JMenu定义的空间都没实现功能
    fileMenu = new JMenu("文件");
    editMenu = new JMenu("编辑");
    formatMenu = new JMenu("格式");
    lookMenu = new JMenu("查看");

    helpMenu = new JMenu("帮助");
    menuBar.add(fileMenu);
    menuBar.add(editMenu);
    menuBar.add(formatMenu);
    menuBar.add(lookMenu);
    menuBar.add(helpMenu);

    GridBagLayout layout = new GridBagLayout(); // 设成GridBagLayout布局
    GridBagConstraints constraints = new GridBagConstraints();
    this.setFont(new Font("SansSerif", Font.PLAIN, 14));
    this.setLayout(layout);
    this.setTitle("风景日历"); // 设定标题
    this.setIconImage(new ImageIcon("res\\title.jpg").getImage()); // 设定图标
    constraints.fill = GridBagConstraints.BOTH;

    constraints.weightx = 0.5;
    constraints.weighty = 0.5;
    layout.setConstraints(listPanel, constraints);
    constraints.weightx = 8.0;
    constraints.weighty = 0.5;
    layout.setConstraints(tablePanel, constraints);

    this.setJMenuBar(menuBar);
    this.add(listPanel);
    this.add(tablePanel);
    this.setSize(this.getToolkit().getScreenSize().width * 3 / 4, this
      .getToolkit().getScreenSize().height * 3 / 4); // 设定窗体的大小
}

private void showTime()
{
    int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
    int minute = Calendar.getInstance().get(Calendar.MINUTE);
    int second = Calendar.getInstance().get(Calendar.SECOND);
    StringBuilder sb = new StringBuilder();
    if (hour < 10)
    {
     sb.append("0" + hour);
    } else
    {
     sb.append(hour);
    }
    if (minute < 10)
    {
     sb.append(":0" + minute);
    } else
    {
     sb.append(":" + minute);
    }
    if (second < 10)
    {
     sb.append(":0" + second);





    } else
     sb.append(":" + second);
    label.setFont(new Font("SansSerif", Font.BOLD, 12));
    label.setForeground(Color.BLACK);
    label.setText(sb.toString());
}

class MyTableModel extends AbstractTableModel // 表格模型类,提供表格数据
{
    private static final long serialVersionUID = 1L;
    String[] columnName = new String[]
    // 表格列名
    { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
      "Saturday" };

    @Override
    public int getColumnCount() // 返回表格列数
    {
     return columnName.length;
    }

    public int getRowCount() // 返回表格行数
    {
     return 6;
    }

    public String getColumnName(int col) // 获得表格列名
    {
     return columnName[col];
    }

    public Class<? extends Object> getColumnClass(int c)
    {
     return getValueAt(0, c).getClass();
    }

    public Object getValueAt(int rowIndex, int columnIndex)
    {
     int month = list.getSelectedIndex();// 获得列表的月份
     int year = (Integer) spinner.getValue();// 获得spinner显示的年份
     GregorianCalendar gc = new GregorianCalendar(year, month, 1);
     int dayOfWeek = gc.get(Calendar.DAY_OF_WEEK) - 1; // 获得这个月的第一天是星期几
     int countDayInMonth = 0;// 某月的总天数,这里的月是从0-11,表示1-12月
     if (month == 0 || month == 2 || month == 4 || month == 6
       || month == 7 || month == 9 || month == 11)
     {
      countDayInMonth = 31; // 是31天的月份
     } else if (month == 3 || month == 5 || month == 8 || month == 10)
     {
      countDayInMonth = 30; // 是30天的月份
     } else if (gc.isLeapYear(year) && month == 1)
     {
      countDayInMonth = 29; // 闰年2月
     } else if (!gc.isLeapYear(year) && month == 1)
     {
      countDayInMonth = 28; // 非闰年2月
     }
     String value = ""; // 因为返回值是Object类型,所以不能直接返回int类型的数据,所以转换成String类型
     if (rowIndex == 0) // 第一列的数据
     {
      if (columnIndex < dayOfWeek)





      {
       value = "";
      } else
      {
       value = "" + (columnIndex - dayOfWeek + 1);
      }
     } else
     // 其余列的数据
     {
      if (((rowIndex - 1) * 7) + (columnIndex + 1 + 7 - dayOfWeek) <= countDayInMonth)
       value = (((rowIndex - 1) * 7) + (columnIndex + 1 + 7 - dayOfWeek))
         + "";
     }
     return value;
    }
}

@SuppressWarnings("rawtypes")
class MyListCellRenderer extends JLabel implements ListCellRenderer // 列表元素绘制类继承自JLabel,实现ListCellRenderer接口
{
    private static final long serialVersionUID = 1L;

    public Component getListCellRendererComponent(JList list, // the list
      Object value, // value to display
      int index, // cell index
      boolean isSelected, // is the cell selected
      boolean cellHasFocus) // does the cell have focus
    {
     String s = value.toString().trim(); // 获取选中元素的字符内容
     setText(s); // 显示这个字符
     Dimension dimension = list.getSize(); // 设定这个每个元素的大小
     int height = dimension.height / 12;
     int width = dimension.width;
     setSize(width, height);
     if (isSelected) // 如果该元素被选中,则背景色为红色
     {
      setBackground(Color.red);
      setForeground(list.getSelectionBackground());
     } else
     // 未被选中的元素背景色显示为灰色,前景色为粉红色
     {
      setBackground(Color.gray);
      setForeground(Color.pink);
     }
     setIcon(new ImageIcon("res\\mon.jpg")); // 设定该元素的图标
     setEnabled(list.isEnabled());
     setFont(new Font("SansSerif", Font.PLAIN, 20)); // 设定字体大小
     setOpaque(true);
     return this;
    }
}

class MyTableRenderer extends JLabel implements TableCellRenderer
{
    private static final long serialVersionUID = 1L;

    public Component getTableCellRendererComponent(JTable table,
      Object value, boolean isSelected, boolean hasFocus, int row,
      int column)




    {
     if ((Calendar.getInstance().get(Calendar.DAY_OF_MONTH) + "") // 如果这个标签上的值等于该天的值
       .equals(value)
       && flag == true // 标记为true
       && list.getSelectedIndex() == Calendar.getInstance().get(
         Calendar.MONTH) // 列表显示的月份是该月的值
       && (Integer) spinner.getValue() == Calendar.getInstance()
         .get(Calendar.YEAR))// spinner显示的年份是该年
     {
      setFont(new Font("SansSerif", Font.PLAIN, 40)); // 设定字体大小
      setText((String) value); // 显示日期字符串
      setBorder(UIManager.getBorder("Table.focusCellHighlightBorder"));
      setForeground(Color.RED); // 设定字符串颜色
      flag = false;
     } else
     // 如果不是改日的标签上的值
     {
      setFont(new Font("SansSerif", Font.PLAIN, 40)); // 设定字体大小
      setText((String) value); // 显示标签上的日期字符串
      if (hasFocus) // 如果是被点中的表格的单元格
      {
       setBorder(UIManager
         .getBorder("Table.focusCellHighlightBorder")); // 设定边框
       setForeground(Color.RED);// 设定日期字符串颜色
      } else
      {
       setForeground(null);
       setBorder(null);
      }
     }
     return this;
    }
}
}

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐