This applet tests two custom components, MirrorLabel and MirrorLabel . It also tests the validate() method. The user can click a button to change the text on the components in the applet. Clicking another button will validate the applet. This will cause components that have been invalidated to be resized. The MirrorLabel is automatically invalidated when its text is changed. For the StopWatch and Buttons, it might be platform-dependent.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | /******************************************************* * MYCPLUS Sample Code - https://www.mycplus.com * * * * This code is made available as a service to our * * visitors and is provided strictly for the * * purpose of illustration. * * * * Please direct all inquiries to saqib at mycplus.com * *******************************************************/ import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ComponentTest extends JApplet { MirrorLabel greet; StopWatch timer; JButton changeText; public void init() { getContentPane().setLayout(new FlowLayout()); greet = new MirrorLabel("PLEASE LET ME OUT!"); greet.setBackground(Color.black); greet.setForeground(Color.red); greet.setFont( new Font("SansSerif", Font.BOLD, 30) ); getContentPane().add(greet); timer = new StopWatch(); timer.setBackground(Color.white); timer.setForeground(Color.blue); timer.setOpaque(true); timer.setFont(new Font("Serif", Font.PLAIN, 20)); getContentPane().add( timer ); changeText = new JButton("Change Text in this Applet"); getContentPane().add(changeText); changeText.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent evt) { if (greet.getText().equals("PLEASE LET ME OUT!")) greet.setText("Help!"); else greet.setText("PLEASE LET ME OUT!"); timer.setText("Please click me."); if (changeText.getText().equals("Change Back")) changeText.setText("Change Text in this Applet"); else changeText.setText("Change Back"); } } ); } // end init() } // end class NullLayoutDemo |