鱼C论坛

 找回密码
 立即注册
查看: 2655|回复: 3

java 求救--为什么两个类会受到对方的影响

[复制链接]
发表于 2011-12-15 01:07:04 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. import javax.swing.*;

  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.util.Calendar;
  5. import java.util.GregorianCalendar;
  6. //import java.util.*;
  7. class Clock extends JPanel {
  8. int x,y,x0,y0,r,h,olds_x,olds_y,oldm_x,oldm_y,oldh_x,oldh_y,ss,mm,hh,old_m,old_h,ang;
  9. final double RAD = Math.PI/180; //度数转换成弧度的比例
  10. Thread timer = null;

  11. //构造函数创建了一个函数
  12. public Clock() {
  13. int delay = 500;

  14. //创建一个监听事件

  15. ActionListener drawClock = new ActionListener() {

  16. public void actionPerformed(ActionEvent e){
  17. repaint();
  18. }
  19. };





  20. //创建一个时间计算器,每一秒触发一次
  21. new Timer(delay, drawClock).start();
  22. /* timer = new Thread();
  23. while(timer!=null){
  24. try{
  25. Thread.sleep(1000);
  26. }catch(InterruptedException e){}

  27. }*/
  28. }

  29. //实现ActionListener接口必须实现的方法
  30. //public void actionPerformed(ActionEvent evt){}

  31. //绘制图像
  32. public void paint(Graphics g){
  33. Graphics2D g2D = (Graphics2D)g;
  34. Insets insets = this.getInsets();
  35. int L = insets.left/2,T = insets.top/2;
  36. h = this.getSize().height;
  37. //g.setColor(Color.white);

  38. //画圆
  39. g2D.setStroke(new BasicStroke(4.0f));
  40. g.drawOval(L+40, T+40, h-80, h-80);
  41. r = h/2-40;
  42. x0 = 40+r-5+L;
  43. y0 = 40+r-5-T;
  44. ang = 60;

  45. //绘制时钟上的12个字
  46. for (int i = 1; i <= 12; i++) {
  47. x = (int)((r+10)*Math.cos(RAD*ang)+x0);
  48. y = (int)((r+10)*Math.sin(RAD*ang)+y0);
  49. g.drawString(""+i, x,h-y);
  50. ang -= 30;
  51. }

  52. //获得想在的时间
  53. Calendar now = new GregorianCalendar();
  54. int nowh = now.get(Calendar.HOUR_OF_DAY);
  55. int nowm = now.get(Calendar.MINUTE);
  56. int nows = now.get(Calendar.SECOND);
  57. String st;
  58. if(nowh<10) st = "0" + nowh; else st = ""+ nowh;
  59. if(nowm<10) st += ":0" + nowm; else st += ":"+ nowm;
  60. if(nows<10) st += ":0" + nows; else st += ":"+ nows;

  61. //在窗体上显示时间
  62. g.setColor(Color.pink);
  63. g.fillRect(L+40, T, 50, 28);
  64. g.setColor(Color.blue);
  65. g.drawString(st,L+40, T+26);

  66. //计算时间与度数的关系
  67. ss = 90-nows*6;
  68. mm = 90-nowm*6;
  69. hh = 90-nowh*30-nowm/2;
  70. x0 = r+40+L;
  71. y0 = r+40+T;
  72. g2D.setStroke(new BasicStroke(1.2f));

  73. //擦除秒针
  74. if (olds_x>0) {
  75. g.setColor(this.getBackground());
  76. g.drawLine(x0, y0, olds_x, h-olds_y);
  77. }else {
  78. old_m = mm;
  79. old_h = hh;
  80. }

  81. //绘制秒针
  82. x = (int)(r*0.9*Math.cos(RAD*ss))+x0;
  83. y = (int)(r*0.9*Math.sin(RAD*ss))+y0-2*T;
  84. g.setColor(Color.yellow);
  85. g.drawLine(x0, y0, x, h-y);
  86. olds_x = x;
  87. olds_y = y;
  88. g2D.setStroke(new BasicStroke(2.2f));

  89. //擦除分针
  90. if (old_m != mm) {
  91. g.setColor(this.getBackground());
  92. g.drawLine(x0, y0, oldm_x, h-oldm_y);
  93. }

  94. //绘制分针
  95. x = (int)(r*0.7*Math.cos(RAD*mm))+x0;
  96. y = (int)(r*0.7*Math.sin(RAD*mm))+y0-2*T;
  97. g.setColor(Color.green);
  98. g.drawLine(x0, y0, x, h-y);
  99. oldm_x = x;
  100. oldm_y = y;
  101. old_m = mm;
  102. g2D.setStroke(new BasicStroke(3.4f));

  103. //擦除时针
  104. if (old_h != hh) {
  105. g.setColor(this.getBackground());
  106. g.drawLine(x0, y0, oldh_x, h-oldh_y);
  107. }

  108. //绘制时针
  109. x = (int)(r*0.5*Math.cos(RAD*hh))+x0;
  110. y = (int)(r*0.5*Math.sin(RAD*hh))+y0-2*T;
  111. g.setColor(Color.red);
  112. g.drawLine(x0, y0, x, h-y);
  113. oldh_x = x;
  114. oldh_y = y;
  115. old_h = hh;
  116. //g2D.setStroke(new BasicStroke(3.4f));
  117. }
  118. }
复制代码
上面是Clock类,目的是显示一个模拟时钟

下面是一个挂历的类
  1. import java.awt.BorderLayout;
  2. import java.awt.Color;
  3. import java.awt.Component;
  4. import java.awt.GridLayout;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.util.Calendar;

  8. import javax.swing.*;
  9. import javax.swing.event.ChangeEvent;
  10. import javax.swing.event.ChangeListener;
  11. import javax.swing.table.AbstractTableModel;
  12. import javax.swing.table.TableCellRenderer;
  13. import javax.swing.table.TableModel;


  14. class MyCalendar extends JPanel {
  15. public static final String WEEK_SUN = "SUN";
  16. public static final String WEEK_MON = "MON";
  17. public static final String WEEK_TUE = "TUE";
  18. public static final String WEEK_WED = "WED";
  19. public static final String WEEK_THU = "THU";
  20. public static final String WEEK_FRI = "FRI";
  21. public static final String WEEK_SAT = "SAT";

  22. //设置背景色
  23. public static final Color background = new Color(100,100,200);
  24. //设置前景色
  25. public static final Color foreground = new Color(200,200,100);
  26. //设置题头星期的背景色和前景色
  27. public static final Color headerBackground = Color.yellow;
  28. public static final Color headerForeground = Color.green;

  29. private JPanel cPanel;
  30. private JLabel yearsLabel,monthsLabel; //年、月
  31. private JSpinner yearsSpinner; //年调控
  32. private JComboBox monthsComboBox; //月下拉框
  33. private JTable daysTable; //用来显示日期的table
  34. private AbstractTableModel daysModel;
  35. private Calendar calendar;



  36. //初始化,对所有的控件进行布局
  37. public MyCalendar(){
  38. cPanel = new JPanel();
  39. cPanel.setLayout(new BorderLayout());
  40. //cPanel.setBounds(0, 300, 300, 300);
  41. calendar = Calendar.getInstance();
  42. yearsLabel = new JLabel("Year:");
  43. yearsSpinner = new JSpinner();
  44. yearsSpinner.setEditor(new JSpinner.NumberEditor(yearsSpinner, "0000"));
  45. yearsSpinner.setValue(new Integer(calendar.get(Calendar.YEAR)));

  46. //增加监听 监听年份的改变
  47. yearsSpinner.addChangeListener(new ChangeListener() {
  48. public void stateChanged(ChangeEvent arg0) {
  49. int day = calendar.get(Calendar.DAY_OF_MONTH);
  50. calendar.set(Calendar.DAY_OF_MONTH, 1);
  51. calendar.set(Calendar.YEAR, ((Integer)yearsSpinner.getValue()).intValue());
  52. int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
  53. calendar.set(Calendar.DAY_OF_MONTH, (day > maxDay ? maxDay: day));
  54. repaint();
  55. }
  56. });
  57. add(cPanel);
  58. JPanel yearMonthPanel = new JPanel();
  59. cPanel.add(yearMonthPanel,BorderLayout.NORTH);
  60. yearMonthPanel.setLayout(new GridLayout(1, 6));
  61. //yearMonthPanel.add(new JPanel(),BorderLayout.CENTER);
  62. JPanel yearPanel = new JPanel();
  63. yearMonthPanel.add(yearPanel);
  64. yearPanel.add(yearsLabel);
  65. yearPanel.add(yearsSpinner);
  66. //yearPanel.add(new JPanel());

  67. monthsLabel = new JLabel("Month: ");

  68. //向月份下拉框中增加内容
  69. monthsComboBox = new JComboBox();
  70. for (int i = 1; i <= 12; i++) {
  71. monthsComboBox.addItem(new Integer(i));
  72. }
  73. monthsComboBox.setSelectedIndex(calendar.get(Calendar.MONTH));

  74. //监听月份的改变
  75. monthsComboBox.addActionListener(new ActionListener(){

  76. public void actionPerformed(ActionEvent arg0) {
  77. int day = calendar.get(Calendar.DAY_OF_MONTH);
  78. calendar.set(Calendar.DAY_OF_MONTH, 1);
  79. calendar.set(Calendar.MONTH, monthsComboBox.getSelectedIndex());;
  80. int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
  81. calendar.set(Calendar.DAY_OF_MONTH, (day > maxDay ? maxDay: day));
  82. repaint();
  83. }
  84. });
  85. JPanel monthPanel =new JPanel();
  86. yearMonthPanel.add(monthPanel);
  87. monthPanel.setLayout(new BorderLayout());
  88. monthPanel.add(monthsLabel,BorderLayout.WEST);
  89. JPanel monthPane2 =new JPanel();
  90. monthPanel.add(monthPane2,BorderLayout.CENTER);
  91. monthPane2.setLayout(new BorderLayout());
  92. monthPane2.add(monthsComboBox,BorderLayout.WEST );

  93. daysModel = new AbstractTableModel() {
  94. //设置行7
  95. public Object getValueAt(int row, int col) {
  96. if (row == 0) {
  97. return getHeader(col);
  98. }
  99. row -- ;
  100. Calendar calendar = (Calendar)MyCalendar.this.calendar.clone();
  101. calendar.set(Calendar.DAY_OF_MONTH, 1);
  102. int dayCount = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
  103. int moreDayCount = calendar.get(Calendar.DAY_OF_WEEK) - 1;
  104. int index = row * 7 +col;
  105. int dayIndex = index - moreDayCount + 1;

  106. if (index < moreDayCount || dayIndex > dayCount) {
  107. return null;
  108. }else {
  109. return new Integer(dayIndex);
  110. }
  111. }

  112. public int getRowCount() {
  113. return 7;
  114. }

  115. public int getColumnCount() {
  116. return 7;
  117. }
  118. };

  119. daysTable = new CalendarTable(daysModel,calendar);

  120. //设置每一个cell可以被选中
  121. daysTable.setCellSelectionEnabled(true);
  122. daysTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  123. daysTable.setDefaultRenderer(daysTable.getColumnClass(0), new TableCellRenderer() {

  124. public Component getTableCellRendererComponent(JTable table, Object value,
  125. boolean isSelect, boolean hasFocus, int row, int col) {
  126. String text = (value == null) ? "" :value.toString();
  127. JLabel cell = new JLabel(text);
  128. cell.setOpaque(true);
  129. if (row == 0) {
  130. cell.setForeground(headerForeground);
  131. cell.setBackground(headerBackground);
  132. }else {
  133. if (isSelect) {
  134. cell.setForeground(Color.white);
  135. cell.setBackground(Color.black);
  136. }else {
  137. cell.setForeground(foreground);
  138. cell.setBackground(background);
  139. }
  140. }
  141. return cell;
  142. }
  143. });
  144. repaint();
  145. cPanel.add(new JPanel(),BorderLayout.EAST);
  146. cPanel.add(daysTable,BorderLayout.CENTER);
  147. cPanel.add(new JPanel(),BorderLayout.WEST);
  148. this.setVisible(true);
  149. }
  150. public static String getHeader(int index){
  151. switch(index){
  152. case 0:
  153. return WEEK_SUN;
  154. case 1:
  155. return WEEK_MON;
  156. case 2:
  157. return WEEK_TUE;
  158. case 3:
  159. return WEEK_WED;
  160. case 4:
  161. return WEEK_THU;
  162. case 5:
  163. return WEEK_FRI;
  164. case 6:
  165. return WEEK_SAT;
  166. default:
  167. return null;

  168. }
  169. }
  170. public void updateView(){
  171. daysModel.fireTableDataChanged();
  172. daysTable.setRowSelectionInterval(calendar.get(Calendar.WEEK_OF_MONTH), calendar.get(Calendar.WEEK_OF_MONTH));
  173. daysTable.setColumnSelectionInterval(calendar.get(Calendar.DAY_OF_MONTH)-1, calendar.get(Calendar.DAY_OF_MONTH)-1);
  174. }
  175. //设置日历的table
  176. public static class CalendarTable extends JTable{
  177. private Calendar calendar;

  178. public CalendarTable(TableModel model,Calendar calendar){
  179. super(model);
  180. this.calendar = calendar;
  181. }
  182. public void changeSelection(int row,int col,boolean toggle,boolean extend){
  183. super.changeSelection(row, col, toggle, extend);
  184. if (row ==0) {
  185. return;
  186. }
  187. Object obj = this.getValueAt(row, col);
  188. if (obj != null) {
  189. calendar.set(Calendar.DAY_OF_MONTH, ((Integer)obj).intValue());
  190. }
  191. }
  192. }


  193. // public void actionPerformed(ActionEvent arg0) {
  194. //
  195. // }

  196. }
复制代码

下面是住类,界面类  
  1. import java.awt.*;
  2. import java.awt.event.WindowAdapter;
  3. import java.awt.event.WindowEvent;
  4. public class MyText
  5. {
  6. public static void main(String[] args) {
  7. new MyFrame("记事本",120,120,800,600);
  8. }
  9. }
  10. class MyFrame extends Frame{
  11. protected MyCalendar p2;
  12. protected Clock p1;
  13. MyFrame(String s,int x,int y,int w,int h) {
  14. super(s);
  15. MyPanel p4 = new MyPanel(w,h);
  16. p2 = new MyCalendar();
  17. Panel p3 = new Panel(null);
  18. p1 = new Clock();
  19. setSize(w, h);
  20. setLocation(x, y);
  21. p1.setBackground(Color.white);
  22. setLayout(new GridLayout(2,2));
  23. this.addWindowListener(new WindowAdapter() {

  24. public void windowClosing(WindowEvent arg0) {
  25. System.exit(0);
  26. }
  27. });
  28. add(p2);
  29. add(p4);
  30. add(p3);
  31. add(p1);
  32. //this.setResizable(false);
  33. setVisible(true);
  34. }
  35. }
  36. class MyPanel extends TextArea{
  37. MyPanel(int w,int h)
  38. {
  39. setSize(50, 50);
  40. setLocation(300, 300);
  41. setEditable(true);

  42. }
  43. }
复制代码


未命名.jpg
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
 楼主| 发表于 2011-12-15 01:08:20 | 显示全部楼层
发上来就是想问问,感激~~!,睡觉去了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2011-12-15 15:27:54 | 显示全部楼层

回帖奖励 +3 鱼币

今晚有空再看看哈!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
 楼主| 发表于 2011-12-15 16:25:21 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-27 03:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表