Label extension

Write a subclass of the Label class in order to add some new attributes and functionality, add new member variables in a subclass,
for example, subclasses can have members of variable text boxes, text areas.
In the following example 4, MyLabel Label class is a subclass of the object MyLabel create the text boxes, text areas, buttons and other member variables. . MyLabe class implements the ActionListener interface, in order to monitor events on the text box inputNumber. When the digital input in the text box inputNumber carriage, the text display area showResult all factors this number, the button click button, the text display area showResult all primes not exceed this number.
* in java.awt Import;. Import the java.awt.event *;. * Import the java.applet;.
class MyLabel the extends the Label the implements the ActionListener
{  
initial String name on the label;
the TextField InputNumber; the TextArea the showResult; the Button Button; MyLabel (String S , CON Container)
{
 Super (S);
initial name on a label = s;
inputNumber=new TextField(10); showResult =new TextArea(10,10); button=new Button("Enter"); button.addActionListener(this);inputNumber.addActionListener(this); con.add(this);con.add(inputNumber);con.add(showResult);con.add(button);
}
public void actionPerformed(ActionEvent e)
{
 long n=0;
showResult.setText(null);
try{  n=Long.valueOf(inputNumber.getText()).longValue();
this.setText(标签上的初始名称);
catch(NumberFormatException e1)
{
 this.setText("请输入数字字符");
}
if(e.getSource()==inputNumber)
{  求因子(n);
}
if(e.getSource()==button)
{  
求素数(n);
}
}
public void 求因子(long n)
{  for(int i=1;i<=n;i++)
IF {(n-I% == 0)
showResult.append ( "\ n-'+ I);
}
}
public void find primes (n-Long)
{  
showResult.append (" less than "+ n +" are prime: ");
for (int I =. 1; I <= n-; I ++)
{
 int J = 0;
for (J = 2; J <I; J ++)
{
 IF (I% J == 0) BREAK;
}
IF (J> = I)
{showResult.append ( "\ n-" + I);
}
}
}
}
public class Example10_4 the extends the Applet
{  
MyLabel Lab;
public void the init ()
{new new MyLabel Lab = ( "Enter the required number of factors, click button is determined less than prime this number ", the this);
}
}
Problem ten
1 write an applet, a button and a text box in the container applet in when the button is clicked.
text box displays the names of the buttons .
2 after writing a text box and two small applications of a button, enter the word girl in the back by a text box
Car key or click the button, another text box can be displayed "boy" word.
3. Write a small application designed four buttons, named "plus", "poor", "plot", "except" there are three text boxes. clicking the appropriate button, the digital calculation do two text boxes, displays the results in the third text box. a NumberFormatException request processing.
preparation of a shaped vertical tab.

Source: webmaster platform

Guess you like

Origin www.cnblogs.com/1994july/p/12412032.html
Recommended