2018年11月30日 星期五

[Android]_ch9.7_地名或地址轉成位置





[Android]_ch9.7_地名或地址轉成位置



呼叫Geocoder.getFromLocationName(String
locationName, int maxResults)可將使用者輸入的地名/地址(LocationName)轉成List<Address>
Address物件可以呼叫getLatitude()getLongitude()得到緯經度


參數maxResults用來限定回傳的資料筆數


et1=(EditText)findViewById(R.id.editText1);

        btn1=(Button)findViewById(R.id.button1);

        btn1.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

String name=et1.getText().toString();

Geocoder gc=new Geocoder(MainActivity.this);

List<Address> list=null;

try {

list=gc.getFromLocationName(name, 3);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

if(list.isEmpty() || list==null)

Toast.makeText(MainActivity.this, "無法找到該位置!", Toast.LENGTH_SHORT).show();

else{

Address address=list.get(0);

double lat=address.getLatitude();

double lng=address.getLongitude();

MarkerOptions mo=new MarkerOptions();

mo.position(new LatLng(lat, lng));

mo.title(name);

mo.snippet(address.getAddressLine(0));

mo.icon(BitmapDescriptorFactory.fromResource(R.drawable.m1));

map.addMarker(mo);

CameraPosition cp=new CameraPosition.Builder().target(new LatLng(lat, lng)).zoom(15).build();

CameraUpdate cu=CameraUpdateFactory.newCameraPosition(cp);

map.animateCamera(cu);

}

}

});

[Android]_ch9.6_Google地圖設定鏡頭焦點





[Android]_ch9.6_Google地圖設定鏡頭焦點

鏡頭設定:改變地圖的鏡頭顯示特定地方
建立CameraPosition物件設定欲顯示的地點與縮放層級
CameraUpdateFactory.newCameraPosition(CameraPosition)建立CameraUpdate物件


GoogleMap.animateCamera(CameraUpdate)以動畫模式移動鏡頭到指定地點

map.setOnMapClickListener(new OnMapClickListener() {

@Override

public void onMapClick(LatLng point) {

MarkerOptions mo=new MarkerOptions();

mo.position(point);

mo.snippet(point.toString());

mo.icon(BitmapDescriptorFactory.fromResource(R.drawable.m1));

map.addMarker(mo);

CameraPosition cp=new CameraPosition.Builder().target(point).zoom(15).build();

CameraUpdate cu=CameraUpdateFactory.newCameraPosition(cp);

map.animateCamera(cu);

}

});

[Android]_ch9.5_Google地圖標記設計





[Android]_ch9.5_Google地圖標記設計

case R.id.checkBox6:

LatLng ks1=new LatLng(22.6398132,120.3016222);

LatLng ks2=new LatLng(22.6498513,120.30606809999995);

MarkerOptions mo1=new MarkerOptions();

mo1.position(ks1);

mo1.title("高雄火車站");

mo1.snippet("聽說又老又窮..但要改變了!");

mo1.draggable(true);

mo1.visible(true);

mo1.icon(BitmapDescriptorFactory.fromResource(R.drawable.m2));

MarkerOptions mo2=new MarkerOptions();

mo2.position(ks2)

.title("高雄愛河").

snippet("聽說王世間要來跳!")

.icon(BitmapDescriptorFactory.fromResource(R.drawable.m3))

.draggable(false);

if(state){

map.addMarker(mo1);

map.addMarker(mo2);

}else{

map.clear();

}

break;

2018年11月27日 星期二

[Android]_ch5.3_Spinner元件(三)





[Android]_ch5.3_Spinner元件(三)

[Android]_ch5.2_Spinner元件(二)





[Android]_ch5.2_Spinner元件(二)

 sp2=(Spinner)findViewById(R.id.spinner2);

        final String data[]={"日間部","夜間部","產學班"};

        ArrayAdapter<String> AA=new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, data);

        AA.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        sp2.setAdapter(AA);

        sp2.setOnItemSelectedListener(new OnItemSelectedListener() {



@Override

public void onItemSelected(AdapterView<?> parent, View view,

int position, long id) {

Toast.makeText(MainActivity.this, "學制:"+data[position],Toast.LENGTH_SHORT ).show();

}



@Override

public void onNothingSelected(AdapterView<?> parent) {

// TODO Auto-generated method stub



}

});


[Android]_ch5.1_Spinner元件(一)





[Android]_ch5.1_Spinner元件(一)



