Java2实用教程(第二版)程序代码——第十章 按钮与标签

  1 None.gif // 例子1
  2 None.gif import java.applet. * ;import java.awt. * ;import java.awt. event . *
  3 None.gif public   class  Example10_1 extends Applet implements ActionListener
  4 ExpandedBlockStart.gifContractedBlock.gif dot.gif {  TextField text;
  5InBlock.gif   Button buttonEnter,buttonQuit;
  6ExpandedSubBlockStart.gifContractedSubBlock.gif   public void init()dot.gif{
  7ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{  text=new TextField("0",10); add(text);
  8InBlock.gif      buttonEnter=new Button("确定"); buttonQuit =new Button("清除");   
  9InBlock.gif      add(buttonEnter); add(buttonQuit);
 10InBlock.gif      buttonEnter.addActionListener(this);
 11InBlock.gif      buttonQuit.addActionListener(this);
 12InBlock.gif      text.addActionListener(this);
 13ExpandedSubBlockEnd.gif   }
 
 14InBlock.gif public void paint(Graphics g) 
 15ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{  g.drawString("在文本框输入数字字符回车或单击按钮",10,100);
 16InBlock.gif      g.drawString("第文本框显示该数的平方根",10,120);
 17ExpandedSubBlockEnd.gif   }

 18InBlock.gif   public void actionPerformed(ActionEvent e) 
 19ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{  if(e.getSource()==buttonEnter||e.getSource()==text)
 20ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{  double number=0;
 21ExpandedSubBlockStart.gifContractedSubBlock.gif           try dot.gif{  number=Double.valueOf(text.getText()).doubleValue();
 22InBlock.gif                  text.setText(""+Math.sqrt(number));
 23ExpandedSubBlockEnd.gif               }

 24InBlock.gif           catch(NumberFormatException event)
 25ExpandedSubBlockStart.gifContractedSubBlock.gif               dot.gif{  text.setText("请输入数字字符");
 26ExpandedSubBlockEnd.gif               }
 
 27ExpandedSubBlockEnd.gif        }

 28InBlock.gif      else if(e.getSource()==buttonQuit)
 29ExpandedSubBlockStart.gifContractedSubBlock.gif       dot.gif{  text.setText("0");
 30ExpandedSubBlockEnd.gif       }

 31ExpandedSubBlockEnd.gif   }
 
 32ExpandedSubBlockEnd.gif}

 33InBlock.gif
 34InBlock.gif//例子2
 35InBlock.gifimport java.awt.*;import java.applet.*;import java.awt.event.*;
 36InBlock.gif//写一个按扭类的子类,增加一些新的功能:
 37InBlock.gifclass MyButton extends Button implements ActionListener,TextListener
 38ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{  TextArea text1,text2;      //类的成员变量。
 39InBlock.gif   char save[];
 40InBlock.gif   MyButton(String s,Container con)
 41ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{  super(s); 
 42InBlock.gif      text1=new TextArea(6,12);  text2=new TextArea(6,12);
 43InBlock.gif      text2.setEditable(false);
 44InBlock.gif      text1.addTextListener(this);  //创建的按扭监视其中一个文本区。
 45InBlock.gif      this.addActionListener(this); //创建的按扭自己监视自己。
 46InBlock.gif      con.add(text1);con.add(text2);con.add(this);
 47ExpandedSubBlockEnd.gif   }

 48InBlock.gif   public void textValueChanged(TextEvent e) //实现接口。
 49ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif
 50InBlock.gif   String s=text1.getText();
 51InBlock.gif   StringBuffer strbuffer=new StringBuffer(s);
 52InBlock.gif   String  temp=new String(strbuffer.reverse());   
 53InBlock.gif   text2.setText(temp);
 54ExpandedSubBlockEnd.gif   }

 55InBlock.gif   public void actionPerformed(ActionEvent e) //实现接口。
 56ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{  text2.setText(null);
 57InBlock.gif      String s=text1.getText();
 58InBlock.gif     int length=s.length();
 59InBlock.gif     save=new char[length];
 60InBlock.gif     //将字符串拷贝到数组save:
 61InBlock.gif     s.getChars(0,length,save,0);
 62InBlock.gif     for(int i=0;i<save.length;i++)
 63ExpandedSubBlockStart.gifContractedSubBlock.gif       dot.gif{  save[i]=(char)(save[i]^'');
 64ExpandedSubBlockEnd.gif       }

 65InBlock.gif     String temp=new String(save);
 66InBlock.gif     text2.setText(temp);   
 67ExpandedSubBlockEnd.gif   }
 
 68ExpandedSubBlockEnd.gif}

 69InBlock.gifpublic class Example10_2 extends Applet implements ActionListener
 70ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{  MyButton mybutton;
 71InBlock.gif   Button button;
 72InBlock.gif   public void init()
 73ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{  mybutton=new MyButton("加密",this); 
 74InBlock.gif      button=new Button("解密");
 75InBlock.gif      button.addActionListener(this);
 76InBlock.gif      add(button); 
 77ExpandedSubBlockEnd.gif   }

 78InBlock.gif   public void actionPerformed(ActionEvent e) //实现接口。
 79ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{  for(int i=0;i<mybutton.save.length;i++)
 80ExpandedSubBlockStart.gifContractedSubBlock.gif       dot.gif{  mybutton.save[i]=(char)(mybutton.save[i]^'');
 81ExpandedSubBlockEnd.gif       }
 
 82InBlock.gif      String temp=new String(mybutton.save);
 83InBlock.gif      mybutton.text2.setText(temp);   
 84ExpandedSubBlockEnd.gif   }
 
 85ExpandedSubBlockEnd.gif}

 86InBlock.gif
 87InBlock.gif//例子3
 88InBlock.gifimport java.awt.*;import java.applet.*;
 89InBlock.gifimport java.awt.event.*;
 90InBlock.gifpublic class Example10_3 extends Applet 
 91ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{   public void init()
 92ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{  MyButton1 button1=new MyButton1();
 93InBlock.gif      MyButton2 button2=new MyButton2();
 94InBlock.gif      setLayout(null);
 95InBlock.gif      add(button1);add(button2);
 96InBlock.gif      button1.setLocation(12,12);
 97InBlock.gif      button2.setLocation(60,12);
 98ExpandedSubBlockEnd.gif   }

 99ExpandedSubBlockEnd.gif}

100InBlock.gifclass MyButton1 extends Button implements ActionListener 
101ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{  int n=-1;
102InBlock.gif   MyButton1()
103ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{ setSize(25,160); addActionListener(this); 
104ExpandedSubBlockEnd.gif   }

105InBlock.gif   public void paint(Graphics g) 
106ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{  g.drawString("",6,14);  g.drawString("",6,34);
107InBlock.gif      g.drawString("",6,54);  g.drawString("",6,74);
108InBlock.gif      g.drawString("",6,94);  g.drawString("",6,114);
109InBlock.gif      g.drawString("",6,134); g.drawString("!",8,154);
110ExpandedSubBlockEnd.gif   }

111InBlock.gif   public void actionPerformed(ActionEvent e)
112ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{  n=(n+1)%3;
113InBlock.gif      if(n==0)
114InBlock.gif         this.setBackground(Color.cyan);
115InBlock.gif      else if(n==1)
116InBlock.gif         this.setBackground(Color.orange);
117InBlock.gif      else if(n==2)
118InBlock.gif         this.setBackground(Color.pink);
119ExpandedSubBlockEnd.gif   }
 
120ExpandedSubBlockEnd.gif}

121InBlock.gifclass MyButton2 extends Button 
122ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{  MyButton2()
123ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{  setSize(38,80); setBackground(Color.cyan);
124ExpandedSubBlockEnd.gif   }

125InBlock.gif   public void paint(Graphics g)
126ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{  g.setColor(Color.red);
127InBlock.gif      g.fillOval(10,3,20,20);        //在按扭上画圆,见17章。
128InBlock.gif      g.setColor(Color.yellow);   g.fillOval(10,28,20,20);
129InBlock.gif      g.setColor(Color.green);    g.fillOval(10,53,20,20);
130ExpandedSubBlockEnd.gif   }

131ExpandedSubBlockEnd.gif}

132InBlock.gif
133InBlock.gif//例子4
134InBlock.gifimport java.awt.*;import java.awt.event.*;import java.applet.*;
135InBlock.gifclass MyLabel extends Label implements ActionListener
136ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{  String 标签上的初始名称;
137InBlock.gif   TextField inputNumber;TextArea showResult;Button button;
138InBlock.gif   MyLabel(String s,Container con) 
139ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{  super(s);
140InBlock.gif      标签上的初始名称=s;
141InBlock.gif      inputNumber=new TextField(10); showResult =new TextArea(10,10);
142InBlock.gif      button=new Button("Enter");
143InBlock.gif      button.addActionListener(this);inputNumber.addActionListener(this);
144InBlock.gif      con.add(this);con.add(inputNumber);con.add(showResult);con.add(button);
145ExpandedSubBlockEnd.gif   }

146InBlock.gif   public void actionPerformed(ActionEvent e) 
147ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{  long n=0;
148InBlock.gif      showResult.setText(null);
149ExpandedSubBlockStart.gifContractedSubBlock.gif      trydot.gif{  n=Long.valueOf(inputNumber.getText()).longValue();
150InBlock.gif            this.setText(标签上的初始名称);
151ExpandedSubBlockEnd.gif         }

152InBlock.gif      catch(NumberFormatException e1)
153ExpandedSubBlockStart.gifContractedSubBlock.gif         dot.gif{  this.setText("请输入数字字符");
154ExpandedSubBlockEnd.gif         }

155InBlock.gif      if(e.getSource()==inputNumber)
156ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{  求因子(n);
157ExpandedSubBlockEnd.gif        }

158InBlock.gif      if(e.getSource()==button)
159ExpandedSubBlockStart.gifContractedSubBlock.gif          dot.gif{  求素数(n);
160ExpandedSubBlockEnd.gif          }

161ExpandedSubBlockEnd.gif   }

162InBlock.gif public void 求因子(long n) 
163ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{  for(int i=1;i<=n;i++)
164ExpandedSubBlockStart.gifContractedSubBlock.gif       dot.gif{  if(n%i==0)
165InBlock.gif            showResult.append("\n"+i);
166ExpandedSubBlockEnd.gif       }

167ExpandedSubBlockEnd.gif   }

168InBlock.gif   public void 求素数(long n) 
169ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{  showResult.append("小于"+n+"的素数有:");
170InBlock.gif      for(int i=1;i<=n;i++)
171ExpandedSubBlockStart.gifContractedSubBlock.gif         dot.gif{  int j=0;
172InBlock.gif            for(j=2;j<i;j++)
173ExpandedSubBlockStart.gifContractedSubBlock.gif               dot.gif{  if(i%j==0)  break;
174ExpandedSubBlockEnd.gif               }

175InBlock.gif            if(j>=i)
176ExpandedSubBlockStart.gifContractedSubBlock.gif              dot.gif{ showResult.append("\n"+i);
177ExpandedSubBlockEnd.gif              }

178ExpandedSubBlockEnd.gif          }

179ExpandedSubBlockEnd.gif    }
 
180ExpandedSubBlockEnd.gif}

181InBlock.gifpublic class Example10_4  extends Applet
182ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{  MyLabel lab;
183InBlock.gif   public void init()
184ExpandedSubBlockStart.gifContractedSubBlock.gif   dot.gif{  lab=new MyLabel("回车求该数的因子,单击按钮求出小于这个数的素数",this);
185ExpandedSubBlockEnd.gif   }

186ExpandedSubBlockEnd.gif}

187InBlock.gif

转载于:https://www.cnblogs.com/rookieport/archive/2005/05/27/163349.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值