=======================================================================
/*
* ed1 = 匯率
* ed2 = 台幣
* 計算公式: 美金 = 台幣/匯率 => ed2/ed1
*
* 學習重點:
* 0) context.findViewById 即 EX203.this (ClassName.this)
* 1) setOn XXX Listener(),建立按鈕事件處理
* 2) new Widget.OnClickListener(){} (口訣:小括號 大括號,最末小括號後分號)
* 3) 滑鼠游標移置紅色文字線上 Add unimplemented methods
* 4) 數學換算,與資料型別轉換 in Java
* 5) 覆寫 Activity 的 onPause() 或 onStop(),將 匯率 儲存下來 (SharePreference物件)
* 6) 覆寫 Activity 的 onResume(),將 匯率 讀取出來 (SharePreference物件)
*/
package irdc.ex203;
import java.text.NumberFormat;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class EX203 extends Activity
{
private EditText ed1, ed2;
private Button btn1;
private TextView tv1;
public static final String MY_PREFS = "mSharedPreferences01";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ed1 = (EditText)EX203.this.findViewById(R.id.editText1); // 匯率
ed2 = (EditText)EX203.this.findViewById(R.id.editText2); // 台幣
btn1 = (Button)EX203.this.findViewById(R.id.button1);
tv1 = (TextView)EX203.this.findViewById(R.id.textView3);
ed1.setText("33.5");
ed2.setText("10000");
btn1.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View arg0)
{
// 按鈕事件,處理數學換算的語法
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits( 2 );
double d = Double.parseDouble(ed2.getText().toString()) / Double.parseDouble(ed1.getText().toString());
tv1.setText( EX203.this.getResources().getString(R.string.str3) + ":" + nf.format(d) );
}
});
}/*End: onCreate()*/
@Override
protected void onResume()
{
// TODO Auto-generated method stub
SharedPreferences mySharedPreferences = getSharedPreferences(MY_PREFS, Activity.MODE_PRIVATE);
// 第二個參數為 假若 "rate" 不存在,程式為第一次執行的話,要以什麼值取代
String previousInputRate = mySharedPreferences.getString("rate", "");
ed1.setText(previousInputRate);
super.onResume();
}
@Override
protected void onStop()
{
// TODO Auto-generated method stub
SharedPreferences mySharedPreferences = getSharedPreferences(MY_PREFS, Activity.MODE_PRIVATE);
SharedPreferences.Editor ed = mySharedPreferences.edit();
ed.putString("rate", ed1.getText().toString() );
ed.commit();
super.onStop();
}
}
================================================================================
作業
=================================================
rem Compiles and runs the DicomViewer example program
rem assumes ij.dir is in the current folder
rem SET PATH=C:\jdk1.2\bin;%PATH%
set CLASSPATH=ij.jar;.
javac -deprecation DicomViewer.java
java DicomViewer 000006.dcm
第十二週 (2011年5月6日)
===========================================================
import ij.*;
import java.applet.*;
import java.awt.*;
import ij.process.*;
/**Simple applet that demonstrates how to use ImageJ's ImageProcessor class.*/
public class IPDemo extends Applet {
String name;
Image img;
ImageProcessor ip = null;
public void init() {
setLayout(new BorderLayout());
Panel p = new Panel();
p.setLayout(new GridLayout(5, 3));
/*
p.add(new Button("Reset"));
p.add(new Button("Flip"));
p.add(new Button("Invert"));
p.add(new Button("Lighten"));
p.add(new Button("Darken"));
p.add(new Button("Rotate"));
p.add(new Button("Zoom In"));
p.add(new Button("Zoom Out"));
p.add(new Button("Threshsold"));
p.add(new Button("Smooth"));
p.add(new Button("Sharpen"));
p.add(new Button("Find Edges"));
p.add(new Button("Macro 1"));
p.add(new Button("Macro 2"));
p.add(new Button("Add Noise"));
*/
add("South", p);
name = getParameter("img");
img = getImage(getDocumentBase(), name);
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(img, 0);
try {tracker.waitForID(0);}
catch (InterruptedException e){}
if (name.endsWith("jpg"))
ip = new ColorProcessor(img);
else
ip = new ByteProcessor(img);
ip.snapshot();
ip.noise(20);
img = ip.createImage();
repaint();
}
public void update(Graphics g) {
paint(g);
}
public void paint(Graphics g) {
g.drawImage(img, 0, 0, this);
}
public boolean action(Event e, Object arg) {
if (e.target instanceof Button) {
String label = (String)arg;
if (label.equals("Reset"))
ip.reset();
else if (label.equals("Flip"))
ip.flipVertical();
else if (label.equals("Invert"))
ip.invert();
else if (label.equals("Lighten"))
ip.multiply(1.1);
else if (label.equals("Darken"))
ip.multiply(0.9);
else if (label.equals("Rotate"))
ip.rotate(30);
else if (label.equals("Zoom In"))
ip.scale(1.2, 1.2);
else if (label.equals("Zoom Out"))
ip.scale(0.8, 0.8);
else if (label.equals("Threshsold"))
ip.autoThreshold();
else if (label.equals("Smooth"))
ip.smooth();
else if (label.equals("Sharpen"))
ip.sharpen();
else if (label.equals("Find Edges"))
ip.findEdges();
else if (label.equals("Macro 1"))
macro1();
else if (label.equals("Macro 2"))
macro2();
else if (label.equals("Add Noise"))
ip.noise(20);
img = ip.createImage();
repaint();
return true;
}
return false;
}
void updateAndDraw() {
img.flush();
img = ip.createImage();
getGraphics().drawImage(img, 0, 0, this);
}
void macro1() {
for (int i=10; i<=360; i+=10) {
ip.reset();
ip.rotate(i);
updateAndDraw();
}
}
void macro2() {
double scale = 1, m = 1.2;
for (int i=0; i<20; i++) {
ip.reset();
scale *= m;
ip.scale(scale, scale);
updateAndDraw();
}
for (int i=0; i <20; i++) {
ip.reset();
scale /= m;
ip.scale(scale, scale);
updateAndDraw();
}
}
}
=====================================================
Chromosomes
java 繼承
例: extends Applet
java applet tutorial
====================================================================
import ij.*;
import java.applet.*;
import java.awt.*;
import ij.process.*;
/**Simple applet that demonstrates how to use ImageJ's ImageProcessor class.*/
public class IPDemo extends Applet {
String name;
Image img;
ImageProcessor ip = null;
public void init() {
setLayout(new BorderLayout());
Panel p = new Panel();
p.setLayout(new GridLayout(5, 3));
p.add(new Button("Reset"));
p.add(new Button("Flip"));
p.add(new Button("Invert"));
p.add(new Button("Lighten"));
p.add(new Button("Darken"));
p.add(new Button("Rotate"));
p.add(new Button("Zoom In"));
p.add(new Button("Zoom Out"));
p.add(new Button("Threshsold"));
p.add(new Button("Smooth"));
p.add(new Button("Sharpen"));
p.add(new Button("Find Edges"));
p.add(new Button("Macro 1"));
p.add(new Button("Macro 2"));
p.add(new Button("Add Noise"));
add("South", p);
name = getParameter("img");
img = getImage(getDocumentBase(), name);
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(img, 0);
try {tracker.waitForID(0);}
catch (InterruptedException e){}
if (name.endsWith("jpg"))
ip = new ColorProcessor(img);
else
ip = new ByteProcessor(img);
ip.snapshot();
}
public void update(Graphics g) {
paint(g);
}
public void paint(Graphics g) {
g.drawImage(img, 0, 0, this);
}
public boolean action(Event e, Object arg) {
if (e.target instanceof Button) {
String label = (String)arg;
if (label.equals("Reset"))
ip.reset();
else if (label.equals("Flip"))
ip.flipVertical();
else if (label.equals("Invert"))
ip.invert();
else if (label.equals("Lighten"))
ip.multiply(1.1);
else if (label.equals("Darken"))
ip.multiply(0.9);
else if (label.equals("Rotate"))
ip.rotate(30);
else if (label.equals("Zoom In"))
ip.scale(1.2, 1.2);
else if (label.equals("Zoom Out"))
ip.scale(0.8, 0.8);
else if (label.equals("Threshsold"))
ip.autoThreshold();
else if (label.equals("Smooth"))
ip.smooth();
else if (label.equals("Sharpen"))
ip.sharpen();
else if (label.equals("Find Edges"))
ip.findEdges();
else if (label.equals("Macro 1"))
macro1();
else if (label.equals("Macro 2"))
macro2();
else if (label.equals("Add Noise"))
ip.noise(20);
img = ip.createImage();
repaint();
return true;
}
return false;
}
void updateAndDraw() {
img.flush();
img = ip.createImage();
getGraphics().drawImage(img, 0, 0, this);
}
void macro1() {
for (int i=10; i<=360; i+=10) {
ip.reset();
ip.rotate(i);
updateAndDraw();
}
}
void macro2() {
double scale = 1, m = 1.2;
for (int i=0; i<20; i++) {
ip.reset();
scale *= m;
ip.scale(scale, scale);
updateAndDraw();
}
for (int i=0; i <20; i++) {
ip.reset();
scale /= m;
ip.scale(scale, scale);
updateAndDraw();
}
}
}
====================================================================
例: extends JFrame
====================================================================
import javax.swing.Timer;
import java.awt.event.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;
public class TimerDemo extends JFrame implements ActionListener {
static JTextField tbx1=new JTextField(10); // 建立1文字方塊物件
Timer t = new Timer(1000,this);
TimerDemo() {
t.start();
}
public static void main(String args[]) {
TimerDemo td = new TimerDemo();
GridLayout grid33= new GridLayout(3,3);
td.setLayout(grid33);
td.setSize(450,450);
JPanel p1 = new JPanel(grid33); //實作 panel 1
p1.add(tbx1);
td.add(p1); // 在視窗myfrm 內加入 panel 1
td.setVisible(true);
// create a dummy frame to keep the JVM running
// (for demonstation purpose)
// java.awt.Frame dummy = new java.awt.Frame();
// dummy.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == t) {
//System.out.println
// ("Being ticked " + Calendar.getInstance().getTime());
// Date mydate=Calendar.getInstance().getTime();
// tbx1.setText(mydate.toString());
tbx1.setText(" "+Calendar.getInstance().getTime());
}
}
}
==================================================================
第十一週 (2011年4月29日)
作業
封裝, 建構子
==================================================================
//主檔案
class Object1 {
public static void main(String args[]) {
Person1 p1 = new Person1("大雄", 50);
Person1 p2 = new Person1("胖虎", 80);
Person1 p3 = new Person1();
p1.checkWeight();
p2.checkWeight();
p2.weight = 68;
p1.checkWeight();
p2.checkWeight();
p3.checkWeight();
}
}
===================================================================
//另一類別檔
class Person1 {
String name;
int weight;
//建構子
Person1(String pName, int pWeight) {
name = pName;
weight = pWeight;
}
Person1() {
name = "0";
weight = 1;
}
void checkWeight() {
System.out.print(name+"體重 "+weight+" 公斤,");
if (weight < 70)
System.out.println("很苗條!");
else
System.out.println("很穩重!");
}
}
====================================================================
=====================================================
// with event
//Swing, JButton類別 有ActionListener
//Swing, JButton類別
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingEightPuzzleEvent 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 SwingEightPuzzleEvent()
{
}
public static void main(String args[])
{
SwingEightPuzzleEvent myfrm=new SwingEightPuzzleEvent();
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"};
//int rndno;
//String stringValue;
//stringValue=tbx1.getText();
//int intValue = Integer.parseInt(stringValue);
//System.out.println(intValue);
//labels[intValue].setText(stringValue);
/*
for (int j = 0; j < 9; j++)
{
//rndno=(int) (Math.random()*9);
//System.out.println(rndno);
//numbers[i]=String.valueOf(rndno);
//buttons[i].setLabel(numbers[i]);
}
*/
//tbx1.setText(numbers[0]);
//buttons[rndno].setBackground(Color.blue);
//labels[rndno].setText(numbers[0]);
}
}
====================================================
第十週 (2011年4月22日)
=========================================================================
// with event
//Swing, JButton類別 有ActionListener
//Swing, JButton類別
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingTestEvent 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[])
{
SwingTestEvent myfrm=new SwingTestEvent();
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 numbers[]=new String[101];
for (int i = 0; i <100; i++)
{
numbers[i]=String.valueOf(i);
}
int rndno;
String stringValue;
String tmp;
//stringValue=tbx1.getText();
//int intValue = Integer.parseInt(stringValue);
//System.out.println(intValue);
//labels[intValue].setText(stringValue);
for (int i = 0; i < 9; i++)
{
int j=8-i;
rndno=(int) (Math.random()*(j+1));
System.out.println(rndno);
tmp=numbers[j];
numbers[j]=numbers[rndno];
numbers[rndno]=tmp;
}
for (int i = 0; i < 9; i++)
{
System.out.println(numbers[i]);
buttons[i].setLabel(numbers[i]);
}
//tbx1.setText(numbers[0]);
//buttons[rndno].setBackground(Color.blue);
//labels[rndno].setText(numbers[0]);
}
}
=========================================================================
作業 bingo card
第八週 (2011年4月8日)
1. penel 1 with 3x3 JButton
2. penel 2 3x3 JLabel
3. penel 3 1x2 JTextField
4. penel 4 1 JButton
5. INPUT Number to 2 JTextFields
6. Change the colors of JButton and JLabel
====================================================================
// with event
//Swing, JButton類別 有ActionListener
//Swing, JButton類別
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingTestEvent 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[])
{
SwingTestEvent myfrm=new SwingTestEvent();
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 stringValue;
stringValue=tbx1.getText();
int intValue = Integer.parseInt(stringValue);
System.out.println(intValue);
buttons[intValue].setBackground(Color.blue);
labels[intValue].setText(stringValue);
}
}
=====================================================================
第七週 (2011年4月1日)
GOOGLE Search
keywords
1. java for
2. java Class FlowLayout
3. java Class ActionEvent
4. java Class Button
(1) AWT 換成Swing
1.全部物件加J
2.import swing
=======================================================================
//AWT, Button Class
import java.awt.*;
import java.awt.event.*;
public class AwtTest extends Frame
{
static Frame myfrm=new Frame("Button class"); // Java Class Frame
static Button btn1=new Button("Button 1"); // 建立1按鈕物件
static Button btn2=new Button("Button 2"); // 建立2按鈕物件
static TextField tbx1=new TextField("TextField 1"); // 建立1文字方塊物件
public static void main(String args[])
{
BorderLayout border=new BorderLayout();
myfrm.setLayout(border);
myfrm.setSize(250,150);
myfrm.add(btn1, border.EAST); // 在視窗內加入按鈕1
myfrm.add(btn2, border.CENTER); // 在視窗內加入按鈕2
myfrm.add(tbx1, border.WEST); // 在視窗內加入按鈕2
myfrm.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
int rn;
rn=(int) (Math.random()*49) ;
System.out.println(rn );
}
}
======================================================================
GridLayout border= new GridLayout(0,2);
==============================================================
(2) 利用 for 和 物件陣列產生多個按鈕並用GridLayout排列
==============================================================
//AWT, JButton類別
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//import javax.swing.JButton;
public class SwingTest extends JFrame
{
static JFrame myfrm=new JFrame("JButton class"); // Java Class JFrame
static JTextField tbx1=new JTextField("JTextField 1"); // 建立1文字方塊物件
/*
// goes on to 26JButtons[] buttons = new JButton(numbers.length)
// create instance of each buttonfor (int i = 0; i < numbers.length; i++){buttons[i] = new JButton(numbers[i]);
// create buttons } String[] numbers = {"0", "1", "2", "3", 4", "5", "6", "7", "8", "9"};
// goes on to 26
*/
public static void main(String args[])
{
JButton buttons[]=new JButton[10];
String numbers[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8"};
//GridBagLayout border=new GridBagLayout ();
GridLayout border= new GridLayout(3,3);
JPanel p = new JPanel(border); //PREFERRED!
for (int i = 0; i < numbers.length; i++)
{
buttons[i] = new JButton(numbers[i]); // create buttons
p.add(buttons[i], border); // 在視窗內加入按鈕1
}
myfrm.add(p);
myfrm.setLayout(border);
myfrm.setSize(250,150);
// create instance of each button
//FlowLayout flow= new FlowLayout();
// myfrm.add(tbx1, flow); // 在視窗內加入文字方塊
myfrm.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
int rn;
rn=(int) (Math.random()*49) ;
System.out.println(rn );
}
}
==============================================================
================================================================
Google search
=================================================================
// with event
//Swing, JButton類別 有ActionListener
//Swing, JButton類別
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingTestEvent 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];
public static void main(String args[])
{
SwingTestEvent myfrm=new SwingTestEvent();
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 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 stringValue;
stringValue=tbx1.getText();
int intValue = Integer.parseInt(stringValue);
System.out.println(intValue);
buttons[intValue].setBackground(Color.blue);
}
}
================================================================
(3) ActionEvent e, 利用e.getSource()
JButton btn=new JButton();
btn=e.getSource()
if (btn=btn1)
{
btn1.setBackground(Color.white);
}
if (btn=btn2)
{
btn2.setBackground(Color.green);
}
(4) 將main()變簡單,將藍色字移送到
1. 建構子方法
2. creatGUI()
public static void main(String args[])
{
BorderLayout border=new BorderLayout();
myfrm.setLayout(border);
myfrm.setSize(200,150);
myfrm.add(btn1, border.EAST); // 在視窗內加入按鈕1
myfrm.add(btn2, border.Center); // 在視窗內加入按鈕2
myfrm.add(tbx1, border.WEST); // 在視窗內加入按鈕2
myfrm.setVisible(true);
}
第六週 (2011年3月25日)
隨堂測驗
大樂透開獎號碼 7 個放入7個
文字方塊(當一個按鈕被按下時)
書到用時方恨少
只要Google 沒煩惱
Java 書當字典就好 (The java textbooks are only used as dictionaries )
Ex
Google
keywords
1. Basic Class Template of AWT
======================================================================
//AWT, Button類別
import java.awt.*;
public class AwtTest
{
static Frame myfrm=new Frame("Button class"); // Java Class Frame
static Button btn1=new Button("Button 1"); // 建立1按鈕物件
static Button btn2=new Button("Button 2"); // 建立2按鈕物件
static TextField tbx1=new TextField("TextField 1"); // 建立1文字方塊物件
public static void main(String args[])
{
BorderLayout border=new BorderLayout();
myfrm.setLayout(border);
myfrm.setSize(200,150);
myfrm.add(btn1, border.EAST); // 在視窗內加入按鈕1
myfrm.add(btn2, border.Center); // 在視窗內加入按鈕2
myfrm.add(tbx1, border.WEST); // 在視窗內加入按鈕2
myfrm.setVisible(true);
}
}
===================================================================
===================================================================
// with event
//AWT, Button類別 有ActionListener
//AWT, Button類別
import java.awt.*;
import java.awt.event.*;
public class AwtTestEvent extends Frame implements ActionListener
{
//static Frame myfrm=new Frame("Button class"); // Java Class Frame
//static AwtTestEvent myfrm=new AwtTestEvent("Frame 1 "); // Java Class Frame
static Button btn1=new Button("Button 1"); // 建立1按鈕物件
static Button btn2=new Button("Button 2"); // 建立2按鈕物件
static TextField tbx1=new TextField("TextField 1"); // 建立1文字方塊物件
public static void main(String args[])
{
AwtTestEvent myfrm=new AwtTestEvent();
BorderLayout border=new BorderLayout();
myfrm.setLayout(border);
myfrm.setSize(250,150);
btn1.addActionListener(myfrm);
myfrm.add(btn1, border.WEST); // 在視窗內加入按鈕1
myfrm.add(btn2, border.CENTER); // 在視窗內加入按鈕2
myfrm.add(tbx1, border.EAST); // 在視窗內加入文字方塊1;
}
public void actionPerformed(ActionEvent e)
{
int rn;
rn=(int) (Math.random()*49) ;
String stringValue = Integer.toString(rn);
System.out.println(rn );
tbx1.setText( stringValue );
}
}
===================================================================
Why these two commends?
import java.awt.*;
import java.awt.event.*;
Because
===================================================================
2. Basic Class Template of Swing
===================================================================
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ButtonText extends JFrame implements ActionListener {
public static void main(String[] args) {
ButtonText test = new ButtonText();
}
ButtonText( )
{
JButton mybutton1 = new JButton("Submit");
JButton mybutton2 = new JButton("Submit");
mybutton1.addActionListener(this);
//Container contentPane = frame.getContentPane();
//contentPane.add(mybutton1);
//contentPane.add(mybutton2);
getContentPane().add(mybutton1);
getContentPane().add(mybutton2);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(200,100);
setVisible(true);
// System.out.println("wait");
//System.exit(0);
}
public void actionPerformed(ActionEvent e)
{
int rn;
rn=(int) (Math.random()*49) ;
System.out.println(rn );
}
}
======================================================================
// app17_8, Button類別
import java.awt.*;
public class app17_8
{
static Frame frm=new Frame("Button class");
static Button btn=new Button("Push Me!!"); // 建立按鈕物件
public static void main(String args[])
{
frm.setSize(200,150);
frm.add(btn); // 在視窗內加入按鈕
frm.setVisible(true);
}
}
==============================================================
// app19_8, 對話方塊的實例應用
import java.awt.*;
import java.awt.event.*;
public class app19_8 extends Frame implements ActionListener
{
static app19_8 frm=new app19_8();
static Dialog dlg=new Dialog(frm); // 建立Dialog物件dlg
static Button Close_btn=new Button("Close"); // Close 按鈕
static Button Cancel_btn=new Button("Cancel"); // Cancel按鈕
static WinLis wlis=new WinLis(); // 建立傾聽者物件wlis
public static void main(String args[])
{
frm.setTitle("Dialog Demo");
frm.setSize(200,150);
dlg.setTitle("Are you sure?"); // 設定對話方塊的標題
dlg.setSize(140,100); // 設定對話方塊的大小
dlg.setLayout(new FlowLayout(FlowLayout.CENTER,5,30));
dlg.add(Close_btn); // 將Close_btn加入對話方塊中
dlg.add(Cancel_btn); // 將Cancel_btn加入對話方塊中
Cancel_btn.addActionListener(frm); // 設定Cancel_btn的傾聽者
Close_btn.addActionListener(frm); // 設定Close_btn的傾聽者
frm.addWindowListener(wlis); // 設定frm的傾聽者
frm.setVisible(true);
}
static class WinLis extends WindowAdapter
{
public void windowClosing(WindowEvent e) // 按下視窗關閉鈕時
{
dlg.setLocation(80,30); // 設定對話方塊的位置
dlg.show(); // 顯示對話方塊
}
}
public void actionPerformed(ActionEvent e) // 按下對話方塊上的按鈕時
{
Button btn=(Button) e.getSource(); // 取得被按下的按鈕
if(btn==Close_btn) // 如果是Close按鈕被按下
{
dlg.dispose(); // 關閉對話方塊
frm.dispose(); // 關閉視窗
}
else if (btn==Cancel_btn) // 如果是Cancel按鈕被按下
dlg.hide(); // 隱藏對話方塊
}
}
====================================================================
Google keywords
JAVA CLASS JTEXTBOX
大樂透開獎號碼
java random
第五週 (2011年3月18日)
Please google the keywords
VB event and
Java event.
http://www.idevelopment.info/data/Programming/java/swing/AWTExample.java
================================================================
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ButtonText extends JFrame implements ActionListener {
public static void main(String[] args) {
ButtonText test = new ButtonText();
}
ButtonText( )
{
JButton mybutton1 = new JButton("Submit");
JButton mybutton2 = new JButton("Submit");
mybutton1.addActionListener(this);
//Container contentPane = frame.getContentPane();
//contentPane.add(mybutton1);
//contentPane.add(mybutton2);
getContentPane().add(mybutton1);
getContentPane().add(mybutton2);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(200,100);
setVisible(true);
// System.out.println("wait");
//System.exit(0);
}
public void actionPerformed(ActionEvent e)
{
int rn;
rn=(int) (Math.random()*49) ;
System.out.println(rn );
}
}
=====================================================================
java layout
=============================================================
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ButtonText extends JFrame implements ActionListener {
public static void main(String[] args) {
ButtonText test = new ButtonText();
}
ButtonText( )
{
// JButton mybutton1 = new JButton("Submit");
// JButton mybutton2 = new JButton("Submit");
//mybutton1.addActionListener(this);
//getContentPane().add(mybutton1);
//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/*
myframe.add(mybutton1);
myframe.add(mybutton2);
myframe.setLayout(new FlowLayout());
myframe.setSize(200,100);
myframe.setVisible(true);
// System.out.println("wait");
//System.exit(0);
*/
}
public void actionPerformed(ActionEvent e)
{
}
}
=============================================================
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ButtonText{
public static void main(String[] args) {
ButtonText test = new ButtonText();
}
ButtonText( )
{
Frame myframe=new Frame("Button Frame");
Button mybutton1 = new Button("Submit");
Button mybutton2 = new Button("Submit");
mybutton1.addActionListener(this);
myframe.add(mybutton1);
myframe.add(mybutton2);
myframe.setLayout(new FlowLayout());
myframe.setSize(200,100);
myframe.setVisible(true);
/*
// System.out.println("wait");
//System.exit(0);
*/
}
}
import java.awt.*;
import java.awt.event.*;
public class ButtonText {
public static void main(String[] args) {
Frame frame=new Frame("Button Frame");
Button button = new Button("Submit");
frame.add(button);
frame.setLayout(new FlowLayout());
frame.setSize(200,100);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
}
第四週 (2011年3月11日)
java button
import java.awt.*;
Frame frame=new Frame("Button Frame");
Button button = new Button("Submit");
frame.add(button);
frame.setVisible(true);
An example to show how to use textboxs.
Please google the keywords
VB event and
Java event.
第四週 (2011年3月4日)
Vb button
It is easy to put a button on a VB form.
java button
Why it is so diffcult to put a button on a JAVA frame?
Java Class button is easy to be extended to make your own button class, but VB is not so open for you to do your own buton.
Java double to int and Other Conversions
Please google keywords "Java Button" to find another example!
To know how to put a button on a frame!
The most important commands are as follows.
import java.awt.*;
Frame frame=new Frame("Button Frame");
Button button = new Button("Submit");
frame.add(button);
frame.setVisible(true);
How to Create Button on Frame,Create a Button in Java,Code to ...
The comment "import java.awt.*;" is related to the file src.zip.
How to Use Buttons, Check Boxes, and Radio Buttons
java class button
隨堂練習
放上一個
Check Boxes
第三週 (2011年3月4日)
隨堂練習
輸入四個數字,前兩個數字代表列向量,後兩個數字代表行向量,
輸出矩陣相乘計算的結果
Java String int
Java String
java return type required
2. 第二週 (2011年2月25日)
課堂作業
1. 第一週 (2011年2月18日)
考試
1.
製作自己的學習部落格blog並
回答下列問題
1.為何why要選修這門課?(動機)
2.希忘從這門課獲得那些知識?(目標)
3.我要如何修習這一門課?(態度與方法)
who, when, where
回答問題: 請問您畢業後,如果找不到工作,您會花錢去上 "Java程式設計實力精修班" 課程嗎? 為甚麼?請您說明原因,100個字以上。
參考網址
java 下載 與 安裝 執行 hellow
http://tccnchsu.blogspot.com/2010/02/java.html
google blog css
====================================================================
奉獻與感動
富爸爸富小孩 為什麼我要學系這些課程
激勵演講
====================================================================
同學的部落格
學號 姓名 網址
D9735878 劉哲宇
http://nicejava-java.blogspot.com/
D9785801 張鶴櫪
http://d9785801.blogspot.com/
D9735780 劉華瑋
http://alan5820.blogspot.com/
D9735851 彭凱頎
http://bleakmirage.blogspot.com/
D9735818 蕭銘宏
http://sean80221123.blogspot.com/
D9735937 汪筠捷
http://tony11206.blogspot.com/
D9785934李虹毅
http://responsible10.blogspot.com/
D9785862王志明
http://j12345741258.blogspot.com/
D9790384吳育全
http://gary1424.blogspot.com/
D9785760莊桂軒
http://www.wretch.cc/blog/onlyyazu
D9785917 謝翰嶔 http://jack10169.blogspot.com/
D9785790 詹宏茂
http://rfdg8546.blogspot.com/
D9726464 吳昱鋒
http://www.wretch.cc/blog/jk15478
D9642313 黃俊隆
http://loganlearninglog.blogspot.com/
D9785578 汪聖凱
http://www.wretch.cc/blog/bho2980112sa
D9726481 黃建志
http://s10024157.blogspot.com/
D9681524 郭政霖
http://www.wretch.cc/blog/charlie0525
d9643522 林哲瑜
http://www.wretch.cc/blog/warbobobo
d9726327 陳政瑜
http://www.wretch.cc/blog/jc1113
D9681200 廖崇閔
http://www.wretch.cc/blog/kobe770201
D9681483 孫子策
http://www.wretch.cc/blog/qoopooguy
D9681609劉駿賢
http://www.wretch.cc/blog/chi2980119nn
D9735970顏三峻
http://www.wretch.cc/blog/ysj78113
D9681139林俊德
http://www.wretch.cc/blog/YFU385
D9786107郭榮浩
http://swsy10301.blogspot.com/
D9681452吳智閔
http://tw.myblog.yahoo.com/jw!zJVuSkGFBQBrxXRQDPohWf1w5209
D9681231劉晏君
http://www.wretch.cc/blog/postre
D9642433柯志陽
http://www.wretch.cc/blog/s2463715s
D9681316郭宗賢
http://kuohome123.blogspot.com/