⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ljq.java

📁 平时的小程序很好的
💻 JAVA
字号:
import java.awt.*; 
import java.applet.*; import java.util.*;
import java.util.Date; import java.awt.geom.*;
import java.text.*; import java.awt.event.*;
 class rl extends Frame 
{ 
Button days[]=new Button[49];//构造日期显示按钮并分配内存
Choice Month=new Choice();//构造月份选择控件并分配内存
Choice Year=new Choice();//构造年份选择控件并分配内存
Label lmonth=new Label("MONTH");//构造月份标签并分配内存
Label lyear=new Label("Year");//构造年份标签并分配内存
Label ltext=new Label("YEAR UPTO:::");//构造年份输入标签并分配内存
Panel p1,p2;//构造两个面板
GregorianCalendar gc=new GregorianCalendar();//分配内存
int totdays; 
TextField textfield=new TextField(2);//分配内存 
 rl() 
{ super("日历");//设置窗口名字
	 setBackground(Color.yellow);//设置窗口低色
setTitle("This is a calender");//设置窗口标题
setSize(400,400);//设置窗口为400*400的像素
setResizable(false);//设置窗口为不可调整大小
setLocation(50,50);//设置窗口位置
p1=new Panel(new FlowLayout());//设置面板1布局为FlowLayout类并分配内存 
p2=new Panel(new GridLayout(7,7,10,10));//设置面板2布局为GridLayout类并分配内存 
p1.setBackground(Color.red);//设置背景色
p2.setBackground(Color.cyan);//设置背景色
add(p1,BorderLayout.NORTH);//添加面板
add(p2); 
p1.add(ltext);//添加面板组件
p1.add(textfield); 
p1.add(lmonth); 
p1.add(Month); 
Month.add("JAN");//在选择控件添加月份组件
Month.add("FEB"); 
Month.add("MAR"); 
Month.add("APR"); 
Month.add("MAY"); 
Month.add("JUN"); 
Month.add("JUL"); 
Month.add("AUG"); 
Month.add("SEP"); 
Month.add("OCT"); 
Month.add("NOV"); 
Month.add("DEC"); 
Month.addItemListener(new myLis(this)); 
// int i1=myAction.newyear; // 
// System.out.println("iiiiiiiiii is :::::::"+i1); // 
textfield.addActionListener(new myAction(this)); // 
p1.add(lyear); 
p1.add(Year); 
Year.add("1990"); 
/* Year.add("1991"); 
Year.add("1992"); 
Year.add("1993"); 
Year.add("1994"); 
Year.add("1995"); */ 
Year.addItemListener(new myLis(this)); 
for(int i=0;i<49;i++) 
{ 
days[i]=new Button(""); days[i].setBackground(Color.green);
} 
for(int c=0;c<49;c++) 
{ 
p2.add(days[c]); 
} 
setVisible(false);//设置窗口为隐藏
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e){setVisible(false); System.exit(0);}});//设置窗口为可关闭
}
//实现年份添加
void setYear(String mynewyear) 
{ 
int h=Integer.parseInt(mynewyear); 
for(int adder=1991;adder<=h;adder++) 
{ 
Year.add(""+adder); 
} }

void setButtons(int myday,int mytotdays) 
{ 
int count=7; 
days[0].setLabel("SUN");
days[1].setLabel("MON"); 
days[2].setLabel("TUE"); 
days[3].setLabel("WED"); 
days[4].setLabel("THUR"); 
days[5].setLabel("FRI"); 
days[6].setLabel("SAT"); 
days[0].setBackground(Color.yellow);
days[1].setBackground(Color.yellow);
days[2].setBackground(Color.yellow);
days[3].setBackground(Color.yellow);
days[4].setBackground(Color.yellow);
days[5].setBackground(Color.yellow);
days[6].setBackground(Color.yellow);
if ( myday>0)//实现日历显示
{ 
int blank= myday; 
for( ;blank>0;blank--,count++) 
{ 
days[count].setLabel(""); 
} 
} 
for(int i=1;i<=mytotdays; i++,count++) 
{ 
days[count].setLabel(""+i); 
} 
for(int j = 1;count < 49; j++,count++) 
{ 
days[count].setLabel(""); 
} } 

void setVal(Date date,int iday,int iselMonth,int iselYear) 
{ 
gc.setTime(date); 
if(iselMonth==0 || iselMonth==2 || iselMonth==4 || iselMonth==6 || iselMonth== 7 ||iselMonth==9 || iselMonth==11) 
{ 
totdays=31; 
setButtons(iday,totdays); 
} 
if(iselMonth==3 || iselMonth==5 || iselMonth==8 || iselMonth==10) 
{ 
totdays=30; 
setButtons(iday,totdays); 
} 
if(gc.isLeapYear(iselYear) && iselMonth==1) 
{ 
totdays=29; 
setButtons(iday,totdays); 
} 
if( !gc.isLeapYear(iselYear) && iselMonth==1) 
{ 
totdays=28; 
setButtons(iday,totdays); 
} } 

class myLis implements ItemListener 
{ 
rl calLis; 
public myLis(rl c) 
{ 
calLis=c; 
} 
public void itemStateChanged(ItemEvent i) 
{ 
int selMonth=calLis.Month.getSelectedIndex(); 
int selYear1=Integer.parseInt(calLis.Year.getSelectedItem()); 
int selYear = selYear1- 1900; 
Date d1 = new Date(selYear,selMonth,1); 
int day = d1.getDay(); 
calLis.setVal(d1,day,selMonth,selYear); 
} } 

static public void main(String args[]) 
{ 
rl c=new rl(); 
} } 

class myAction implements ActionListener 
{ 
rl calAc; 
int newyear; 

public myAction(rl ca) 
{ 
calAc=ca; 
} 
public void actionPerformed(ActionEvent e) 
{ 
String s=calAc.textfield.getText(); 
// newyear=Integer.parseInt(s); 
System.out.println("Year upto::::::::"+s); 
calAc.setYear(s); 
TextField tf = (TextField)e.getSource(); 
tf.removeActionListener(this); 
} } 

