2011年6月16日 星期四

執行別人的程式

參考網址http://sukelven.pixnet.net/blog/post/53825727
package fcu.edu;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Menu extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String[] vData = {"星期一","星期二","星期三","星期四","星期五","星期六","星期日",};
        ListView lv = new ListView(this);                   
        lv.setChoiceMode( ListView.CHOICE_MODE_MULTIPLE ); 
        ArrayAdapter arrayAdapter2 = new ArrayAdapter(this,
        android.R.layout.simple_list_item_multiple_choice, vData);
  ArrayAdapter arrayAdapter = arrayAdapter2;
  ArrayAdapter vArrayData = arrayAdapter;
        lv.setAdapter( vArrayData );       
        setContentView(lv);   
    }
}

JAVA期末自製

package fcu.edu;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class add extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final EditText fromNumber = (EditText)this.findViewById( R.id.fromNumber );
        final EditText toNumber   = (EditText)this.findViewById( R.id.toNumber );
        Button   equals     = (Button)this.findViewById( R.id.equals );
        final TextView totalValue = (TextView)this.findViewById( R.id.totalValue );
        equals.setOnClickListener(new OnClickListener
        (){
         public void onClick(View v) {              
          try {                   
           int fromNum = Integer.parseInt(fromNumber.getText().toString());
           int toNum   = Integer.parseInt(toNumber.getText().toString());
           totalValue.setText( fromNum + toNum + "" );
          }catch( Exception e )
          {                  
           totalValue.setText("請輸入正確的數字型態 !! ");
          }
         }
       
       
        });
    }
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
<EditText    android:id="@+id/fromNumber"
        android:layout_width="149px"
        android:layout_height="wrap_content"  
        android:text="" 
        android:textSize="18sp"
        android:layout_x="10px" 
        android:layout_y="32px"   
    />
    <TextView    android:id="@+id/widgetAdd" 
      android:layout_width="wrap_content"  
       android:layout_height="wrap_content" 
         android:text="+"
            android:textSize="20sp"  
             android:layout_x="10px"   
    android:layout_y="82px"   
    />
<EditText    android:id="@+id/toNumber"
   android:layout_width="150px" 
     android:layout_height="wrap_content"
  android:text=""  
   android:textSize="18sp" 
     android:layout_x="10px"  
      android:layout_y="112px" 
        />
<Button    android:id="@+id/equals"  
 android:layout_width="wrap_content"  
  android:layout_height="wrap_content" 
    android:text="="  
     android:textSize="20sp"
        android:layout_x="10px" 
          android:layout_y="172px"
             />
<TextView    android:id="@+id/totalValue"  
 android:layout_width="wrap_content"  
  android:layout_height="wrap_content"  
   android:textSize="30sp" 
     android:textColor="#ffffff00"
        android:layout_x="10px" 
          android:layout_y="232px" 
            />
   
</LinearLayout>

輸入正確數字會相加


不是輸入數字會要求重新輸入

2011年4月1日 星期五

Get text

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class AWT extends JFrame implements ActionListener
{
static JFrame myfrm=new JFrame("Button class");
static JTextField tex1=new JTextField("TextField");
static JButton but1=new JButton("點擊"); 
//static AWT myfrm=new AWT("Frame 1 ");
//static JTextField tbx1=new JTextField("JTextField");

public static void main(String args[])
{
AWT myfrm=new AWT();
GridLayout f = new GridLayout(3,3);
GridLayout f1 = new GridLayout(1,2);
String numbers[]  = {"0", "1", "2", "3", "4", "5", "6", "7", "8"};
JButton buttons[]=new JButton[10];
JPanel p = new JPanel(f);
for (int i = 0; i < numbers.length; i++)
{
buttons[i] = new JButton(numbers[i]); // create buttons
p.add(buttons[i], f); // 在視窗內加入按鈕1
}

JPanel k = new JPanel(f1);
k.add(tex1);
k.add(but1);
myfrm.setLayout(f);
myfrm.setSize(500,300);
but1.addActionListener(myfrm);
myfrm.add(p,f);
myfrm.add(k,f); 
myfrm.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String stringValue;
stringValue=tex1.getText();
int intValue = Integer.parseInt(stringValue);
System.out.println(stringValue);

}
}

2011年3月25日 星期五

大樂透PART2

  程式設計工藝大師
