1、 JPanel timeArea=new JPanel(); /Internet时间 JPanel internetTime=new JPanel(); /创建属性页tabbedPane JTabbedPane tabbedPane=new JTabbedPane(); tabbedPane.addTab(时间和日期, dt);时区, timeArea);Internet时间, internetTime); tabbedPane.setSelectedIndex(0); /设置默认版位第一版 getContentPane().add(tabbedPane,BorderLayout.CENTER
2、); /把属性页加到frame中去 public static void main(String args) JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame=new main0(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); frame.setSize(400,250);Rili.javaimport java.awt.event.*;import java.util.*;import
3、javax.swing.event.*;import javax.swing.table.*;public class rili extends JApplet /*定义全局变量*/ public static final Color background = Color.white;/背景色 public static final Color foreground = Color.black;/前景色 public static final Color headerBackground = Color.DARK_GRAY;/星期 public static final Color heade
4、rForeground = Color.white;/星期前景色 public static final Color selectedBackground = Color.blue;/选中背景色 public static final Color selectedForeground = Color.white;/选中前景色 public static final String WeekSun = 日; /星期标签名称 public static final String WeekMon = 一 public static final String WeekTue = 二 public sta
5、tic final String WeekWed = 三 public static final String WeekThu = 四 public static final String WeekFri = 五 public static final String WeekSat = 六 private JPanel MainPanel;/日历面板 private JLabel yearsLabel;/“年份” 标签 private JSpinner yearsSpinner;/年份组合框 private JLabel monthsLabel;/“月份” 标签 private JComboB
6、ox monthsComboBox; /12月份下拉框 private JLabel InfoLabel;/信息显示标签 private JTable daysTable; /日表格 private AbstractTableModel daysModel;/天单元表格private Calendar calendar;/日历对象/*函数定义*/ public rili() /构造函数 MainPanel = (JPanel) getContentPane(); public void init() /初始化面板界面函数 MainPanel.setLayout(new BorderLayout
7、(); 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.addCha
8、ngeListener(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(Cal
9、endar.DAY_OF_MONTH); calendar.set(Calendar.DAY_OF_MONTH, day maxDay ? maxDay : day); updateView(); /更新显示 ); /- monthsComboBox = new JComboBox(); /月份下拉框 monthsLabel = new JLabel(月 /设置月份标签显示 for (int i = 1; i = 12; i+) /构造下拉框的12个月份 monthsComboBox.addItem(new Integer(i); monthsComboBox.setSelectedIndex
10、(calendar.get(Calendar.MONTH);/下拉框当前月份为选中状态 monthsComboBox.addActionListener(new ActionListener() /注册月份下拉框的事件监听器 public void actionPerformed(ActionEvent actionEvent) calendar.set(Calendar.MONTH, monthsComboBox.getSelectedIndex();/更新面板显示 ); InfoLabel=new JLabel(当前时区:中国标准时间 JPanel yearMonthPanel = new
11、 JPanel(); /定义年月面板 MainPanel.add(yearMonthPanel, BorderLayout.NORTH);/添加年月面板到日历面板的南面(最上方) MainPanel.add(InfoLabel, BorderLayout.SOUTH); yearMonthPanel.setLayout(new BorderLayout();/边布局模式 JPanel yearPanel = new JPanel(); /构建年份面板 yearMonthPanel.add(yearPanel, BorderLayout.WEST);/年份面板添加到年月面板西部(左边) year
12、Panel.setLayout(new BorderLayout();/设置年份面板为边布局并添加年份标签和组合框 yearPanel.add(yearsLabel, BorderLayout.EAST); yearPanel.add(yearsSpinner, BorderLayout.CENTER); /- JPanel monthPanel = new JPanel();/定义月份面板 yearMonthPanel.add(monthPanel, BorderLayout.EAST);/添加月份面板到年月面板的东面(右边) monthPanel.setLayout(new BorderL
13、ayout();/月份面板设为边布局方式 monthPanel.add(monthsLabel, BorderLayout.EAST);/添加月份名称标签到月份面板西面(左边) monthPanel.add(monthsComboBox, BorderLayout.CENTER);/添加月份下拉框到月份面板中间 JPanel txetPanel = new JPanel();/定义标题文本显示面板 yearMonthPanel.add(txetPanel,BorderLayout.CENTER);/添加标题文本显示面板到年月面板中间 daysModel = new AbstractTableM
14、odel() /设置7行7列 public int getRowCount() return 7; public int getColumnCount() public Object getValueAt(int row, int column) if (row = 0) /第一行显示星期 return getHeader(column); row-; Calendar calendar = (Calendar) rili.this.calendar.clone(); int dayCount = calendar.getActualMaximum(Calendar.DAY_OF_MONTH)
15、; int moreDayCount = calendar.get(Calendar.DAY_OF_WEEK) - 1; int index = row * 7 + column; int dayIndex = index - moreDayCount + 1; if (index dayCount) return null; else return new Integer(dayIndex); ; daysTable = new CalendarTable(daysModel, calendar); /构造日表格 daysTable.setCellSelectionEnabled(true)
16、;/设置表格单元格可选择 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
17、 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(selectedFor
18、eground); cell.setBackground(selectedBackground); else /设置日期单元格的普通前、背景色 cell.setForeground(foreground); cell.setBackground(background); return cell; MainPanel.add(daysTable, BorderLayout.CENTER);/添加日面板到日历面板中间/- public static String getHeader(int index) /设置第一行星期的显示 switch (index) case 0: return WeekS
19、un; case 1: return WeekMon; case 2: return WeekTue; case 3: return WeekWed; case 4: return WeekThu; case 5: return WeekFri; case 6: return WeekSat; default: public void updateView() /更新面板显示方法 daysModel.fireTableDataChanged();daysTable.setRowSelectionInterval(calendar.get(Calendar.WEEK_OF_MONTH), cal
20、endar.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) /构造方法 supe
21、r(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_
22、OF_MONTH, (Integer)obj).intValue();Clock.javaimport java.text.*;public class Clock extends JPanel implements Runnable private JLabel jl;private DateFormat df;public Clock() jl=new JLabel(); jl.setHorizontalAlignment(JLabel.CENTER); df=DateFormat.getDateTimeInstance(); new Thread(this).start(); this.
23、setLayout(new BorderLayout(); this.add(jl,BorderLayout.SOUTH); public void run() while(true) try Thread.sleep(1000); catch(InterruptedException ie) ie.printStackTrace(); jl.setText(df.format(new Date(); repaint();public void paintComponent(Graphics g) super.paintComponent(g); Calendar cal=Calendar.g
24、etInstance(); /得到当前的时间信息 int hour=cal.get(Calendar.HOUR); int minute=cal.get(Calendar.MINUTE); int second=cal.get(Calendar.SECOND); /得到当前的面板的大小信息 int width=this.getWidth(); int height=this.getHeight(); /钟的那个圆盘取两个之中小的那个 int small=widthheight?width:height; int diameter=(int)(small*0.8); int radius=dia
25、meter/2; /确定中心点 Point center=new Point(width/2,height/2); /确定时针,分钟,秒针的长度 int secondLength=(int)(radius*0.8); int minuteLength=(int)(secondLength*0.8); int hourLength=(int)(minuteLength*0.8); /确定时针,分针,秒针的另一端坐标 int secondX=center.x+(int)(secondLength*Math.sin(second*2*Math.PI/60.0); int secondY=center
26、.y-(int)(secondLength*Math.cos(second*2*Math.PI/60.0); int minuteX=center.x+(int)(minuteLength*Math.sin(minute*2*Math.PI/60.0); int minuteY=center.y-(int)(minuteLength*Math.cos(minute*2*Math.PI/60.0); int hourX=center.x+(int)(hourLength*Math.sin(minute/60.0+hour)*Math.PI/6.0); int hourY=center.y-(int)(hourLength*Math.cos(minute/60.0+hour)*Math.PI/6.0); Graphics2D g2d=(Graphics2D)g; /画表盘和刻度 g.drawOval(center.x-radius,center.y-radius,dia
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1