package com.charlinda.deskclock.view;import javax.swing.*;
import com.charlinda.deskclock.model.Month;
import com.charlinda.deskclock.model.Year;
import java.awt.*;
import java.awt.event.*;
import java.util.*;public class CalendarFrame extends JFrame implements MouseListener,
ActionListener {
/**
 * @author 陈巧林
 * @description 日历界面
 * @time 2011.09.12
 * @fileName CalendarFrame.java
 */
private static final long serialVersionUID = 1L; // 已上定义的变量为CalendarFrame的变量,可用于整个类当中;
private int year, month, day; // 用于存储当前的时间; private JTextField showDay[]; // 日历中每天号数显示的区域; private JLabel title[]; // 用于日历中星期的显示; private Calendar calendar; // 日历对象; private int day_of_the_week; // 表示具体的星期几; private Month showMonth; // 显示月份的区域的组件对象(继承了Box) private Year showYear; // 显示年份的区域的组件对象(继承了Box) private String weekDay[] = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五",
"星期六" }; /**
 * 构造方法做初始化工作 
 * @param year,month,day
 */
public CalendarFrame(int year, int month, int day) {
//设置本窗口性质;
setVisible(true); // 可见;
setBounds(100, 50, 337, 519); // 显示位置,窗口大小;
setBackground(new Color(128, 128, 255));
validate();
setTitle("万年历");// 设置标题; calendar = Calendar.getInstance(); // 实例化日历对象; //创建几个Jpanel容器
JPanel wholePanel = new JPanel();// wholePanel 为窗口左半部分;
JPanel north = new JPanel();// north为窗口的wholePanel的上部分,用于显示年月及相关按钮;
JPanel center = new JPanel();// center为窗口的wholePanel的中间部分,用于显示日历;
GridLayout gb = new GridLayout(7, 7);// 把这块区域分成7行7列; center.setLayout(gb);//设置center的布局方式为网格布局 // 对传递给构造函数的参数值赋给类中的变量;
this.year = year;
this.month = month;
this.day = day; // 创建showYear,showMonth对象,并传递时间值给他们;
showYear = new Year(this);
showYear.setYear(year);
showMonth = new Month(this);
showMonth.setMonth(month); // 把星期几的标题栏设置好;
title = new JLabel[7];// 创建JLabel,用于显示星期
for (int j = 0; j < 7; j++) {
title[j] = new JLabel();
title[j].setText(weekDay[j]);
title[j].setBorder(BorderFactory.createRaisedBevelBorder()); // 突出格式;
center.add(title[j]);
}
// 周六周日红色表示
title[0].setForeground(Color.red);
title[6].setForeground(Color.red); // 把showDay一块块加上去;
showDay = new JTextField[42]; // 日历中用来显示日期号数的区域;
for (int i = 0; i < 42; i++) {
showDay[i] = new JTextField();
showDay[i].addMouseListener(this); // 注册监听器;
showDay[i].setEditable(false); // 不可编辑;
center.add(showDay[i]); // 分好7*7块之后一块一块加上的;
}
// 在north添加组件;
Box box = Box.createHorizontalBox(); // 新建一个Box对象,box能沿着指定坐标轴显示;
box.add(showYear);
box.add(showMonth);
north.add(box);
/*north.add(showYear);
 north.add(showMonth);*/ // 对wholePanel添加组件,格式设置;
wholePanel.setLayout(new BorderLayout());
wholePanel.add(north, BorderLayout.NORTH);
wholePanel.add(center, BorderLayout.CENTER);
JTextArea txt = new JTextArea();
txt.setLineWrap(true);
txt.setText("请在输入框输入所查年份(负数表示公元前)和月份,并回车确定");
wholePanel.add(txt, BorderLayout.SOUTH);
wholePanel.validate();// 确保组件具有有效的布局; // 对整个JFrame添加组件,格式设置;
Container con = this.getContentPane();
con.add(wholePanel);
con.validate(); // 添加、设置菜单;
JMenuBar menuBar = new JMenuBar();//创建菜单条
//创建菜单
JMenu menusetting = new JMenu();
JMenu about = new JMenu();
//创建菜单项
JMenuItem ringbell = new JMenuItem();
JMenuItem set = new JMenuItem();
JMenuItem aboutthis = new JMenuItem();
//菜单项属性设置
menusetting.setText("功能设置");
set.setText("全局设置");
about.setText("关于");
ringbell.setText("添加闹钟");
aboutthis.setText("关于日历闹钟"); // 添加菜单条
menusetting.add(set);
menusetting.add(ringbell);
about.add(aboutthis);
//添加菜单项
menuBar.add(menusetting);
menuBar.add(about);
menuBar.setVisible(true);
//在窗体上添加菜单条
this.setJMenuBar(menuBar); // 注册监听器
ringbell.addActionListener(this);
set.addActionListener(this);
aboutthis.addActionListener(this); this.setCalendar(year, month);// 设置日历时间为传递进来的时间; // 窗口事件的侦听器;
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});// 关闭窗口; }// 构造方法结束; /**
 * 以下为本类的函数方法:
 */ // 此方法用于数i月份有几周(只横跨了几周,最小为5,最大为6)
