2.5.事件監聽
public class Test02 {
public static void main(String[] args) {
//按下按鈕,觸發事件
//兩個按鈕可以實現同一個監聽
Frame frame = new Frame();
frame.setBackground(new Color(208, 162, 226));
frame.setVisible(true);
frame.setBounds(200,200,200,200);
Button button1 = new Button("button1");
button1.setActionCommand("button1按鈕點擊");
//因爲addActionListener()需要一個ActionListener,所以我們需要構造一個ActionListener
button1.addActionListener(new MyListen());
Button button2 = new Button("button2");
button2.setActionCommand("button2按鈕點擊");
button2.addActionListener(new MyListen());
frame.add(button1,BorderLayout.WEST);
frame.add(button2,BorderLayout.EAST);
}
}
class MyListen implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(e.getActionCommand());
}
}
按下按鈕一和按鈕二時,會發生如下點擊事件:
2.6.輸入框TextField監聽
public class Test2 {
public static void main(String[] args) {
new MyFrame();
}
}
class MyFrame extends Frame{
public MyFrame(){
setVisible(true);
//frame的自適應大小函數
pack();
TextField field = new TextField();
add(field);
//設置替換編碼
field.setEchoChar('*');
//監聽這個文本框輸入的文字,按下enter,就會觸發這個輸入框的事件
field.addActionListener(new MyActionListen());
}
}
class MyActionListen implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//獲得一些資源,返回一個對象
TextField field = (TextField) e.getSource();
//獲得輸入框的文字
System.out.println(field.getText());
//null,輸入框清零
field.setText("");
}
}
2.7.簡易計算器
//簡易計算器
public class TestCalc {
public static void main(String[] args) {
new Calculator().loadFrame();
}
}
//計算器類
class Calculator extends Frame {
//屬性
TextField num1,num2,num3;
//方法
public void loadFrame(){
num1 = new TextField(10);//字符數
num2 = new TextField(10);
num3 = new TextField(20);
Button button = new Button("=");
Label label = new Label("+");
button.addActionListener(new MyCalculatorListener());
//佈局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
//監聽器類
//內部類最大的好處,就是可以暢通無阻的訪問外部的屬性和方法
private class MyCalculatorListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//1.獲得加數和被加數
//2.將這個值 +法運算後,放到第三個框
//3.清除前兩個框
int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());
num3.setText(""+(n1+n2));
num1.setText("");
num2.setText("");
}
}
}
2.8.畫筆
public class TestPaint {
public static void main(String[] args) {
new MyPaint().loadFrame();
}
}
class MyPaint extends Frame{
public void loadFrame(){
setVisible(true);
setBounds(400,400,400,400);
}
//畫筆
@Override
public void paint(Graphics g) {
//畫筆,需要有顏色,畫筆可以畫畫
g.setColor(new Color(125, 57, 99));
g.drawRect(100,100,100,100);
g.drawOval(200,200,50,50);
}
}
2.9.鼠標監聽
//鼠標監聽事件
public class TestMouseListener {
public static void main(String[] args) {
new MyFrame("畫圖");
}
}
class MyFrame extends Frame {
//畫畫需要畫筆,需要監聽鼠標當前的位置,需要集合來存儲這個點
private List points;
public MyFrame(String title){
super(title);
//存鼠標點擊的點
this.points = new ArrayList();
setVisible(true);
setBounds(400,400,400,400);
//鼠標監聽器
addMouseListener(new MouseListener());
}
@Override
public void paint(Graphics g) {
Iterator iterator = points.iterator();
while (iterator.hasNext()){
Point point = (Point) iterator.next();
g.setColor(new Color(103, 188, 47));
g.drawOval(point.x,point.y,5,5);
}
}
class MouseListener extends MouseAdapter{
@Override
public void mousePressed(MouseEvent e) {
MyFrame frame = (MyFrame) e.getSource();
//這個點就是鼠標的點
Point point = new Point(e.getX(), e.getY());
points.add(point);
//每次點擊鼠標都需要重新畫一遍
frame.repaint();
}
}
}
2.10.窗口監聽
public class TestWindowListener {
public static void main(String[] args) {
new MyWindowListener();
}
}
class MyWindowListener extends Frame{
public MyWindowListener(){
setVisible(true);
setBackground(Color.BLUE);
setBounds(300,300,300,300);
addWindowListener(new WindowAdapter() {
@Override//關閉窗口
public void windowClosing(WindowEvent e) {
System.out.println("窗口已關閉");
System.exit(0);
}
@Override//激活窗口
public void windowActivated(WindowEvent e) {
setTitle("你快回來啊");
}
});
}
}
2.11鍵盤監聽
public class TestKeyListener {
public static void main(String[] args) {
new KeyFrame();
}
}
class KeyFrame extends Frame{
public KeyFrame(){
setVisible(true);
setBounds(200,200,200,200);
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
//獲得鍵盤下的鍵是哪一個,當前的碼
int keyCode = e.getKeyCode();
//不需要記住這個數值,直接使用靜態屬性VK_XX
if(keyCode == KeyEvent.VK_UP){
System.out.println("你按了上鍵");
}
//根據按下不同鍵,產生不同操作
}
});
}
}