2018年11月16日 星期五

[Java]_ch15.3_Button元件事件處理





[Java]_ch15.3_Button元件事件處理

package YPU;



import java.awt.Button;

import java.awt.Color;

import java.awt.Frame;

import java.awt.Label;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;



public class MyFrame extends Frame implements ActionListener{

Label lb=new Label();

Button btn1=new Button("紅色");

Button btn2=new Button("綠色");

Button btn3=new Button("藍色");

Button btn4=new Button("離開");

public MyFrame(String name){

super(name);

setLayout(null);

setBackground(Color.PINK);

setSize(300,300);

setLocation(800,300);

setVisible(true);



lb.setText("視窗元件設計");

lb.setBackground(Color.YELLOW);

lb.setSize(200, 50);

lb.setLocation(50, 50);



btn1.setSize(100, 50);

btn1.setLocation(50, 110);



btn2.setSize(100, 50);

btn2.setLocation(150, 110);



add(btn1);

add(btn2);

add(lb);

}

public MyFrame(String name,int w,int h,Color color){

super(name);

setLayout(null);

setBackground(color);

setSize(w,h);

setLocation(800,300);

setVisible(true);



lb.setText("視窗元件設計");

lb.setBackground(Color.YELLOW);

lb.setSize(200, 50);

lb.setLocation(50, 50);



btn1.setSize(50, 30);

btn1.setLocation(50, 110);



btn2.setSize(50, 30);

btn2.setLocation(100, 110);



btn3.setBounds(150, 110, 50, 30);

btn4.setBounds(200, 110, 50, 30);

add(btn4);

add(btn3);

add(btn1);

add(btn2);

add(lb);

}

@Override

public void actionPerformed(ActionEvent e) {

Button btn=(Button)e.getSource();

if(btn==btn1){

lb.setBackground(Color.RED);

lb.setText(btn1.getLabel());

}else if(btn==btn2){

lb.setBackground(Color.GREEN);

lb.setText(btn2.getLabel());

}else if(btn==btn3){

lb.setBackground(Color.BLUE);

lb.setText(btn3.getLabel());

}else if(btn==btn4){

System.exit(0);

}

}

}


沒有留言:

張貼留言

Ether.fi Cash Card 一張所有人必備的加密貨幣信用卡   [Ether.fi Cash Card] : https://www.ether.fi/app/cash/sign-up?ref_code=d27f8523 ------國際交易所------ [Bitg...