2011年6月16日 星期四

android checkbox


package com.android.demo.checkbox;

import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;

public class Checkbox extends Activity {


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findview();
checkboxlistener();
}
private CheckBox malecheckbox;
private CheckBox femalecheckbox;
private void findview()
{
malecheckbox=(CheckBox)findViewById(R.id.male);
femalecheckbox=(CheckBox)findViewById(R.id.female);
}
private void checkboxlistener()
{
malecheckbox.setOnCheckedChangeListener(listener);
femalecheckbox.setOnCheckedChangeListener(listener);
}
private CheckBox.OnCheckedChangeListener listener=new CheckBox.OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked)
{
if(malecheckbox.isChecked()==true)
{
Toast.makeText(Checkbox.this,"哦,你是個男生",Toast.LENGTH_LONG).show();
}
if(femalecheckbox.isChecked()==true)
{
Toast.makeText(Checkbox.this,"哦,妳是個女生",Toast.LENGTH_LONG).show();
}
}};
}


網路下載下來的
http://kkwinds.blogspot.com/2010/06/android-checkbox.html <----這是網址

2011年4月28日 星期四

JAVA-19

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test8 extends JFrame implements ActionListener
{
//static JFrame myfrm=new JFrame("JButton class"); // Java Class JFrame
//static AwtTestEvent myfrm=new AwtTestEvent("JFrame 1 "); // Java Class JFrame
static JTextField tbx1=new JTextField(2);
static JButton buttons[]=new JButton[25];
static JLabel labels[]=new JLabel [25];
public static void main(String args[])
{
Test8 myfrm=new Test8();

String numbers[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24"};
FlowLayout flow=new FlowLayout();
GridLayout grid12= new GridLayout(1,2);
GridLayout grid55= new GridLayout(5,5);
myfrm.setLayout(grid12);
myfrm.setSize(450,450);
JPanel p1 = new JPanel(grid55);
for (int i = 0; i < 25; i++)
{
buttons[i] = new JButton(numbers[i]);
p1.add(buttons[i], grid55);
}

myfrm.add(p1);
JPanel p3 = new JPanel(grid55);
for (int i = 0; i < numbers.length; i++)
{
labels[i] = new JLabel();
p3.add(labels[i], grid55);
}
myfrm.add(p3);
JPanel p2 = new JPanel(flow);
JButton btn1=new JButton("JButton 1");
btn1.addActionListener(myfrm);
p2.add(tbx1);
p2.add(btn1);
myfrm.add(p2);
myfrm.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String numbers[]= {"0", "1", "2", "3", "4", "5", "6", "7", "8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24"};
int ran;


for (int i=0 ; i < 25 ;i++)
{
ran = (int) (Math.random()*25);

String tmp = numbers[ran];
numbers[ran] = numbers[i];
numbers[i] = tmp;
}

for (int j=0 ; j < 25 ;j++)
{
buttons[j].setLabel(numbers[j]);
}


}
}

2011年4月21日 星期四

12321213

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test8 extends JFrame implements ActionListener
{
//static JFrame myfrm=new JFrame("JButton class"); // Java Class JFrame
//static AwtTestEvent myfrm=new AwtTestEvent("JFrame 1 "); // Java Class JFrame
static JTextField tbx1=new JTextField(2); // 建立1文字方塊物件
static JButton buttons[]=new JButton[10];
static JLabel labels[]=new JLabel [10];
public static void main(String args[])
{
Test8 myfrm=new Test8();

String numbers[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8"};
FlowLayout flow=new FlowLayout();
GridLayout grid12= new GridLayout(1,2);
GridLayout grid33= new GridLayout(3,3);
myfrm.setLayout(grid12);
myfrm.setSize(450,450);
JPanel p1 = new JPanel(grid33); //實作 panel 1
for (int i = 0; i < numbers.length; i++)
{
buttons[i] = new JButton(numbers[i]); // create buttons
p1.add(buttons[i], grid33); // 在 panel 1內加入按鈕陣列
}

myfrm.add(p1); // 在視窗myfrm 內加入 panel 1
JPanel p3 = new JPanel(grid33); //實作 panel 3
for (int i = 0; i < numbers.length; i++)
{
labels[i] = new JLabel(); // create labels
p3.add(labels[i], grid33); // 在 panel 1內加入按鈕陣列
}
myfrm.add(p3); // 在視窗myfrm 內加入 panel 3

JPanel p2 = new JPanel(flow); //實作 panel 2
JButton btn1=new JButton("JButton 1"); // 建立按鈕物件 btn1
btn1.addActionListener(myfrm);
p2.add(tbx1); // 在 panel 2內加入文字方塊
p2.add(btn1); // 在 panel 2內加入按鈕

myfrm.add(p2); // 在視窗myfrm 內加入 panel 2


myfrm.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String numbers[]={"0","1","2","3","4","5","6","7","8"};
String stringValue;
//stringValue=tbx1.getText();
//int intValue = Integer.parseInt(stringValue);

int rndno = (int) (Math.random()*9);
System.out.println(rndno);
numbers[0] = String.valueOf(rndno);
tbx1.setText(numbers[0]);
buttons[rndno].setBackground(Color.blue);
labels[rndno].setText(numbers[0]);
System.out.println(rndno);
}
}

2011年4月7日 星期四

JAVA-18

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

public class Test6 extends JFrame implements ActionListener
{
static TextField tbx1 = new TextField(2);
static JButton buttons[] = new JButton[10];
public static void main(String args[])
{
Test6 frame = new Test6();
FlowLayout flow = new FlowLayout();
String numbers[]={"0","1","2","3","4","5","6","7","8"};
GridLayout border1 = new GridLayout(3,3);
GridLayout border2 = new GridLayout(0,2);
frame.setLayout(border2);
frame.setSize(300,300);
JPanel p1= new JPanel(border1);
for (int i=0; i{
buttons[i] = new JButton(numbers[i]);
p1.add(buttons[i],border1);
}
frame.add(p1);
JPanel p2 = new JPanel(flow);
JButton button = new JButton("JButton 1");
button.addActionListener(frame);
p2.add(tbx1);
p2.add(button);
frame.add(p2);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String stringValue;
stringValue = tbx1.getText();
int intValue = Integer.parseInt(stringValue);
System.out.println(intValue);
buttons[intValue].setBackground(Color.black);
}
}

2011年4月6日 星期三

JAVA-17

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

public class Test4 extends JFrame implements ActionListener
{

static JFrame frame=new JFrame("JButton class");
static JButton button=new JButton("JButton 1");
static TextField tbx1=new TextField("TextField 1");

public static void main(String args[])
{
String numbers[]={"0","1","2","3","4","5","6","7","8"};
JButton buttons[]=new JButton[9];
GridLayout border1=new GridLayout(3,3);
GridLayout border2=new GridLayout(0,2);
JPanel p1 = new JPanel(border1);
JPanel p2 = new JPanel(border2);

for (int i = 0; i < numbers.length; i++)
{
buttons[i] = new JButton(numbers[i]);
p1.add(buttons[i], border1);
}

frame.setLayout(border1);
frame.setSize(400,400);
frame.add(tbx1);

frame.add(p1);
frame.add(p2);
//button.addActionListener(frame);
p2.add(button);
p2.add(tbx1);
frame.add(p2);
frame.setVisible(true);

}
public void actionPerformed(ActionEvent e)
{
tbx1.setText("");
String stringValue;
stringValue=tbx1.getText();
System.out.println(stringValue);
}
}

2011年4月1日 星期五

2011年3月25日 星期五

JAVA-15

這是今天作業的程式碼
一開始遇到很多問題
後來經過問同學後才知道
因為使用了flowlayout之後
之前的borderlayout就沒有再用了
要用掉
用掉之後因為沒有borderlayout
所以方位也不用了

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

public class Test3 extends Frame implements ActionListener
{

static Frame frame=new Frame("Button class");
static Button button1=new Button("Button 1");
//static Button button2=new Button("Button 2");
static TextField tbx1=new TextField("TextField1");
static TextField tbx2=new TextField("TextField2");
static TextField tbx3=new TextField("TextField3");
static TextField tbx4=new TextField("TextField4");
static TextField tbx5=new TextField("TextField5");
static TextField tbx6=new TextField("TextField6");
static TextField tbx7=new TextField("TextField7");
static TextField tbx8=new TextField("TextField8");



public static void main(String args[])
{
Test3 frame=new Test3();
FlowLayout border=new FlowLayout();
frame.setLayout(border);
frame.setSize(400,400);
button1.addActionListener(frame);
frame.add(button1);
//frame.add(button2, border);
frame.add(tbx1, border);
frame.add(tbx2);
frame.add(tbx3);
frame.add(tbx4);
frame.add(tbx5);
frame.add(tbx6);
frame.add(tbx7);
frame.add(tbx8);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
int a;
int b;
int c;
int d;
int f;
int g;
int h;
a=(int) ((Math.random()*49)+1);
String stringValue1=Integer.toString(a);
b=(int) ((Math.random()*49)+1);
String stringValue2=Integer.toString(b);
c=(int) ((Math.random()*49)+1);
String stringValue3=Integer.toString(c);
d=(int) ((Math.random()*49)+1);
String stringValue4=Integer.toString(d);
f=(int) ((Math.random()*49)+1);
String stringValue5=Integer.toString(f);
g=(int) ((Math.random()*49)+1);
String stringValue6=Integer.toString(g);
h=(int) ((Math.random()*49)+1);
String stringValue7=Integer.toString(h);
System.out.println(a);
tbx2.setText(stringValue1);
System.out.println(b);
tbx3.setText(stringValue2);
System.out.println(c);
tbx4.setText(stringValue3);
System.out.println(d);
tbx5.setText(stringValue4);
System.out.println(f);
tbx6.setText(stringValue5);
System.out.println(g);
tbx7.setText(stringValue6);
System.out.println(h);
tbx8.setText(stringValue7);
}
}