2012年6月1日 星期五

Java Basic(64)-JcheckBox


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


public class Gui extends JFrame{
private JTextField tf;
private JCheckBox boldbox;
private JCheckBox italicbox;
public Gui(){
super("the title");
setLayout(new FlowLayout());
tf = new JTextField("This is a sentence", 20);
tf.setFont(new Font("Serif", Font.PLAIN, 14));
add(tf);
boldbox = new JCheckBox("bold");
italicbox = new JCheckBox("italic");
add(boldbox);
add(italicbox);
HandlerClass handler = new HandlerClass();
boldbox.addItemListener(handler);
italicbox.addItemListener(handler);
}
private class HandlerClass implements ItemListener{
}
}

Java Basic(63)-JButton Final Program


import javax.swing.JFrame;

class apples{
public static void main(String[] args){
Gui go = new Gui();
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.setSize(300, 200);
go.setVisible(true);
}
}

加入這些在 Gui.java:

HandlerClass handler = new HandlerClass();
reg.addActionListener(handler);
custom.addActionListener(handler);
}
private class HandlerClass implements ActionListener{
public void actionPerformed(ActionEvent event){
JOptionPane.showMessageDialog(null, String.format("%s", event.getActionCommand()));
}

這是運行時的結果:




Java Basic(62)-Jbutton


1.創建新class的名稱Gui



2.put 2圖片40X40)在你的/ bin



import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

public class Gui extends JFrame{
private JButton reg;
private JButton custom;
public Gui(){
super("The title");
setLayout(new FlowLayout());
reg = new JButton("reg Button");
add(reg);
Icon b = new ImageIcon(getClass().getResource("b.png")); 圖片名稱
Icon x = new ImageIcon(getClass().getResource("g.png"));
custom = new JButton("Custom",b);
custom.setRolloverSelectedIcon(x);
add(custom);
}

}




Java Basic(61)-Simple Polymorphic Program


public class Fish extends Animal{
public void noise(){
System.out.println("Click Slurk");
}
}

public class Animal {

public void noise(){
System.out.println("Animal dont make noise");
}
}


public class Dog extends Animal{
public void noise(){
System.out.println("Ruff");
}

}

class apples{
public static void main(String[] args){
Animal[] theList = new Animal[2];
Dog d = new Dog();
Fish f = new Fish();
theList[0]=d;
theList[1]=f;
for(Animal x: theList){
x.noise();
}

}
}

這是運行時的結果:
Ruff 
Click Slurk 

Java Basic(60)-Array Holding Many Objects


創建新class稱為AnimalList



public class AnimalList {
private Animal[] theList = new Animal[5];
private int i=0;
public void add(Animal a){
if(i<theList.length){
theList[i]=a;
System.out.println("Animal added at index "+1);
i++;
}
}

}

class apples{
public static void main(String[] args){
AnimalList ALO = new AnimalList();
Dog d = new Dog();
Fish f = new Fish();
ALO.add(d);
ALO.add(f);

}
}



這是運行時的結果:
Animal added at index 0 這是Dog
Animal added at index 1 這是Fish

Java Basic(59)-Class to Hold Objects


1.創建新class稱為DogAnimalFish



2.創建新class稱為DogList src文件夾中



public class Fish extends Animal{

}



public class Animal {

}

public class Dog extends Animal{

}

public class DogList {
private Dog[] theList = new Dog[5];
private int i=0;
public void add(Dog d){
if(i<theList.length){
theList[i]=d;
System.out.println("Dog added at index "+i);
i++;
}
}
}

class apples{
public static void main(String[] args){
DogList DLO = new DogList();
Dog d = new Dog();
DLO.add(d);

}
}

這是運行時的結果:
Dog added at index 0