nSpinner非常類似下拉選單
n建立Spinner元件以及事件處理步驟如下:
n1.在文字檔案內建立字串陣列作為選項 
n2.layout檔案內使用<Spinner>建立
nandroid:entries」屬性設定選項
n3.呼叫setOnItemSelectedListener()
註冊,並實作onItemSelected()以回應選項改變


2018年11月23日 星期五

[Java]_ch15.8_KeyListener與MouseListener與WindowListener事件處理





[Java]_ch15.8_KeyListener與MouseListener與WindowListener事件處理



package YPU;



import java.awt.Button;

import java.awt.event.MouseAdapter;

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;

import java.awt.event.ItemEvent;

import java.awt.event.ItemListener;

import java.awt.event.KeyAdapter;

import java.awt.event.KeyEvent;

import java.awt.event.KeyListener;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import java.awt.event.MouseMotionListener;

import java.awt.event.TextEvent;

import java.awt.event.TextListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;



public class MyFrame extends Frame {

static Label lb=new Label();

Label lb1=new Label("國文: ");

Label lb2=new Label("英文: ");

Label lb3=new Label("數學: ");

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

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

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

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

static Button btn5=new Button("送出");

static Button btn6=new Button("清除");

static Button btn7=new Button("計算");

static Button btn8=new Button("清除");

static TextField tf=new TextField();

static TextField tf1=new TextField();

static TextField tf2=new TextField();

static TextField tf3=new TextField();

static TextArea ta=new TextArea("結果顯示如下:\n",10,10,TextArea.SCROLLBARS_VERTICAL_ONLY);

static Checkbox cb1=new Checkbox("加權1.2");

static Checkbox cb2=new Checkbox("加權1.5",true);

static 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);

}

static class MyWindowAdapter extends WindowAdapter{

public void windowClosing(WindowEvent e){

System.exit(0);

}

}

static class MyMosuseAdapter extends MouseAdapter{

public void mousePressed(MouseEvent e){

ta.append("按下滑鼠..X="+e.getX()+" Y="+e.getY()+"\n");

}

}



static class MyMouseListener implements MouseListener,MouseMotionListener{



@Override

public void mouseDragged(MouseEvent e) {

ta.append("拖曳滑鼠..X="+e.getX()+" Y="+e.getY()+"\n");

}



@Override

public void mouseMoved(MouseEvent e) {

ta.append("移動滑鼠..X="+e.getX()+" Y="+e.getY()+"\n");

}



@Override

public void mouseClicked(MouseEvent e) {

ta.append("點擊滑鼠..X="+e.getX()+" Y="+e.getY()+"\n");

}



@Override

public void mouseEntered(MouseEvent e) {

ta.append("滑鼠移入..X="+e.getX()+" Y="+e.getY()+"\n");



}



@Override

public void mouseExited(MouseEvent e) {

ta.append("滑鼠移出..X="+e.getX()+" Y="+e.getY()+"\n");

}



@Override

public void mousePressed(MouseEvent e) {

ta.append("按下滑鼠..X="+e.getX()+" Y="+e.getY()+"\n");

}



@Override

public void mouseReleased(MouseEvent e) {

ta.append("放開滑鼠..X="+e.getX()+" Y="+e.getY()+"\n");

}



}

static class MyKeyAdapter extends KeyAdapter{

public void keyReleased(KeyEvent e){

ta.append("3.放開按鍵..字元:"+e.getKeyChar()+"\n");

}

}

static class MyKeyListener implements KeyListener{



@Override

public void keyPressed(KeyEvent e) {

ta.append("1.按下按鍵..字元:"+e.getKeyChar()+"\n");

}



@Override

public void keyReleased(KeyEvent e) {

// ta.append("3.放開按鍵..字元:"+e.getKeyChar()+"\n");

}



@Override

public void keyTyped(KeyEvent e) {

// ta.append("2.完成輸入..字元:"+e.getKeyChar()+"\n");

}



}



static class MyItemListener implements ItemListener{



@Override

public void itemStateChanged(ItemEvent e) {

Checkbox cb=(Checkbox)e.getSource();

if(cb.getState())

ta.append("權重:"+cb.getLabel()+"\n");

}

}



static class MyTextListener implements TextListener{

@Override

public void textValueChanged(TextEvent e) {

ta.setText(tf.getText());

}



}

static class MyActionListener implements ActionListener{



@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("");

}

}



}

}


2018年11月21日 星期三

[Java]_ch15.7_ItemListener與TextListener事件處理





[Java]_ch15.7_ItemListener與TextListener事件處理



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;

import java.awt.event.ItemEvent;

import java.awt.event.ItemListener;