import java.awt.*;
import java.awt.event.*;
public class AWT extends Frame implements ActionListener
{
//static Frame myfrm=new Frame("Button class");
//static AWT myfrm=new AWT("Frame 1 ");
static Button btn1=new Button("Button 1");
static TextField tbx1=new TextField("TextField");
static TextField tbx2=new TextField("TextField");
static TextField tbx3=new TextField("TextField");
static TextField tbx4=new TextField("TextField");
static TextField tbx5=new TextField("TextField");
static TextField tbx6=new TextField("TextField");
static TextField tbx7=new TextField("TextField"); 
public static void main(String args[])
{
AWT myfrm=new AWT();
FlowLayout f=new FlowLayout();
myfrm.setLayout(f);
myfrm.setSize(250,150);
btn1.addActionListener(myfrm);
myfrm.add(btn1,f);
myfrm.add(tbx1,f);
myfrm.add(tbx2,f);
myfrm.add(tbx3,f);
myfrm.add(tbx4,f);
myfrm.add(tbx5,f);
myfrm.add(tbx6,f);
myfrm.add(tbx7,f);
myfrm.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
int rn,a,b,c,d,k,f;
rn=(int) (Math.random()*49) ;
a=(int) (Math.random()*49) ;
b=(int) (Math.random()*49) ;
c=(int) (Math.random()*49) ;
d=(int) (Math.random()*49) ;
k=(int) (Math.random()*49) ;
f=(int) (Math.random()*49) ;
String stringValue = Integer.toString(rn);
String a1 = Integer.toString(a);
String a2 = Integer.toString(b);
String a3 = Integer.toString(c);
String a4 = Integer.toString(d);
String a5 = Integer.toString(k);
String a6 = Integer.toString(f);
System.out.println(rn );
System.out.println(a );
System.out.println(b );
System.out.println(c );
System.out.println(d );
System.out.println(k );
System.out.println(f );
tbx1.setText( stringValue );
tbx2.setText( a1 );
tbx3.setText( a2 );
tbx4.setText( a3 );
tbx5.setText( a4 );
tbx6.setText( a5 );
tbx7.setText( a6 );

}
}



2011年3月18日 星期五

大樂透



import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ButtonText extends JFrame implements ActionListener {
 
    public static void main(String[] args) {
    ButtonText test = new ButtonText();
}
ButtonText()
{
    JFrame frame=new JFrame("Button Frame");
   
    JButton button1 = new JButton("Submit");
    JButton button2 = new JButton("Submit");
   
button1.addActionListener(this);
button2.addActionListener(this);
    
getContentPane().add(button1); 
getContentPane().add(button2);
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setSize(200,100);
    setVisible(true);
  
    
}

public void actionPerformed(ActionEvent e)
{
int ra,rb,rc,rd,re,rf,rg;
ra=(int)(Math.random()*49+1);
rb=(int)(Math.random()*49+1);
rc=(int)(Math.random()*49+1);
rd=(int)(Math.random()*49+1);
re=(int)(Math.random()*49+1);
rf=(int)(Math.random()*49+1);
rg=(int)(Math.random()*49+1);
System.out.println(ra);
System.out.println(rb);
System.out.println(rc);
System.out.println(rd);
System.out.println(re);
System.out.println(rf);
System.out.println(rg);

}

2011年3月11日 星期五

checkbox


Frame frame = new Frame("Check Box Frame");
   
   
    //frame.setSize(400, 400);
   
    //frame.setVisible(true);
 
   
       public static void main(String[] args) { 
    // Work2 test = new Work2();
    // getContentPane().add(mybutton);
  
       Frame frame=new Frame("Button Frame");      
       frame.setSize(800,600);
       frame.setVisible(true);
       Button button = new Button("Submit");
       frame.add(button);
       frame.setLayout(new FlowLayout());
       button.setSize(500,200);
       button.setVisible(true);
       Checkbox chk = new Checkbox("This is the Check Box");
       frame.add(chk);
       chk.setSize(30,30);
       chk.setVisible(true);
      

      frame.addWindowListener(new WindowAdapter(){
      public void windowClosing(WindowEvent e){
        System.exit(0);
      }
    });


       } 
   
       // 用建構方法來建立元件、將元件加入視窗、顯示視窗 
        public Work2() { 
        /* setTitle("Listener 示範");    // 設定視窗標題 
        Button mybutton = new Button("換個標題"); 
   
         // 通知按鈕物件:本物件要當傾聽者 
        mybutton.addActionListener(this); 
   
         getContentPane().add(mybutton); 
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
         setSize(SizeW,SizeH); 
         setVisible(true);*/ 
       } 
   
       public void actionPerformed(ActionEvent e) { 
           SizeW = (int)(SizeW * 2); 
           SizeH = (int)(SizeH * 2);   
          //setSize(SizeW,SizeH); 
       } 
   
 }

2011年3月3日 星期四