想问一下如何在日历系统里插入图片

大二萌新做课设风景日历制作系统,图片不会插入,希望向各位大佬求助

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;

public class MyCalendar extends JApplet {
   //定义星期天到星期六全局变量
    public static final String WEEK_SUN = "星期日";       //星期标签名称
    public static final String WEEK_MON = "星期一";
    public static final String WEEK_TUE = "星期二";
    public static final String WEEK_WED = "星期三";
    public static final String WEEK_THU = "星期四";
    public static final String WEEK_FRI = "星期五";
    public static final String WEEK_SAT = "星期六";

    public static final Color background = Color.white;          //设置背景颜色
    public static final Color foreground = Color.black;          //设置前景颜色
    public static final Color headerBackground = Color.blue;     //设置题头星期的背景颜色
    public static final Color headerForeground = Color.white;    //设置题头星期的前景颜色
    public static final Color selectedBackground = Color.green;   //设置被选中的日期的背景颜色
    public static final Color selectedForeground = Color.white;  //设置被选中的日期的前景颜色

    private JPanel cPane;           //日历面板
    private JLabel yearsLabel;      //"年份"标签
    private JSpinner yearsSpinner;  //年调控,年份组合框
    private JLabel monthsLabel;     //"月份"标签
    private JComboBox monthsComboBox;//12月下拉框
    private JTable daysTable;        //用来显示日期的table,日表格
    private AbstractTableModel daysModel;//天单元表格
    private Calendar calendar;        //日历对象
    //函数定义
    public MyCalendar() {  //构造函数
        cPane = (JPanel) getContentPane();
    }
    public void init() {  //初始化,对所有的空间进行布局,面板界面函数
        cPane.setLayout(new BorderLayout());
                //使用border布局管理器
        calendar = Calendar.getInstance();//默认方式,以本地的时区和地区来构造Calendar
        yearsLabel = new JLabel("年份: ");//设置年份标签显示
        yearsSpinner = new JSpinner();//构造年份spinner组合框
        yearsSpinner.setEditor(new JSpinner.NumberEditor(yearsSpinner, "0000"));
        yearsSpinner.setValue(new Integer(calendar.get(Calendar.YEAR)));
        //增加监听 监听年份的改变
        yearsSpinner.addChangeListener(new ChangeListener() {//注册该组合框的事件监听器
                public void stateChanged(ChangeEvent changeEvent) {
                    int day = calendar.get(Calendar.DAY_OF_MONTH);
                    calendar.set(Calendar.DAY_OF_MONTH, 1);
                    calendar.set(Calendar.YEAR, ((Integer) yearsSpinner.getValue()).intValue());
                    int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
                    calendar.set(Calendar.DAY_OF_MONTH, day > maxDay ? maxDay : day);
                    updateView();//更新显示
                }
            });

        JPanel yearMonthPanel = new JPanel();//定义年月面板
        cPane.add(yearMonthPanel, BorderLayout.NORTH);//添加年月面板到日历面板的南面(最上方)
        yearMonthPanel.setLayout(new BorderLayout());//边布局模式
        yearMonthPanel.add(new JPanel(), BorderLayout.CENTER);
        JPanel yearPanel = new JPanel();//构建年份面板
        yearMonthPanel.add(yearPanel, BorderLayout.WEST);//年份面板添加到年月面板西部(左边)
        yearPanel.setLayout(new BorderLayout());//设置年份面板为边布局并添加年份标签和组合框
        yearPanel.add(yearsLabel, BorderLayout.WEST);
        yearPanel.add(yearsSpinner, BorderLayout.CENTER);

        monthsLabel = new JLabel("月份: "); //设置月份标签显示
        monthsComboBox = new JComboBox(); //向月份下拉框中增加内容
        for (int i = 1; i <= 12; i++) {  //构造下拉框的12个月份
            monthsComboBox.addItem(new Integer(i));
        }
        monthsComboBox.setSelectedIndex(calendar.get(Calendar.MONTH));//下拉框当前月份为选中状态
        monthsComboBox.addActionListener(new ActionListener() { //注册月份下拉框的事件监听器
                public void actionPerformed(ActionEvent actionEvent) {
                    int day = calendar.get(Calendar.DAY_OF_MONTH);
                    calendar.set(Calendar.DAY_OF_MONTH, 1);
                    calendar.set(Calendar.MONTH, monthsComboBox.getSelectedIndex());
                    int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
                    calendar.set(Calendar.DAY_OF_MONTH, day > maxDay ? maxDay : day);
                    updateView();//更新面板显示
                }
            });
        JPanel monthPanel = new JPanel();//定义月份面板
        yearMonthPanel.add(monthPanel, BorderLayout.EAST);//添加月份面板到年月面板的东面(右面)
        monthPanel.setLayout(new BorderLayout());//月份面板设为边布局方式
        monthPanel.add(monthsLabel, BorderLayout.WEST);//添加月份名称标签到月份面板西面(左面)
        monthPanel.add(monthsComboBox, BorderLayout.CENTER);//添加月份下拉框到月份面板中间

        daysModel = new AbstractTableModel() {   //设置7行7列
                public int getRowCount() {        //设置7行
                    return 7;
                }

                public int getColumnCount() {     //设置列7
                    return 7;
                }

                public Object getValueAt(int row, int column) {
                    if (row == 0) { //第一行显示星期
                        return getHeader(column);
                    }
                    row--;
                    Calendar calendar = (Calendar) MyCalendar.this.calendar.clone();
                    calendar.set(Calendar.DAY_OF_MONTH, 1);
                    int dayCount = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
                    int moreDayCount = calendar.get(Calendar.DAY_OF_WEEK) - 1;
                    int index = row * 7 + column;
                    int dayIndex = index - moreDayCount + 1;
                    if (index < moreDayCount || dayIndex > dayCount) {
                        return null;
                    } else {
                        return new Integer(dayIndex);
                    }
                }
            };

        daysTable = new CalendarTable(daysModel, calendar);//构造日表格
        //设置每个call可以被选中
        daysTable.setCellSelectionEnabled(true);//设置表格单元格可选择
        daysTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        daysTable.setDefaultRenderer(daysTable.getColumnClass(0), new TableCellRenderer() {
                public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
                                                               boolean hasFocus, int row, int column) {
                    String text = (value == null) ? "" : value.toString();
                    JLabel cell = new JLabel(text);
                    cell.setOpaque(true);//绘制边界内的所有像素
                    if (row == 0) {//第一行显示星期,设置为星期的前景色和背景色
                        cell.setForeground(headerForeground);
                        cell.setBackground(headerBackground);
                    } else {
                        if (isSelected) {//日期单元格如果选中,则设置为日期选中的前、背景色
                            cell.setForeground(selectedForeground);
                            cell.setBackground(selectedBackground);
                        } else { //设置日期单元格的普通前、背景色
                            cell.setForeground(foreground);
                            cell.setBackground(background);
                        }
                    }

                    return cell;
                }
            });
        updateView();

        cPane.add(daysTable, BorderLayout.CENTER);//添加日面板到日历面板中间
    }

    public static String getHeader(int index) {//设置第一行星期的显示
        switch (index) {
        case 0:
            return WEEK_SUN;
        case 1:
            return WEEK_MON;
        case 2:
            return WEEK_TUE;
        case 3:
            return WEEK_WED;
        case 4:
            return WEEK_THU;
        case 5:
            return WEEK_FRI;
        case 6:
            return WEEK_SAT;
        default:
            return null;
        }
    }

    public void updateView() {//更新面板显示方法
        daysModel.fireTableDataChanged();
        daysTable.setRowSelectionInterval(calendar.get(Calendar.WEEK_OF_MONTH),
                                          calendar.get(Calendar.WEEK_OF_MONTH));
        daysTable.setColumnSelectionInterval(calendar.get(Calendar.DAY_OF_WEEK) - 1,
                                             calendar.get(Calendar.DAY_OF_WEEK) - 1);
    }
    //设置日历的table
    public static class CalendarTable extends JTable {//表格类
        private Calendar calendar;
        public CalendarTable(TableModel model, Calendar calendar) {//构造方法
            super(model);
            this.calendar = calendar;
        }
        public void changeSelection(int row, int column, boolean toggle, boolean extend) {//选择表格单元格时
            super.changeSelection(row, column, toggle, extend);
            if (row == 0) {//选择为第一行(星期)时不改变单元格
                return;
            }
            Object obj = getValueAt(row, column);
            if (obj != null) {
                calendar.set(Calendar.DAY_OF_MONTH, ((Integer)obj).intValue());
            }
        }
    }
 //让applet作为一个可执行的程序来运行
    public static void main(String[] args) {
        JFrame frame = new JFrame("Calendar Application");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        MyCalendar myCalendar = new MyCalendar();
        myCalendar.init();
        frame.getContentPane().add(myCalendar);
        frame.setSize(400, 250);
        frame.show();
    }

}

1个回答

本质上就是在 JFrame 里面添加图片,现在没时间仔细写这个功能,提供一些参考线索:

https://blog.csdn.net/codelife_long/article/details/25488567
https://bbs.csdn.net/topics/190076174?list=20547929

先弄清楚怎么在 JFrame 中添加图片,然后再理清楚 JApplet 和 JFrame 的关系做代码调整。

weixin_45628590
weixin_45628590 具体应该在哪进行代码插入呢?我直接在主函数里进行了添加代码但连日历都无法显示了
9 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