/* public int countweek(int i, int day_of_the_week) {
 int daynum = 0;  // 不同月份的处理;
 if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10
 || i == 12) {
 daynum = 31;
 } else if (i == 4 || i == 6 || i == 9 || i == 11) {
 daynum = 30;
 } else if (i == 2) {
 if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
 daynum = 29;
 } else {
 daynum = 28;
 }
 }// 对月份天数处理好了;  if (daynum + day_of_the_week > 35)// 本月有6周;
 {
 return 6;
 } else
 return 5;
 } // countweek()方法结束;
 */ /**
 * @description 此方法用于设置日历牌,
 *              但只是起到对不同月份的分类而已,
 *              通过调用sortDay()方法,
 *              把每个格要显示几号的任务交给调用方法;
 * @time 2011.09.12
 * @param year,month
 */
public void setCalendar(int year, int month) {
calendar.set(year, month - 1, 1);// 注意0表示一月份;
day_of_the_week = calendar.get(Calendar.DAY_OF_WEEK) - 1;
// 对不同月份处理;
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8
|| month == 10 || month == 12) {
sortDay(day_of_the_week, 31); // 调用sortDay方法;
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
sortDay(day_of_the_week, 30);
} else if (month == 2) {
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
sortDay(day_of_the_week, 29);
} else {
sortDay(day_of_the_week, 28);
}
} } /**
 * @description 此方法供setCalendar()调用,用来设置每个格要显示几号;
 * @time 2011.09.12
 * @param day_of_the_week,days
 */
public void sortDay(int day_of_the_week, int days) {
for (int i = day_of_the_week, n = 1; i < day_of_the_week + days; i++) // i=3,星期三;
{
showDay[i].setText("" + n); if ((Calendar.getInstance().get(Calendar.YEAR) == year)
&& (Calendar.getInstance().get(Calendar.MONTH) + 1 == month)
&& (n == day)) // 如果是日期刚好是今天;
{ showDay[i].setForeground(Color.cyan);// 青色表示;
showDay[i].setFont(new Font("TimesRoman", Font.BOLD, 25)); // 大号字体表示;
} else// 其他时间颜色为黑;
{
showDay[i].setFont(new Font("TimesRoman", Font.BOLD, 12));
showDay[i].setForeground(Color.black);
}
if (i % 7 == 6 || i % 7 == 0)// 周六周日设置为红色;
{
showDay[i].setForeground(Color.red);
}
n++;
}
// 本月第一天之前的格子置为空;
for (int i = 0; i < day_of_the_week; i++) {
showDay[i].setText("");
}
// 本月最后一天后的格子置为空;
for (int i = day_of_the_week + days; i < 42; i++) {
showDay[i].setText("");
} } // 得到年;
public int getYear() {
return year;
} // 设置年;
public void setYear(int y) {
year = y;
showYear.setYear(year);// 设置年时显示年的地方相应改变;
} // 得到月份;
public int getMonth() {
return month; } // 设置月份;
public void setMonth(int m) {
month = m;
showMonth.setMonth(month); // 设置月份时显示月份的地方相应改变
} // 得到号数;
public int getDay() {
return day;
} // 设置号数;
public void setDay(int d) {
day = d; } // 增加鼠标监听器,按住鼠标的响应,为读取日志、写日志功能服务;
public void mousePressed(MouseEvent e) // 增加鼠标按钮的触发;
{
JTextField source = (JTextField) e.getSource();
try {
day = Integer.parseInt(source.getText()); } catch (Exception ee) {
}
} // 事件监听器;
public void actionPerformed(ActionEvent e) {
// 如果点击菜单里"设置闹钟时间";
if (e.getActionCommand().equals("添加闹钟")) {
//跳转到添加闹钟的页面
try {
CreateAlarmDialog dialog = new CreateAlarmDialog();
dialog.setVisible(true);
} catch (Exception ee) {
ee.printStackTrace();
}
}
// 点击“关于”时的显示内容;
if (e.getActionCommand().equals("关于日历闹钟")) {
//用Dialog来实现
try {
IntroductionDialog dialog = new IntroductionDialog();
dialog.setVisible(true);
} catch (Exception ex) {
ex.printStackTrace();
}
}
} public void mouseClicked(MouseEvent e) {
} public void mouseReleased(MouseEvent e) {
} public void mouseEntered(MouseEvent e) {
} public void mouseExited(MouseEvent e) {
}}
 运行出来菜单无法 显示 请高手帮忙

解决方案 »

  1.   

    setVisible(true); // 可见;这句,放到构造方法的最后一行
      

  2.   

    一般是在所有UI布局结束后再setVisible,可以放在构造方法最后,也可以在创建对象后调用,,,
      

  3.   

    setToolTipText 只能存纯文本数据 而且不能数据进行设置风格等 不知道还其它功能多一点儿的控件或什么的 能完成我的要求 在网上搜了我就只看到这一种方法
      

  4.   

    继承Swing 组件,override 从 JComponent 继承的   public javax.swing.JToolTip createToolTip()方法,返回一个自定义的 JToolTip 子类。
      

  5.   

    并非纯文本
    JToolTip 是支持 html 格式的
    setToolTipText("<html>  </html>");
    这样就可以定义各种风格的文本了