清/wx风 发表于 2011-12-15 01:07:04

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

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.util.Calendar;
import java.util.GregorianCalendar;
//import java.util.*;
class Clock extends JPanel {
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;
final double RAD = Math.PI/180; //度数转换成弧度的比例
Thread timer = null;

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

//创建一个监听事件

ActionListener drawClock = new ActionListener() {

public void actionPerformed(ActionEvent e){
repaint();
}
};





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

}*/
}

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

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

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

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

//获得想在的时间
Calendar now = new GregorianCalendar();
int nowh = now.get(Calendar.HOUR_OF_DAY);
int nowm = now.get(Calendar.MINUTE);
int nows = now.get(Calendar.SECOND);
String st;
if(nowh<10) st = "0" + nowh; else st = ""+ nowh;
if(nowm<10) st += ":0" + nowm; else st += ":"+ nowm;
if(nows<10) st += ":0" + nows; else st += ":"+ nows;

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

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

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

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

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

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

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

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

下面是一个挂历的类
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;


class MyCalendar extends JPanel {
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 = new Color(100,100,200);
//设置前景色
public static final Color foreground = new Color(200,200,100);
//设置题头星期的背景色和前景色
public static final Color headerBackground = Color.yellow;
public static final Color headerForeground = Color.green;

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



//初始化,对所有的控件进行布局
public MyCalendar(){
cPanel = new JPanel();
cPanel.setLayout(new BorderLayout());
//cPanel.setBounds(0, 300, 300, 300);
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 arg0) {
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));
repaint();
}
});
add(cPanel);
JPanel yearMonthPanel = new JPanel();
cPanel.add(yearMonthPanel,BorderLayout.NORTH);
yearMonthPanel.setLayout(new GridLayout(1, 6));
//yearMonthPanel.add(new JPanel(),BorderLayout.CENTER);
JPanel yearPanel = new JPanel();
yearMonthPanel.add(yearPanel);
yearPanel.add(yearsLabel);
yearPanel.add(yearsSpinner);
//yearPanel.add(new JPanel());

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 arg0) {
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));
repaint();
}
});
JPanel monthPanel =new JPanel();
yearMonthPanel.add(monthPanel);
monthPanel.setLayout(new BorderLayout());
monthPanel.add(monthsLabel,BorderLayout.WEST);
JPanel monthPane2 =new JPanel();
monthPanel.add(monthPane2,BorderLayout.CENTER);
monthPane2.setLayout(new BorderLayout());
monthPane2.add(monthsComboBox,BorderLayout.WEST );

daysModel = new AbstractTableModel() {
//设置行7
public Object getValueAt(int row, int col) {
if (row == 0) {
return getHeader(col);
}
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 +col;
int dayIndex = index - moreDayCount + 1;

if (index < moreDayCount || dayIndex > dayCount) {
return null;
}else {
return new Integer(dayIndex);
}
}

public int getRowCount() {
return 7;
}

public int getColumnCount() {
return 7;
}
};

daysTable = new CalendarTable(daysModel,calendar);

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

public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelect, boolean hasFocus, int row, int col) {
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 (isSelect) {
cell.setForeground(Color.white);
cell.setBackground(Color.black);
}else {
cell.setForeground(foreground);
cell.setBackground(background);
}
}
return cell;
}
});
repaint();
cPanel.add(new JPanel(),BorderLayout.EAST);
cPanel.add(daysTable,BorderLayout.CENTER);
cPanel.add(new JPanel(),BorderLayout.WEST);
this.setVisible(true);
}
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_MONTH)-1, calendar.get(Calendar.DAY_OF_MONTH)-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 col,boolean toggle,boolean extend){
super.changeSelection(row, col, toggle, extend);
if (row ==0) {
return;
}
Object obj = this.getValueAt(row, col);
if (obj != null) {
calendar.set(Calendar.DAY_OF_MONTH, ((Integer)obj).intValue());
}
}
}


// public void actionPerformed(ActionEvent arg0) {
//
// }

}

下面是住类,界面类
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class MyText
{
public static void main(String[] args) {
new MyFrame("记事本",120,120,800,600);
}
}
class MyFrame extends Frame{
protected MyCalendar p2;
protected Clock p1;
MyFrame(String s,int x,int y,int w,int h) {
super(s);
MyPanel p4 = new MyPanel(w,h);
p2 = new MyCalendar();
Panel p3 = new Panel(null);
p1 = new Clock();
setSize(w, h);
setLocation(x, y);
p1.setBackground(Color.white);
setLayout(new GridLayout(2,2));
this.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent arg0) {
System.exit(0);
}
});
add(p2);
add(p4);
add(p3);
add(p1);
//this.setResizable(false);
setVisible(true);
}
}
class MyPanel extends TextArea{
MyPanel(int w,int h)
{
setSize(50, 50);
setLocation(300, 300);
setEditable(true);

}
}


清/wx风 发表于 2011-12-15 01:08:20

发上来就是想问问,感激~~!,睡觉去了

zhuangyanxiang 发表于 2011-12-15 15:27:54

今晚有空再看看哈!

清/wx风 发表于 2011-12-15 16:25:21

zhuangyanxiang 发表于 2011-12-15 15:27 static/image/common/back.gif
今晚有空再看看哈!

谢谢~~! 先感激下先~~!
页: [1]
查看完整版本: java 求救--为什么两个类会受到对方的影响