?? calc1.java
字號:
import java.awt.*;
import java.awt.event.*;
public class Calc1 implements ActionListener
{
Frame f;
TextField tf1;
Button b1,b2,b3,b4;
public void display()
{
f = new Frame("Calculation");
f.setSize(260,150);
f.setLocation(320,240); //設置窗口初始位置
f.setBackground(Color.lightGray);
f.setLayout(new FlowLayout(FlowLayout.LEFT));//改變布局且左對齊
tf1 = new TextField(30);
tf1.setEditable(false); //只能顯示,不允許編輯
f.add(tf1);
b1 = new Button("1");
b2 = new Button("2");
b3 = new Button("+");
b4 = new Button("C");
f.add(b1);
f.add(b2);
f.add(b3);
f.add(b4);
b1.addActionListener(this); //為按鈕b1注冊事件監聽程序
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
f.addWindowListener(new WinClose()); //為框架f注冊事件監聽程序
f.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{ //實現ActionListener接口中的方法,單擊按鈕時產生該事件
if (e.getSource()==b4) //獲得產生事件的對象
tf1.setText("");
else //獲取按鈕標簽,重新設置文本內容
tf1.setText(tf1.getText()+e.getActionCommand());
}
public static void main(String arg[])
{
(new Calc1()).display();
}
}
class WinClose extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{ //覆蓋WindowAdapter類中同名方法,單擊窗口關閉按鈕時產生該事件
System.exit(0); //結束程序運行,關閉窗口
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -