40 likes | 144 Vues
Application. import javax.swing.*; import java.awt.Color; public class ApplicationEG { private ExampleTextField field ; /**post:win has been instantiated * and win.getX() == win.getY() == 0
E N D
Application import javax.swing.*; import java.awt.Color; public class ApplicationEG { private ExampleTextField field; /**post:win has been instantiated * and win.getX() == win.getY() == 0 * and win.getWidth()==200 and win.getHeight()==30 * and field has been added to win * and field.getX() == field.getY() == 10 * and field.getWidth()==200 and field.getHeight()==30 */ public ApplicationEG() { JFrame win = new JFrame("Window"); win.setLayout(null); win.setVisible(true); win.setBounds(0, 0, 400, 300); field = new ExampleTextField(10, 10, 200, 30); win.add(field, 0); win.setBackground(Color.blue); win.repaint(); } public static void main(String[] args) { ApplicationEG something = new ApplicationEG(); } }
Application import java.awt.event.*; import javax.swing.JTextField; public class ExampleTextField extends JTextField implements ActionListener { /** post: a new text field is instantiated * and getX()==x and getY()==y * and getWidth()==w and getHeight()==h */ public ExampleTextField(int x, int y, int w, int h) { super(); setBounds(x, y, w, h); addActionListener( this ); } /* EVENT HANDLER - called in response to striking return * post: the string content of this text field is output * and getX() == getX()@pre + 10 */ public void actionPerformed( ActionEvent e ) { System.out.println( getText() ); setLocation( getX()+10, getY() ); repaint(); } }
Applet import javax.swing.*; import java.awt.Color; public class AppletEG extends JApplet { private ExampleTextField field; /**post: field has been added to this * and getX() == getY() == 0 * and getWidth()==200 and getHeight()==30 * and field.getX() == field.getY() == 10 * and field.getWidth()==200 and field.getHeight()==30 */ public AppletEG() { setLayout(null); setVisible(true); setBounds(0, 0, 400, 300); field = new ExampleTextField(10, 10, 200, 30); add(field, 0);setBackground(Color.blue); repaint(); } } <HTML> <HEAD> <TITLE>Example use of an applet</TITLE> </HEAD> <BODY> <APPLET CODE="AppletEG.class" WIDTH=400 HEIGHT=300 CODEBASE="." > </APPLET> </BODY> </HTML>
Applet import java.awt.event.*; import javax.swing.JTextField; public class ExampleTextField extends JTextField implements ActionListener { /** post: a new text field is instantiated * and getX()==x and getY()==y * and getWidth()==w and getHeight()==h */ public ExampleTextField(int x, int y, int w, int h) { super(); setBounds(x, y, w, h); addActionListener( this ); } /* EVENT HANDLER - called in response to striking return * post: the string content of this text field is output * and getX() == getX()@pre + 10 */ public void actionPerformed( ActionEvent e ) { System.out.println( getText() ); setLocation( getX()+10, getY() ); repaint(); } }