1 / 4

import javax.swing.*; import java.awt.Color; public class ApplicationEG { private ExampleTextField field ; /**po

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

laverne
Télécharger la présentation

import javax.swing.*; import java.awt.Color; public class ApplicationEG { private ExampleTextField field ; /**po

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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(); } }

  2. 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(); } }

  3. 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>

  4. 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(); } }

More Related