import java.awt.event.TextEvent;

import java.awt.event.TextListener;



public class MyFrame extends Frame {

static Label lb=new Label();

Label lb1=new Label("國文: ");

Label lb2=new Label("英文: ");

Label lb3=new Label("數學: ");

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

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

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

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

static Button btn5=new Button("送出");

static Button btn6=new Button("清除");

static Button btn7=new Button("計算");

static Button btn8=new Button("清除");

static TextField tf=new TextField();

static TextField tf1=new TextField();

static TextField tf2=new TextField();

static TextField tf3=new TextField();

static TextArea ta=new TextArea("結果顯示如下:\n",10,10,TextArea.SCROLLBARS_VERTICAL_ONLY);

static Checkbox cb1=new Checkbox("加權1.2");

static Checkbox cb2=new Checkbox("加權1.5",true);

static 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);

}



static class MyItemListener implements ItemListener{



@Override

public void itemStateChanged(ItemEvent e) {

Checkbox cb=(Checkbox)e.getSource();

if(cb.getState())

ta.append("權重:"+cb.getLabel()+"\n");

}

}



static class MyTextListener implements TextListener{

@Override

public void textValueChanged(TextEvent e) {

ta.setText(tf.getText());

}



}

static class MyActionListener implements ActionListener{



@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("");

}

}



}

}







[Java]_ch15.6_CheckBox元件事件處理





[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("");

}

}

}


2018年11月19日 星期一

[Python基礎程式設計]ch6.3_計算1-100中2或3的倍數



[Python基礎程式設計]ch6.3_計算1-100中2或3的倍數

# Python Program
start = int(input("請輸入起始值 =>"))
end = int(input("請輸入終止值 =>"))
sum=0
count=0
for i in range(start, end+1):
if i%2==0 or i%3==0:
sum=sum+i
count=count+1
print("i="+str(i)+" sum="+str(sum))


print("個數 = " + str(count)+" 總和 = " + str(sum))

[Java]_ch15.5_TextArea元件事件處理





[Java]_ch15.5_TextArea元件事件處理



package YPU;



import java.awt.Button;

import java.awt.Checkbox;

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();

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

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

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

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

Button btn5=new Button("送出");

Button btn6=new Button("清除");

TextField tf=new TextField();

TextArea ta=new TextArea("結果顯示如下:\n",10,10,TextArea.SCROLLBARS_VERTICAL_ONLY);

Checkbox cb1=new Checkbox("游泳");

Checkbox cb2=new Checkbox("閱讀",true);

Checkbox cb3=new Checkbox("唱歌");

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);

ta.setBounds(50, 190, 200, 100);

ta.setEditable(false);

cb1.setBounds(50, 300, 70, 30);

cb2.setBounds(120, 300, 70, 30);

cb3.setBounds(190, 300, 70, 30);

add(btn4);

add(btn3);

add(btn1);

add(btn2);

add(btn5);

add(btn6);

add(lb);

add(tf);

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("");

}

}

}



package YPU;

import java.awt.Button;
import java.awt.Color;

public class A16 {

public static void main(String[] args) {
MyFrame frm=new MyFrame("國立交通大學",300,500,Color.PINK);

frm.btn1.addActionListener(frm);
frm.btn2.addActionListener(frm);
frm.btn3.addActionListener(frm);
frm.btn4.addActionListener(frm);
frm.btn5.addActionListener(frm);
frm.btn6.addActionListener(frm);
}

}

[Java]_ch15.4_TextField元件事件處理



[Java]_ch15.4_TextField元件事件處理



package YPU;



import java.awt.Button;

import java.awt.Color;

import java.awt.Font;

import java.awt.Frame;

import java.awt.Label;

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();

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

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

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

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

Button btn5=new Button("送出");

Button btn6=new Button("清除");

TextField tf=new TextField();

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);

add(btn4);

add(btn3);

add(btn1);

add(btn2);

add(btn5);

add(btn6);

add(lb);

add(tf);

}

@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());

}else if(btn==btn6){

lb.setText("");

tf.setText("");

}

}

}


package YPU;

import java.awt.Button;
import java.awt.Color;

public class A16 {

public static void main(String[] args) {
MyFrame frm=new MyFrame("國立交通大學",300,500,Color.PINK);

frm.btn1.addActionListener(frm);
frm.btn2.addActionListener(frm);
frm.btn3.addActionListener(frm);
frm.btn4.addActionListener(frm);
frm.btn5.addActionListener(frm);
frm.btn6.addActionListener(frm);
}

}

