//GiGinew.java --- @CopyLeft by tsaiwn@csie.nctu // http://www.csie.nctu.edu.tw/~tsaiwn/course/java/... import java.awt.*; import java.awt.event.*; public class GiGinew { Frame f = new Frame("Window 2005"); Button b1,b2,b3; Panel p; Button n, bq, big; int xSize=380, ySize=280; Dimension dim = Toolkit.getDefaultToolkit( ).getScreenSize(); TextArea tx= new TextArea("Hey 我愛 GiGi", 5,20, TextArea.SCROLLBARS_HORIZONTAL_ONLY); public static void main( String s[ ]) { new GiGinew( ); //can NOT use non-static var in main( ) } public GiGinew( ) { // Constructor f.setSize(xSize,ySize); f.setLocation(100,100); tx.append("\n=== ==== ==== TextArea tx="); tx.append(" new TextArea(\"Hey 我愛 GiGi\", 5,20,"); tx.append( " TextArea.SCROLLBARS_HORIZONTAL_ONLY);\n"); tx.append( "If 如果 GiGi 不漂亮, 你還會覺得她的歌好聽嗎?\n"); b1=new Button("111"); b2=new Button("222"); p = new Panel(); n= new Button("HahaHehehe"); p.add(n); // 把 n 這 Button 貼到 p 裡面 // more Buttons bq=new Button("Quit"); p.add(bq); bq.setCursor(new Cursor(Cursor.HAND_CURSOR) ); b1.setCursor(new Cursor(Cursor.HAND_CURSOR) ); bq.setBackground(new Color(38,38,38)); bq.setForeground(Color.red); big=new Button("Bigger"); p.add(big); big.setForeground(new Color(255,0,0)); n.setBackground(Color.green); Handler chang3 = new Handler( ); // 請一個外勞chang3 n.addActionListener(chang3); //有事交給chang3 // 的actionPerformed() b1.addActionListener(chang3); //b1 也要 bq.addActionListener(chang3); big.addActionListener(chang3); f.add(tx,"Center"); //文字擺中間 f.add(b1,"West"); ; f.add(b2,"East"); f.add(p,BorderLayout.SOUTH); b2.addActionListener(chang3); f.addWindowListener(chang3); // see below Amigo lee4 = new Amigo( ); // 錢多再請個外勞 b1.addMouseListener(lee4); // b1 上滑鼠事件交 lee4 處理 f.setVisible(true); //f.show(); 和 f.hide();是舊的寫法 } //an inner class class Handler extends WindowAdapter implements ActionListener{ public void windowClosing(WindowEvent e){ f.dispose( ); //System.exit(0); } public void actionPerformed(ActionEvent evt){ if(evt.getSource( ) == n){ n.setBackground(Color.red); n.setLabel(" 777 "); } /* evt... == n */ if(evt.getSource( ) == b1) n.setLabel("Eleven"); if(evt.getSource( ) == bq) System.exit(0); if(evt.getSource( ) == b2) n.setBackground(new Color(88,234,38)); //RGB if(evt.getSource( ) == big) { //increase window size xSize = (int) (xSize*1.2); if(xSize>dim.width) xSize= dim.width; ySize = (int) (ySize*1.2); if(ySize>dim.height) ySize= dim.height; tx.append("\n"+xSize+ "," + ySize); f.setSize(xSize,ySize); // resize the frame } f.validate( ); // so that Layout Manager do ... } /* actionPerformed */ } // Handler // another inner class Amigo class Amigo extends MouseAdapter { public void mouseEntered(MouseEvent e){ tx.append("\nMouse entered .. " ); Object who = e.getSource( ); tx.append( who.getClass( ).toString( )); String msg; Class what = who.getClass( ); if(what == (new Button()).getClass( ) ) //is Button msg = " "+((Button)who).getLabel( ); else if(what == (new Frame()).getClass( ) ) //is Frame msg = " "+((Frame)who).getTitle( ); else msg = " "+((Component)who).getName( ); tx.append(msg); } public void mouseExited(MouseEvent e){ } } // Amigo == means friend in spanish }