!!!!!!!!!!!! ClickListener.java A simple subclass of MouseAdapter that reports where the mouse was pressed. When attached to an applet, look for the report in the Java Console. !!!!!!!!!!!! import java.awt.event.*; /** The listener used by ClickReporter. * ************** public class ClickListener extends MouseAdapter { public void mousePressed(MouseEvent event) { System.out.println("Mouse pressed at (" + event.getX() + "," + event.getY() + ")."); } } <<<<<<<<<<<<<<
Aug 27
Subclass of MouseAdapter
Aug 27
Create PopupMenu and add MenuItems
import java.applet.Applet; import java.awt.*; import java.awt.event.*; ************************ /** Simple demo of pop-up menus. * ******************** public class ColorPopupMenu extends Applet implements ActionListener { private String[] colorNames = { "White", "Light Gray", "Gray", "Dark Gray", "Black" }; private Color[] colors = { Color.white, Color.lightGray, Color.gray, Color.darkGray, Color.black }; private PopupMenu menu; /** Create PopupMenu and add MenuItems. */ public void init() { setBackground(Color.gray); menu = new PopupMenu("Background Color"); enableEvents(AWTEvent.MOUSE_EVENT_MASK); MenuItem colorName; for(int i=0; i
Aug 27
ReverseLabels.java Inherits from CloseableFrame.java and uses ReversibleLabel.java.
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); } } #############################
Aug 27
TextFields
import java.applet.Applet; import java.awt.*; /** A TextField from each of the four constructors. * ********************* public class TextFields extends Applet { public void init() { add(new TextField()); add(new TextField(30)); add(new TextField("Initial String")); add(new TextField("Initial", 30)); } }
Aug 27
ChoiceTest2
ChoiceTest2.java /././././././././ import java.applet.Applet; import java.awt.*; import java.awt.event.*; /***********************/ public class ChoiceTest2 extends Applet implements ItemListener { private Choice choice; public void init() { setFont(new Font("SansSerif", Font.BOLD, 36)); choice = new Choice(); choice.addItem("Choice 1"); choice.addItem("Choice 2"); choice.addItem("Choice 3"); choice.addItemListener(this); add(choice); } public void itemStateChanged(ItemEvent event) { Choice choice = (Choice)event.getSource(); String selection = choice.getSelectedItem(); if (selection.equals("Choice 1")) { doChoice1Action(); } else if (selection.equals("Choice 2")) { doChoice2Action(); } else if (selection.equals("Choice 3")) { doChoice3Action(); } } private void doChoice1Action() { System.out.println("Choice 1 Action"); } private void doChoice2Action() { System.out.println("Choice 2 Action"); } private void doChoice3Action() { System.out.println("Choice 3 Action"); } }
Aug 27
CheckboxGroups
CheckboxGroups.java /./././././././././ ///////////////////// import java.applet.Applet; import java.awt.*; //////////////////// public class CheckboxGroups extends Applet { public void init() { setLayout(new GridLayout(4, 2)); setBackground(Color.lightGray); setFont(new Font("Serif", Font.BOLD, 16)); add(new Label("Flavor", Label.CENTER)); add(new Label("Toppings", Label.CENTER)); CheckboxGroup flavorGroup = new CheckboxGroup(); add(new Checkbox("Vanilla", flavorGroup, true)); add(new Checkbox("Colored Sprinkles")); add(new Checkbox("Chocolate", flavorGroup, false)); add(new Checkbox("Cashews")); add(new Checkbox("Strawberry", flavorGroup, false)); add(new Checkbox("Kiwi")); } }
Aug 26
A textfield and three buttons arranged by a verticle BoxLayout
BoxLayoutTest.java A textfield and three buttons arranged by a verticle BoxLayout. Uses WindowUtilities.java and ExitListener.java. ################## import java.awt.*; import java.awt.event.*; import javax.swing.*; /** An example of BoxLayout. * *********** public class BoxLayoutTest extends JPanel implements ActionListener{ BoxLayout layout; JButton topButton, middleButton, bottomButton; public BoxLayoutTest() { layout = new BoxLayout(this, BoxLayout.Y_AXIS); setLayout(layout); JLabel label = new JLabel("BoxLayout Demo"); topButton = new JButton("Left Alignment"); middleButton = new JButton("Center Alignment"); bottomButton = new JButton("Right Alignment"); topButton.addActionListener(this); middleButton.addActionListener(this); bottomButton.addActionListener(this); add(label); add(topButton); add(middleButton); add(bottomButton); setBackground(Color.white); } public void actionPerformed(ActionEvent event) { if (event.getSource() == topButton) { refresh(Component.LEFT_ALIGNMENT); } else if (event.getSource() == middleButton) { refresh(Component.CENTER_ALIGNMENT); } else if (event.getSource() == bottomButton) { refresh(Component.RIGHT_ALIGNMENT); } } private void refresh(float alignment){ topButton.setAlignmentX(alignment); middleButton.setAlignmentX(alignment); bottomButton.setAlignmentX(alignment); revalidate(); System.out.println("x: "+layout.getLayoutAlignmentX(this)); } public static void main(String[] args) { WindowUtilities.setNativeLookAndFeel(); WindowUtilities.openInJFrame(new BoxLayoutTest(), 300, 135, "BoxLayoutTest"); } }
Aug 26
Implementation of a simple browser in Swing (The user can specify a URL to load into the browser (JEditorPane))
Browser.java Implementation of a simple browser in Swing. The user can specify a URL to load into the browser (JEditorPane). By attaching an Hyperlink Listener, the editor pane is responsive to hyperlinks selected by the user. Uses the following class and image:
import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; import java.net.*; import java.io.*; /** Very simplistic "Web browser" using Swing. Supply a URL on * the command line to see it initially and to set the * destination of the "home" button. * */ public class Browser extends JFrame implements HyperlinkListener, ActionListener { public static void main(String[] args) { if (args.length == 0) new Browser("http://www.corewebprogramming.com/"); else new Browser(args[0]); } private JIconButton homeButton; private JTextField urlField; private JEditorPane htmlPane; private String initialURL; public Browser(String initialURL) { super("Simple Swing Browser"); this.initialURL = initialURL; addWindowListener(new ExitListener()); WindowUtilities.setNativeLookAndFeel(); JPanel topPanel = new JPanel(); topPanel.setBackground(Color.lightGray); homeButton = new JIconButton("home.gif"); homeButton.addActionListener(this); JLabel urlLabel = new JLabel("URL:"); urlField = new JTextField(30); urlField.setText(initialURL); urlField.addActionListener(this); topPanel.add(homeButton); topPanel.add(urlLabel); topPanel.add(urlField); getContentPane().add(topPanel, BorderLayout.NORTH); try { htmlPane = new JEditorPane(initialURL); htmlPane.setEditable(false); htmlPane.addHyperlinkListener(this); JScrollPane scrollPane = new JScrollPane(htmlPane); getContentPane().add(scrollPane, BorderLayout.CENTER); } catch(IOException ioe) { warnUser("Can't build HTML pane for " + initialURL + ": " + ioe); } Dimension screenSize = getToolkit().getScreenSize(); int width = screenSize.width * 8 / 10; int height = screenSize.height * 8 / 10; setBounds(width/8, height/8, width, height); setVisible(true); } public void actionPerformed(ActionEvent event) { String url; if (event.getSource() == urlField) { url = urlField.getText(); } else { // Clicked "home" button instead of entering URL. url = initialURL; } try { htmlPane.setPage(new URL(url)); urlField.setText(url); } catch(IOException ioe) { warnUser("Can't follow link to " + url + ": " + ioe); } } public void hyperlinkUpdate(HyperlinkEvent event) { if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED) { try { htmlPane.setPage(event.getURL()); urlField.setText(event.getURL().toExternalForm()); } catch(IOException ioe) { warnUser("Can't follow link to " + event.getURL().toExternalForm() + ": " + ioe); } } } private void warnUser(String message) { JOptionPane.showMessageDialog(this, message, "Error", JOptionPane.ERROR_MESSAGE); } }
Aug 26
A simple applet (JApplet) created in Swing.
import java.awt.*; import javax.swing.*; /** Tiny example showing the main differences in using * JApplet instead of Applet: using the content pane, * getting Java (Metal) look and feel by default, and * having BorderLayout be the default instead of FlowLayout. * */ public class JAppletExample extends JApplet { public void init() { WindowUtilities.setNativeLookAndFeel(); Container content = getContentPane(); content.setBackground(Color.white); content.setLayout(new FlowLayout()); content.add(new JButton("Button 1")); content.add(new JButton("Button 2")); content.add(new JButton("Button 3")); } }
Aug 26
Illustrates the insertion of menu entries in Frame menu bars.
************** ColorMenu.java ************** import java.awt.*; import java.awt.event.*; /** Illustrates the insertion of menu entries in Frame * menu bars. * public class ColorMenu extends CloseableFrame implements ActionListener { private String[] colorNames = { "Black", "White", "Light Gray", "Medium Gray", "Dark Gray" }; private Color[] colorValues = { Color.black, Color.white, Color.lightGray, Color.gray, Color.darkGray }; public ColorMenu() { super("ColorMenu"); MenuBar bar = new MenuBar(); Menu colorMenu = new Menu("Colors"); for(int i=0; i<2; i++) { colorMenu.add(colorNames[i]); } Menu grayMenu = new Menu("Gray"); for(int i=2; i