2018年11月18日 星期日

[Java認證題庫]_1Z0-804題庫詳解_6





[Java認證題庫]_1Z0-804題庫詳解_6



public void deleteDB(String id){

SQLiteDatabase db=getWritableDatabase();

db.execSQL("DELETE FROM teacher WHERE id='"+id+"';");

}





public void show5(View v){

String id=et1.getText().toString();

dbhp.deleteDB(id);

Toast.makeText(MainActivity.this, "刪除成功!", Toast.LENGTH_SHORT).show();

}

2018年11月17日 星期六

[Java認證題庫]_1Z0-804題庫詳解_5





[Java認證題庫]_1Z0-804題庫詳解_5



public void updateDB(String id,String name,String phoneNo,String address){

SQLiteDatabase db=getWritableDatabase();

db.execSQL("UPDATE teacher SET id='"+id+"',name='"+name+"',phoneNo='"+phoneNo+"',address='"+address+"' WHERE id='"+id+"';");

}





public void show4(View v){

String id=et1.getText().toString();

String name=et2.getText().toString();

String phoneNo=et3.getText().toString();

String address=et4.getText().toString();

dbhp.updateDB(id, name, phoneNo, address);

Toast.makeText(MainActivity.this, "更新成功!", Toast.LENGTH_SHORT).show();

}

[Java認證題庫]_1Z0-804題庫詳解_4





[Java認證題庫]_1Z0-804題庫詳解_4



package com.example.sqlitedb;



import java.util.ArrayList;



import android.content.Context;

import android.database.Cursor;

import android.database.sqlite.SQLiteDatabase;

import android.database.sqlite.SQLiteDatabase.CursorFactory;

import android.database.sqlite.SQLiteOpenHelper;



public class DBHP extends SQLiteOpenHelper{



public DBHP(Context context) {

super(context, "Gjun", null, 1);

}



@Override

public void onCreate(SQLiteDatabase db) {

db.execSQL("CREATE TABLE teacher(id TEXT,name TEXT,phoneNo TEXT,address TEXT);");

}



@Override

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

// TODO Auto-generated method stub



}



public void insertDB(){

SQLiteDatabase db=getWritableDatabase();

db.execSQL("INSERT INTO teacher VALUES('1','王小明','0912-345678','Taipei');");

db.execSQL("INSERT INTO teacher VALUES('2','陳大為','0952-261258','Taipei');");

db.execSQL("INSERT INTO teacher VALUES('3','張三','0918-349527','Hsinchu');");

db.execSQL("INSERT INTO teacher VALUES('4','李四','0934-598741','Taipei');");

db.execSQL("INSERT INTO teacher VALUES('5','王武','0909-125348','Hsinchu');");

}

public ArrayList<String> queryDB(){

SQLiteDatabase db=getWritableDatabase();

Cursor cursor=db.rawQuery("SELECT * FROM teacher;", null);

ArrayList<String> list=new ArrayList<String>();

while(cursor.moveToNext()){

String id=cursor.getString(0);

String name=cursor.getString(1);

String phoneNo=cursor.getString(2);

String address=cursor.getString(3);

String result=id+"\t"+name+"\t"+phoneNo+"\t"+address+"\n";

list.add(result);

}

return list;

}





}










package com.example.sqlitedb;



import java.util.ArrayList;



import android.app.Activity;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;

import android.widget.Toast;





public class MainActivity extends Activity {

Button btn1,btn2,btn3;

TextView tv1;

DBHP dbhp;

ArrayList<String> AL;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);  //載入版面

        btn1=(Button)findViewById(R.id.button1);

        btn2=(Button)findViewById(R.id.button2);

        btn3=(Button)findViewById(R.id.button3);

        tv1=(TextView)findViewById(R.id.textView1);

        if(dbhp==null)

        dbhp=new DBHP(MainActivity.this);

       

    }

   

    public void show1(View v){

    String msg=btn1.getText().toString();

    tv1.setText(msg);

    dbhp.insertDB();

    Toast.makeText(MainActivity.this, "新增成功!", Toast.LENGTH_SHORT).show();

    }

public void show2(View v){

String msg=btn2.getText().toString();

    tv1.setText(msg);

    AL=dbhp.queryDB();

    StringBuilder sb=new StringBuilder("查詢結果如下:\n");

    for(int i=0;i<AL.size();i++){

    sb.append(AL.get(i).toString());

    }

    tv1.setText(sb);

}

public void show3(View v){

String msg=btn3.getText().toString();

    tv1.setText("");

}







}




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