📄 usetextfield.java
字号:
import java.awt.*;
import java.awt.event.*;
public class UseTextField{
public static void main(String args[]){
MyFrame f=new MyFrame();
f.setSize(330,200);
f.setBackground(Color.lightGray);
f.show();
}
}
class MyFrame extends Frame implements ActionListener{
TextField tf1,tf2,tf3;
Label l1,l2;
public MyFrame(){
super("my frame");
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
System.exit(0);
}});
l1=new Label("请数入两个数:");
l2=new Label(" ");
tf1=new TextField(5);
tf2=new TextField(5);
tf3=new TextField("+ - * %");
tf3.setForeground(Color.red);
tf3.setBackground(Color.cyan);
tf3.addActionListener(this);
setLayout(new FlowLayout());
add(l1); add(tf1);add(tf2);add(tf3);add(l2);
}
public void actionPerformed(ActionEvent e){
int x=0,y=1;char ch='0';
if(e.getSource()==tf3){
x=Integer.parseInt(tf1.getText());
y=Integer.parseInt(tf2.getText());
ch=tf3.getSelectedText().charAt(0);
}
switch(ch){
case'+':
l2.setText("运算结果:"+x+"+"+y+"="+(x+y));break;
case'-':
l2.setText("运算结果:"+x+"-"+y+"="+(x-y));break;
case'*':
l2.setText("运算结果:"+x+"*"+y+"="+(x*y));break;
case'%':
l2.setText("运算结果:"+x+"%"+y+"="+(x%y));break;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -