📄 c23.java
字号:
/*<applet code="c23.class" width=300 height=300>
</applet>
*/
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class c23 extends Applet
implements ActionListener,Runnable
{
Button btn1;
Label btn;
TextField t1,t2,t3;
Thread t=null,ttext=null;
String str;
Choice c;
Image imgs[]=new Image[3];
String text=" ";
int n=0,pos=0;
int len=this.getSize().width;
public void start()
{
ttext=new Thread(this);
t=new Thread(this);
n=0;
t.start();
}
public void init()
{
setBackground(Color.blue);
t1=new TextField(6);
t2=new TextField(6);
t3 =new TextField(15);
t1.setBackground(Color.lightGray);
t2.setBackground(Color.lightGray);
t3.setBackground(Color.lightGray);
for(int i=0;i<3;i++)
{
imgs[i]=getImage(getCodeBase(),String.valueOf(i+1)+".gif");
}
c=new Choice();
c.add("+");
c.add("-");
c.add("*");
c.add("/");
c.add("%");
//c.addItemListener(this);
add(t1);
add(c);
add(t2);
btn=new Label("=");
add(btn);
add(t3);
btn1=new Button("查看结果");
add(btn1);
btn1.addActionListener(this);
}
/*public void itemStateChanged(ItemEvent e)
{
str=(String)e.getItem();
} */
public String getResult()
{
if(c.getSelectedItem()=="+")
return String.valueOf(Integer.parseInt(t1.getText())+Integer.parseInt(t2.getText()));
if(c.getSelectedItem()=="-")
return String.valueOf(Integer.parseInt(t1.getText())-Integer.parseInt(t2.getText()));
if(c.getSelectedItem()=="*")
return String.valueOf(Integer.parseInt(t1.getText())*Integer.parseInt(t2.getText()));
if(c.getSelectedItem()=="/")
return String.valueOf(Integer.parseInt(t1.getText())/Integer.parseInt(t2.getText()));
else
return String.valueOf(Integer.parseInt(t1.getText())%Integer.parseInt(t2.getText()));
}
public void run()
{
while(t==Thread.currentThread())
{
try
{
t.sleep(300);
repaint();
n++;
}catch(Exception e){}
if(n>2) n=0;
}
while(ttext==Thread.currentThread())
{
try
{
ttext.sleep(300);
repaint();
pos+=5;
}catch(Exception e){}
if(pos>550)
ttext=null;
}
}
public void paint(Graphics g)
{
float rr=(float)Math.random();
float gg=(float)Math.random();
float bb=(float)Math.random();
g.setColor(new Color(rr,gg,bb));
g.setFont(new Font("宋体",Font.BOLD+Font.ITALIC,36));
g.drawString(text,this.getSize().width-2*pos,this.getSize().height/2-30);
g.drawImage(imgs[n],this.getSize().width/2-100,this.getSize().height/2,230,200,this);
g.fillOval(100,150,80,80);}
public void actionPerformed(ActionEvent e)
{
String s=t3.getText();
if(e.getSource()==btn1)
{
if(s.equals(getResult()))
{
ttext=new Thread(this);
ttext.start();
text="真聪明!!";
}
else
{
ttext=new Thread(this);
ttext.start();
text="你好笨!!^_^糗大了!!哈哈!!";
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -