帮忙注释下下面这段java各行的意思,完全看不懂

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了帮忙注释下下面这段java各行的意思,完全看不懂相关的知识,希望对你有一定的参考价值。

package lipei;

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

/**********
* 该类继承自JApplet,可作为一个applet程序运行于web客户端*/
public class MyCalendar extends JApplet

//定义一些字符串常量,用来表示周日至周六
public static final String WEEK_SUN = "SUN";
public static final String WEEK_MON = "MON";
public static final String WEEK_TUE = "TUE";
public static final String WEEK_WED = "WED";
public static final String WEEK_THU = "THU";
public static final String WEEK_FRI = "FRI";
public static final String WEEK_SAT = "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.blue;
public static final Color selectedForeground = Color.white;

//定义一些成员变量,表示图形用户界面的各个主要部件
private JPanel cPane;
private JLabel yearsLabel;
private JSpinner yearsSpinner;
private JLabel monthsLabel;
private JComboBox monthsComboBox;
private JTable daysTable;
private AbstractTableModel daysModel;
private Calendar calendar;

//默认无参构造函数,在该类被载入时执行,对cPane进行初始化。
public MyCalendar()
cPane = (JPanel) getContentPane();
//获取该applet的工作界面,用以初始化cPane,cPane引用了该APPLET的工作界面。


//init()初始化方法,处于applet生命周期的第一步,在该applet被载入时,由applet容器自动调用。
public void init()
cPane.setLayout(new BorderLayout());//设置cPane的布局管理器为BorderLayout

calendar = Calendar.getInstance(); //获得日期类的实例,具体可参看API文档
yearsLabel = new JLabel("Year: "); //一个label,显示Year:
yearsSpinner = new JSpinner(); //一个JSpinner,就是带上下箭头选择年份的那个
//一下都是对这个年份选择器的定义,包括取值范围、跟月份的联动等。
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();

);

//以下为一个Panel,就是最上方显示年月的那一条。。
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("Month: ");
monthsComboBox = new JComboBox();
for (int i = 1; i <= 12; i++)
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);

/****
* 以下这部分比较复杂,是实现了一个匿名内部类,用以做日期表的数据模型
* 关于这一部分内容,请参看JTable类以及TableModel等相关类的类文档
* JTable是swing中比较难掌握,也是很强大的一个组件*/
daysModel = new AbstractTableModel()
public int getRowCount()
return 7;


public int getColumnCount()
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);


;

/*****
* 以下部分是日期表JTable的各项设置,具体去看API文档吧,在这儿不好解释*/
daysTable = new CalendarTable(daysModel, calendar);
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();

//将日期表添加到该Applet的工作界面中
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);

/*
*该类继承了JTable类,使其更适合用来显示日期表
* */
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());




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(240, 172);
frame.show();


参考技术A 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 = "SUN";
public static final String WEEK_MON = "MON";
public static final String WEEK_TUE = "TUE";
public static final String WEEK_WED = "WED";
public static final String WEEK_THU = "THU";
public static final String WEEK_FRI = "FRI";
public static final String WEEK_SAT = "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.blue;
public static final Color selectedForeground = Color.white;

private JPanel cPane;
private JLabel yearsLabel;
private JSpinner yearsSpinner;
private JLabel monthsLabel;
private JComboBox monthsComboBox;
private JTable daysTable;
private AbstractTableModel daysModel;
private Calendar calendar;

public MyCalendar()
cPane = (JPanel) getContentPane();


public void init()
cPane.setLayout(new BorderLayout());

calendar = Calendar.getInstance();
calendar = Calendar.getInstance();
yearsLabel = new JLabel("Year: ");
yearsSpinner = new JSpinner();
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("Month: ");
monthsComboBox = new JComboBox();
for (int i = 1; i <= 12; i++)
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()
public int getRowCount()
return 7;


public int getColumnCount()
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);
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);

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());




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(240, 172);
frame.show();



这里这里!!!
参考技术B 怎么没看到代码追问

太多了,我把它发给你吧

以上是关于帮忙注释下下面这段java各行的意思,完全看不懂的主要内容,如果未能解决你的问题,请参考以下文章

这段语句看不懂,可以帮我注释一下吗?

帮忙看下这个带参的宏定义,看不懂?

以下一段java程序,看不懂,求加下注释,讲解下,同时给下运行结果(原题是那样要求的)

c语言,break和continue语句.两个题 我看不懂 帮忙解析下吧 教科书上就写了例子 不解析。啥跳不跳出 跳哪

delphi 高手帮忙解释下。详细解释下。觉得哪里是菜鸟看不懂的 就解释下。

一段程序看不懂,高手帮忙读一下