class jsq extends Frame implements ActionListener
{
//构造一个面板用来设置计算器功能按钮
class Panela extends Panel
{ 
 Button b1;Button b2;Button b3;Button b4;
 Panela()
{ setLayout(new GridLayout(4,4));
b1=new Button("加");b2=new Button("减");
b3=new Button("乘");b4=new Button("除");
b1.setBackground(Color.magenta);b2.setBackground(Color.magenta);
b3.setBackground(Color.magenta);b4.setBackground(Color.magenta);
add(b1);add(b2);add(b3);add(b4); }
}
 //构造一个面板用来输入计算数据
 class Panelc extends Panel
 {
 TextField text1,text2;
 Panelc()
 { setLayout(new GridLayout(1,4));setBackground(Color.red);
   text1=new TextField();text2=new TextField();
   add(new Label("输入数A"));add(text1);add(new Label("输入数B"));add(text2); }
 }
 //构造一个面板显示计算结果
 class Paneld extends Panel
 {
  TextField text;
  Paneld()
 { setLayout(new GridLayout(1,1));
   text=new TextField();
   add(text); }
 }
 Panela A;Panelc C;Paneld D;int n=0,m=0,a,b;
  jsq(int r,int t)
 {
  super("计数器");//设置窗口名字
  //实现窗口布局
  setLayout(null);setSize(r,t);setBackground(Color.yellow);
  A=new Panela();C=new Panelc();D=new Paneld();
  add(C);add(A);add(D);
  a=getSize().width;b=getSize().height;
  n=a;m=b/8;
  C.setBounds(0,m,n,m);D.setBounds(0,2*m,n,m);A.setBounds(0,3*m,n,4*m);
  //将主类的实例作为b1.b2.b3.b4的监视器
  A.b1.addActionListener(this);A.b2.addActionListener(this);
  A.b3.addActionListener(this);A.b4.addActionListener(this);
   setVisible(false);//设置窗口为隐藏
   //设置窗口为可关闭
   addWindowListener(new WindowAdapter()
	 {public void windowClosing(WindowEvent e)
		{setVisible(false); System.exit(0);}});
 }
 public void actionPerformed(ActionEvent e)//实现接口
 {
  double a,b,c;
  if(e.getSource()==A.b1)
  {a=Double.valueOf(C.text1.getText()).doubleValue();
  b=Double.valueOf(C.text2.getText()).doubleValue();
  c=a+b;D.text.setText(String.valueOf(c));} 
 
  else if(e.getSource()==A.b2)
  {a=Double.valueOf(C.text1.getText()).doubleValue();
  b=Double.valueOf(C.text2.getText()).doubleValue();
  c=a-b;D.text.setText(String.valueOf(c));} 
  
  else if(e.getSource()==A.b3)
  {a=Double.valueOf(C.text1.getText()).doubleValue();
  b=Double.valueOf(C.text2.getText()).doubleValue();
  c=a*b;D.text.setText(String.valueOf(c));}
  
  else if(e.getSource()==A.b4)
  {a=Double.valueOf(C.text1.getText()).doubleValue();
  b=Double.valueOf(C.text2.getText()).doubleValue();
  c=a/b;D.text.setText(String.valueOf(c));} 
 }
 }

 public class ljq extends Applet implements ActionListener,Runnable
{ Button b1,b2;jsq window1;rl window2;
int x=0,y=0,w,h;
Date timenow; 
Clock myClock; 
Thread clockthread = null; 
//初始化Applet 
public void init() 
{ setLayout(null);//设置布局
setBackground(Color.yellow);//设置背景色
window1=new jsq(200,200);//为对象分配内存并设置大小
window2=new rl();//为对象分配内存
b1=new Button("计数器");b2=new Button("日历");//对按钮命名并分配内存
b1.setBackground(Color.green);b2.setBackground(Color.green);//设置按钮背景色
b1.addActionListener(this);b2.addActionListener(this);//向按钮添加动作监视器
add(b1);add(b2);
w=getSize().width;h=getSize().height;//实现整体布局
x=w/3;y=h/16;
b1.setBounds(0,y,x,y);b2.setBounds(2*x,y,x,y);
} 
public void actionPerformed(ActionEvent e)//接口方法的实现
	{if(e.getSource()==b1)
		{window1.setVisible(true);}
	else if(e.getSource()==b2)
		{window2.setVisible(true);}}

//启动线程 
public void start() 
{ //
if(clockthread == null) 
{ 
clockthread = new Thread(this,"clockthread"); 
clockthread.start(); 
} 
} 

//终止线程 
public void stop() 
{ 
clockthread.stop(); 
clockthread = null; 
} 

//线程体 
public void run() 
{ 
Thread current = Thread.currentThread(); 
while(clockthread == current) 
{ 

try 
{ 
Thread.sleep(1000);//刷新间隔为1000毫秒 
} 
catch(InterruptedException e) 
{} 
repaint(); 
} 
} 

public void paint(Graphics g) 
{ 
timenow = new Date(); 
myClock = new Clock(timenow.getHours(),timenow.getMinutes(),timenow.getSeconds()); 
//条形时钟面板 
g.drawString(timenow.toString(),25,400); 
SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss a"); 
String msg = df.format(timenow); 
g.clearRect(11,9,199,13); 
g.drawRect(10,8,200,14); 
g.drawString(msg,20,20); 

//圆形时钟面板 
myClock.show(g,100,200,100); 
} 
} 
class Clock 
{ 
int hour,minute,second; 

Clock(int hrs, int min, int sec)//取时间 
{ 
hour = hrs%12; 
minute = min; 
second = sec; 
} 

void show(Graphics g,int cx,int cy,int rad)//显示圆形时钟 
{ 
int hrs_len = (int)(rad*0.5),min_len = (int)(rad*0.6),sec_len = (int)(rad*0.9); 
double theta; 
int i; 
//画时钟面板 
g.drawOval(cx-rad,cy-rad,rad*2,rad*2); 
for(i=1;i<=12;i++) 
{ 
theta = (double)(i)/12*2.0*Math.PI; 
int tx = (int)(cx + sec_len*Math.sin(theta)); 
int ty = (int)(cy - sec_len*Math.cos(theta)); 
g.drawString(""+i,tx,ty); 
} 

//画时、分、秒针 
theta = (double)(hour*60*60 + minute*60 + second)/(12*3600.0)*2.0*Math.PI; 
drawNiddle(g,Color.blue,cx,cy,hrs_len,theta); 
theta = (double)(minute*60 + second)/3600.0*2.0*Math.PI; 
drawNiddle(g,Color.red,cx,cy,min_len,theta); 
theta = (double)(second)/60.0*2.0*Math.PI; 
drawNiddle(g,Color.green,cx,cy,sec_len,theta); 
} 

//表针函数 
private void drawNiddle(Graphics g,Color c,int x,int y, int len, double theta) 
{ 
int ex = (int)(x + len*Math.sin(theta)); 
int ey = (int)(y - len*Math.cos(theta)); 
g.setColor(c); 
g.drawLine(x,y,ex,ey); 
} 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -