//pp0.java class pp0 { } //pp1.java class pp1 { public static void main( ){ System.out.println("Hey, PI=" + Math.PI); } } // pp1 --- why error ? //pp2.java -- note the parameter of main class pp2 { public static void main(String x[ ] ){ System.out.println("Hey, PI=" + Math.PI); } } //pp3.java class pp3 { public static void main(String x[ ] ){ //System.out.println("Hey, PI=" + Math.PI); for(i=1; i<=9; ++i) { for(k=1; k<=9; k++) print("" + i*k); print("\n"); /* 用起來比較清爽 */ } } void print(String x) { System.out.print(x); } } // pp3 --- why the print( ) function error? //pp4.java // -- note that main() is static, and thus can only use static fn class pp4 { public static void main(String x[ ] ){ //System.out.println("Hey, PI=" + Math.PI); for(int i=1; i<=9; ++i) { for(int k=1; k<=9; k++){ if(i*k < 10) print(" "); print(" " + i*k); } print("\n"); } } static void print(String x) { System.out.print(x); } } //pp5.java class pp5 { public static void main(String x[ ] ){ new pp5( ); } pp5 ( ) { // we can use non-static functions in here myfun( ); } void myfun( ) { //System.out.println("Hey, PI=" + Math.PI); for(int i=1; i<=9; ++i) { for(int k=1; k<=9; k++){ if(i*k < 10) print(" "); print(" " + i*k); } print("\n"); } } void print(String x) { System.out.print(x); } } //pp6.java import java.awt.*; class pp6 { Frame f = new Frame("Ha Ha Ha"); public static void main(String x[ ] ){ new pp6( ); } pp6 ( ) { myfun( ); } void myfun( ) { f.setVisible(true); //f.show( ); } void print(String x) { System.out.print(x); } } //pp7.java import java.awt.event.*; import java.awt.*; class pp7 extends WindowAdapter { Frame f = new Frame("Ha Ha Ha"); public static void main(String x[ ] ){ new pp7( ); } pp7 ( ) { print("Hey .. Running ...\n"); myfun( ); } void myfun( ) { f.setSize(580,380); f.setVisible(true); //f.show( ); } public void windowClosing(WindowEvent e){ f.dispose( ); } void print(String x) { System.out.print(x); } } //pp8.java // do NOT forget to setVisible ! // use the WindowAdapter import java.awt.event.*; import java.awt.*; class pp8 extends WindowAdapter { Frame f = new Frame("Ha Ha Ha"); public static void main(String x[ ] ){ new pp8( ); } pp8 ( ) { print("Hey .. Running ...\n"); myfun( ); } void myfun( ) { f.addWindowListener(this); f.setSize(580,380); f.setVisible(true); //f.show( ); } public void windowClosing(WindowEvent e){ f.dispose( ); } void print(String x) { System.out.print(x); } } //pp9.java // using the event handler in other class import java.awt.event.*; import java.awt.*; class pp9 { Frame f = new Frame("Ha Ha Ha"); public static void main(String x[ ] ){ new pp9( ); } pp9 ( ) { print("Hey .. Running ...\n"); myfun( ); } void myfun( ) { YLau arbu = new YLau(f); // arbu is a YLau f.addWindowListener(arbu); // !!! important f.setSize(580,380); f.setVisible(true); //f.show( ); } void print(String x) { System.out.print(x); } } // pp9 ///////// YLau is an independent class class YLau extends WindowAdapter { Frame fff; YLau(Frame x) { fff = x; } public void windowClosing(WindowEvent e){ fff.dispose( ); } } //ppa.java // note that YLau is an inner class import java.awt.event.*; import java.awt.*; class ppa { Frame f = new Frame("Ha Ha Ha"); public static void main(String x[ ] ){ new ppa( ); } ppa ( ) { print("Hey .. Running ...\n"); myfun( ); } void myfun( ) { YLau arbu = new YLau( ); f.addWindowListener(arbu); f.setSize(580,380); f.setVisible(true); //f.show( ); } void print(String x) { System.out.print(x); } //// not end yet class YLau extends WindowAdapter { // public void windowClosing(WindowEvent e){ f.dispose( ); } } } // ppa //ppb.java // why ? why can NOT see the button ba ? bb? bc? import java.awt.event.*; import java.awt.*; class ppb { Frame f = new Frame("Ha Ha Ha"); Button ba, bb, bc; public static void main(String x[ ] ){ new ppb( ); } ppb ( ) { print("Hey .. Running ...\n"); myfun( ); } void myfun( ) { YLau arbu = new YLau( ); f.addWindowListener(arbu); f.setSize(580,380); f.setVisible(true); //f.show( ); ba = new Button("AAAA"); bb = new Button("BBBBB"); bc = new Button("CCC"); bb.setForeground(Color.blue); bb.setBackground(new Color(28, 228, 58)); f.add("West", ba); // add ba into f } void print(String x) { System.out.print(x); } //// not end yet class YLau extends WindowAdapter { // public void windowClosing(WindowEvent e){ f.dispose( ); } } } // ppb //ppc.java // do NOT forget to validate ! import java.awt.event.*; import java.awt.*; class ppc { Frame f = new Frame("Ha Ha Ha"); Button ba, bb, bc; public static void main(String x[ ] ){ new ppc( ); } ppc ( ) { print("Hey .. Running ...\n"); myfun( ); } void myfun( ) { YLau arbu = new YLau( ); f.addWindowListener(arbu); f.setSize(580,380); f.setVisible(true); //f.show( ); ba = new Button("AAAA"); bb = new Button("BBBBB"); bc = new Button("CCC"); bb.setForeground(Color.blue); bb.setBackground(new Color(28, 228, 58)); f.add("West", ba); // add ba into f f.validate( ); // !! important } void print(String x) { System.out.print(x); } //// not end yet class YLau extends WindowAdapter { // public void windowClosing(WindowEvent e){ f.dispose( ); } } } //ppd.java import java.awt.event.*; import java.awt.*; class ppd { Frame f = new Frame("Ha Ha Ha"); Button ba, bb, bc; public static void main(String x[ ] ){ new ppd( ); } ppd ( ) { print("Hey .. Running ...\n"); myfun( ); } void myfun( ) { YLau arbu = new YLau( ); f.addWindowListener(arbu); f.setSize(580,380); f.setVisible(true); //f.show( ); ba = new Button("AAAA"); bb = new Button("BBBBB"); bc = new Button("CCC"); bb.setForeground(Color.blue); bb.setBackground(new Color(28, 228, 58)); Panel p=new Panel(); p.add(ba); p.add(bb); f.add("West", p); f.add(bc, "Center"); f.add(new Button("eeee"), BorderLayout.EAST); f.validate( ); // important ! } void print(String x) { System.out.print(x); } //// not end yet class YLau extends WindowAdapter { // public void windowClosing(WindowEvent e){ f.dispose( ); } } } //ppe.java import java.awt.event.*; import java.awt.*; class ppe { Frame f = new Frame("Ha Ha Ha"); Button ba, bb, bc; public static void main(String x[ ] ){ new ppe( ); } ppe ( ) { print("Hey .. Running ...\n"); myfun( ); } void myfun( ) { YLau arbu = new YLau( ); f.addWindowListener(arbu); f.setSize(580,380); f.setVisible(true); //f.show( ); ba = new Button("Quit"); bb = new Button("BBBBB"); bc = new Button("CCC"); bb.setForeground(Color.blue); bb.setBackground(new Color(28, 228, 58)); Panel p=new Panel(); p.add(ba); p.add(bb); f.add("West", p); f.add(bc, "Center"); f.add(new Button("eeee"), BorderLayout.EAST); ba.addActionListener(new BenLau() ); bb.addActionListener(new BenLau() ); f.validate( ); } void print(String x) { System.out.print(x); } //// not end yet class YLau extends WindowAdapter { // public void windowClosing(WindowEvent e){ f.dispose( ); } } class BenLau implements ActionListener { public void actionPerformed(ActionEvent e){ System.exit(0); } } /// /// /// } // end of ppe.java //ppf.java import java.awt.event.*; import java.awt.*; class ppf { Frame f = new Frame("Ha Ha Ha"); Button ba, bb, bc; public static void main(String x[ ] ){ new ppf( ); } ppf ( ) { print("Hey .. Running ...\n"); myfun( ); } void myfun( ) { YLau arbu = new YLau( ); f.addWindowListener(arbu); f.setSize(580,380); f.setVisible(true); //f.show( ); ba = new Button("Quit"); bb = new Button("BBBBB"); bc = new Button("CCC"); bb.setForeground(Color.blue); bb.setBackground(new Color(28, 228, 58)); Panel p=new Panel(new BorderLayout()); p.add(ba, "North"); p.add("Center", bb); bx = new Button("----"); // see the end of this program p.add(bx, "South"); Panel p2 = new Panel(new BorderLayout()); p2.add(p, "North"); bx.setForeground(Color.red); bx.setBackground(new Color(58, 198, 238)); bx.setLabel("< bx >"); // change caption BenLau chang3 = new BenLau( ); bb.addActionListener( chang3 ); bx.addActionListener( chang3 ); f.add("West", p2); f.add(bc, "Center"); f.add(new Button("eeee"), BorderLayout.EAST); ba.addActionListener(new BenLau() ); f.validate( ); } void print(String x) { System.out.print(x); } //// not end yet class YLau extends WindowAdapter { // public void windowClosing(WindowEvent e){ f.dispose( ); } } class BenLau implements ActionListener { public void actionPerformed(ActionEvent e){ Object who = e.getSource(); if(who == ba) System.exit(0); if(who == bb) bc.setBackground(Color.green); if(who == bx) bc.setBackground(Color.blue); } } Button bx; //////////////// Note: this is global ! } // ppf ends here