?? 第十章例子.txt
字號:
10-例子1
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Example10_1 extends Applet implements ActionListener
{ Label label1,label2; TextField text1,text2;Button button;
public void init()
{ label1=new Label("我是第一個標簽",Label.CENTER);
label2=new Label("我是第二個標簽",Label.LEFT);
text1=new TextField(10); text2=new TextField(10);
button=new Button("確定"); add(label1);add(label2);
add(text1);add(text2); add(button);
button.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{ if(e.getSource()==button)
{ text1.setText(label1.getText()); text2.setText(label2.getText());
}
else
{}
}
}
10-例子2
import java.applet.*;
import java.awt.*;
public class Example10_2 extends Applet
{ Label label1,label2; TextField text1,text2;
public void init()
{ label1=new Label("輸入姓名");
label2=new Label("輸入性別");
label1.setBackground(Color.red);label1.setForeground(Color.blue);
text1=new TextField(10); text2=new TextField(10);
add(label1); add(text1);add(label2); add(text2);
}
}
10-例子3
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
class MyLabel extends Label implements ActionListener
{TextField text1;TextArea text2;int k=0;
MyLabel(String s)
{super(s);
text1=new TextField(10); text2=new TextArea(10,10);
text1.addActionListener(this); //標簽作為文本框的監視器。
}
public void actionPerformed(ActionEvent e)
{ double n=0;text2.setText(null);
try{n=Double.valueOf(text1.getText()).doubleValue();
for(int i=1;i<=n;i++)
{if(n%i==0)
text2.append("\n"+i);
}
}
catch(NumberFormatException e1)
{text1.setText("請輸入數字字符");
}
if(n%2==0)
{this.setBackground(Color.green);}
else
{this.setBackground(Color.yellow);}
}
}
public class Example10_3 extends Applet
{ MyLabel lab;
public void init()
{lab=new MyLabel("請輸入一個數后按回車鍵");
add(lab);add(lab.text1);add(lab.text2);
}
}
10-例子4
import java.awt.*;import java.applet.*;
public class Example10_3 extends Applet
{public void init()
{MyLabel mylabel=new MyLabel();setLayout(null);
setLocation(12,12);add(mylabel);
}
}
class MyLabel extends Label
{ MyLabel()
{setSize(20,160);setBackground(Color.white);
}
public void paint(Graphics g)
{g.drawString("我",2,14); //在標簽上寫"我"。
g.drawString("是",2,34);
g.drawString("一",2,54);
g.drawString("個",2,74);
g.drawString("豎",2,94);
g.drawString("標",2,114);
g.drawString("簽",2,134);
g.drawString("!",2,154);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -