ReverseLabels.java Inherits from CloseableFrame.java and uses ReversibleLabel.java. ********************** ReverseLabels.java ********************** import java.awt.*; ****************** public class ReverseLabels extends CloseableFrame { public static void main(String[] args) { new ReverseLabels(); } public ReverseLabels() { super("Reversible Labels"); setLayout(new FlowLayout()); setBackground(Color.lightGray); setFont(new Font("Serif", Font.BOLD, 18)); ReversibleLabel label1 = new ReversibleLabel("Black on White", Color.white, Color.black); add(label1); ReversibleLabel label2 = new ReversibleLabel("White on Black", Color.black, Color.white); add(label2); pack(); setVisible(true); } } /./././././././././. CloseableFrame.java &&&&&&&&&&&&&&&&&&& import java.awt.*; import java.awt.event.*; /** A Frame that you can actually quit. Used as the starting * point for most Java 1.1 graphical applications. * ******************* */ public class CloseableFrame extends Frame { public CloseableFrame(String title) { super(title); enableEvents(AWTEvent.WINDOW_EVENT_MASK); } /** Since we are doing something permanent, we need * to call super.processWindowEvent first. */ public void processWindowEvent(WindowEvent event) { super.processWindowEvent(event); // Handle listeners. if (event.getID() == WindowEvent.WINDOW_CLOSING) { // If the frame is used in an applet, use dispose(). System.exit(0); } } } &&&&&&&&&&&&&&&&&&&& ReversibleLabel.java. &&&&&&&&&&&&&&&&&&&& import java.awt.*; import java.awt.event.*; /** A Label that reverses its background and * foreground colors when the mouse is over it. * ********************** public class ReversibleLabel extends Label { public ReversibleLabel(String text, Color bgColor, Color fgColor) { super(text); MouseAdapter reverser = new MouseAdapter() { public void mouseEntered(MouseEvent event) { reverseColors(); } public void mouseExited(MouseEvent event) { reverseColors(); // or mouseEntered(event); } }; addMouseListener(reverser); setText(text); setBackground(bgColor); setForeground(fgColor); } protected void reverseColors() { Color fg = getForeground(); Color bg = getBackground(); setForeground(bg); setBackground(fg); } } #############################
Note: Brought from our old site: http://www.salearningschool.com/example_codes/ on Jan 2nd, 2017 From: http://sitestree.com/?p=10343
Categories:Programming Code Examples, Java/J2EE/J2ME, AWT Components
Tags:Java/J2EE/J2MEAWT Components
Post Data:2017-01-02 16:04:35
Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada