[Java]_ch15.6_CheckBox元件事件處理
package YPU;
import java.awt.Button;
import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MyFrame extends Frame implements ActionListener{
Label lb=new Label();
Label lb1=new Label("國文: ");
Label lb2=new Label("英文: ");
Label lb3=new Label("數學: ");
Button btn1=new Button("紅色");
Button btn2=new Button("綠色");
Button btn3=new Button("藍色");
Button btn4=new Button("離開");
Button btn5=new Button("送出");
Button btn6=new Button("清除");
Button btn7=new Button("計算");
Button btn8=new Button("清除");
TextField tf=new TextField();
TextField tf1=new TextField();
TextField tf2=new TextField();
TextField tf3=new TextField();
TextArea ta=new TextArea("結果顯示如下:\n",10,10,TextArea.SCROLLBARS_VERTICAL_ONLY);
Checkbox cb1=new Checkbox("加權1.2");
Checkbox cb2=new Checkbox("加權1.5",true);
Checkbox cb3=new Checkbox("加權1.8");
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);
Font lbFont=new Font("新細明體",Font.BOLD+Font.ITALIC,24);
Font btnFont=new Font("新細明體",Font.BOLD,14);
Color btnColor=new Color(255,0,255);
lb.setText("視窗元件設計");
lb.setBackground(Color.YELLOW);
lb.setSize(200, 50);
lb.setLocation(50, 50);
lb.setFont(lbFont);
lb.setAlignment(Label.CENTER);
btn1.setSize(50, 30);
btn1.setLocation(50, 110);
btn1.setFont(btnFont);
btn1.setForeground(btnColor);
btn2.setSize(50, 30);
btn2.setLocation(100, 110);
btn2.setFont(btnFont);
btn2.setForeground(btnColor);
btn3.setBounds(150, 110, 50, 30);
btn3.setFont(btnFont);
btn3.setForeground(btnColor);
btn4.setBounds(200, 110, 50, 30);
btn4.setFont(btnFont);
btn4.setForeground(btnColor);
tf.setBounds(50, 150, 100, 30);
tf.setEchoChar('*');
btn5.setBounds(150, 150, 50, 30);
btn6.setBounds(200, 150, 50, 30);
lb1.setBounds(50, 190, 50, 30);
tf1.setBounds(100, 190, 100, 30);
lb2.setBounds(50, 220, 50, 30);
tf2.setBounds(100, 220, 100, 30);
lb3.setBounds(50, 250, 50, 30);
tf3.setBounds(100, 250, 100, 30);
btn7.setBounds(200, 190, 50, 45);
btn8.setBounds(200, 235, 50, 45);
ta.setEditable(false);
cb1.setBounds(50, 280, 70, 30);
cb2.setBounds(120, 280, 70, 30);
cb3.setBounds(190, 280, 70, 30);
ta.setBounds(50, 310, 200, 100);
CheckboxGroup grp=new CheckboxGroup();
cb1.setCheckboxGroup(grp);
cb2.setCheckboxGroup(grp);
cb3.setCheckboxGroup(grp);
add(btn4);
add(btn3);
add(btn1);
add(btn2);
add(btn5);
add(btn6);
add(btn7);
add(btn8);
add(lb);
add(lb1);
add(lb2);
add(lb3);
add(tf);
add(tf1);
add(tf2);
add(tf3);
add(ta);
add(cb1);
add(cb2);
add(cb3);
}
@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);
}else if(btn==btn5){
lb.setText(tf.getText());
ta.append(tf.getText()+"\n");
if(cb1.getState())
ta.append(cb1.getLabel()+"\n");
if(cb2.getState())
ta.append(cb2.getLabel()+"\n");
if(cb3.getState())
ta.append(cb3.getLabel()+"\n");
}else if(btn==btn6){
lb.setText("");
tf.setText("");
ta.setText("");
}else if(btn==btn7){
String s1=tf1.getText();
int n1=Integer.parseInt(s1);
String s2=tf2.getText();
double n2=Double.parseDouble(s2);
String s3=tf3.getText();
int n3=Integer.parseInt(s3);
double sum=n1+n2+n3;
double avg=(n1+n2+n3)/3;
ta.append("國文:"+s1+" 英文:"+s2+" 數學:"+s3+"\n");
ta.append("總分:"+sum+"\n");
ta.append("平均:"+avg+"\n");
ta.append((avg>=60)?"恭喜及格\n":"抱歉不及格!\n");
if(avg>=90 && avg<=100)
ta.append("A級\n");
if(avg>=80 && avg<90)
ta.append("B級\n");
if(avg>=70 && avg<80)
ta.append("C級\n");
if(avg>=60 && avg<70)
ta.append("D級\n");
if(avg>=0 && avg<60)
ta.append("E級\n");
if(cb1.getState()){
ta.append("權重:"+cb1.getLabel()+"\n");
ta.append("加權總分:"+sum*1.2+"\n加權平均:"+sum*1.2/3);
}else if(cb2.getState()){
ta.append("權重:"+cb2.getLabel()+"\n");
ta.append("加權總分:"+sum*1.5+"\n加權平均:"+sum*1.5/3);
}else if(cb3.getState()){
ta.append("權重:"+cb3.getLabel()+"\n");
ta.append("加權總分:"+sum*1.8+"\n加權平均:"+sum*1.8/3);
}
}else if(btn==btn8){
tf1.setText("");
tf2.setText("");
tf3.setText("");
ta.setText("");
}
}
}
沒有留言:
張貼留言