Tag: Java/J2EE/J2MEAWT Components

Jan 02

Lists.java

Lists.java Inherits from CloseableFrame.java. /./././././././././ import java.awt.*; /*****************/ public class Lists extends CloseableFrame { public static void main(String[] args) { new Lists(); } public Lists() { super("Lists"); setLayout(new FlowLayout()); setBackground(Color.lightGray); setFont(new Font("SansSerif", Font.BOLD, 18)); List list1 = new List(3, false); list1.add("Vanilla"); list1.add("Chocolate"); list1.add("Strawberry"); add(list1); List list2 = new List(3, true); list2.add("Colored Sprinkles"); list2.add("Cashews"); list2.add("Kiwi"); add(list2); …

Continue reading

Jan 02

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)); } } Note: Brought from our old site: http://www.salearningschool.com/example_codes/ on Jan 2nd, 2017

Jan 02

TextAreas

TextAreas.java ************** import java.applet.Applet; import java.awt.*; /././././././,/././././ public class TextAreas extends Applet { public void init() { setBackground(Color.lightGray); add(new TextArea(3, 10)); add(new TextArea("SomenInitialnText", 3, 10)); } } Note: Brought from our old site: http://www.salearningschool.com/example_codes/ on Jan 2nd, 2017

Jan 02

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 …

Continue reading

Jan 02

Layout of a complicated GUI interface with GridLayout

################################## GridBagTest.java Layout of a complicated GUI interface with GridLayout. Uses WindowUtilities.java and ExitListener.java. ################################## import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import javax.swing.border.*; /** An example demonstrating a GridBagLayout GUI with * input text area and multiple buttons. * ********* public class GridBagTest extends JPanel { private JTextArea textArea; private JButton bSaveAs, bOk, …

Continue reading

Jan 02

FrameExample1.java and 2

****************** # FrameExample1.java ****************** import java.awt.*; /** */ public class FrameExample1 { public static void main(String[] args) { Frame f = new Frame("Frame Example 1"); f.setSize(400, 300); f.setVisible(true); } } ********************* # FrameExample2.java ********************* import java.awt.*; /** */ public class FrameExample2 extends Frame { public static void main(String[] args) { new FrameExample2(); } public FrameExample2() …

Continue reading

Jan 02

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 …

Continue reading

Jan 02

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() …

Continue reading

Jan 02

Custom AWT Slider

***************** Custom AWT Slider * LabeledCostSlider.java. A numeric slider class with attached label. * CostSlider.java. A slider class that lets you read numeric values. Used in the LabeledCostSlider class. * Slider.java. A slider class: a combination of Scrollbar and TextField. Used in the CostSlider class. * ScrollbarPanel.java A Panel with adjustable top and bottom insets, …

Continue reading

Jan 02

A Frame that lets you draw circles with mouse clicks

SavedFrame.java **************** A Frame that lets you draw circles with mouse clicks //************** import java.awt.*; import java.awt.event.*; import java.io.*; /** A Frame that lets you draw circles with mouse clicks * and then save the Frame and all circles to disk. * public class SavedFrame extends CloseableFrame implements ActionListener { /** If a saved version …

Continue reading

Jan 02

A Frame that uses the Confirm dialog to verify quit

ConfirmTest.java **************** import java.awt.*; import java.awt.event.*; /** A Frame that uses the Confirm dialog to verify that * users really want to quit. * public class ConfirmTest extends Frame { public static void main(String[] args) { new ConfirmTest(); } public ConfirmTest() { super("Confirming QUIT"); setSize(200, 200); addWindowListener(new ConfirmListener()); setVisible(true); } public ConfirmTest(String title) { super(title); …

Continue reading

Jan 02

Uses a FileDialog to choose the file to display

DisplayFile.java **************** import java.awt.*; import java.awt.event.*; import java.io.*; /** Uses a FileDialog to choose the file to display. *************** public class DisplayFile extends CloseableFrame implements ActionListener { public static void main(String[] args) { new DisplayFile(); } private Button loadButton; private TextArea fileArea; private FileDialog loader; public DisplayFile() { super("Using FileDialog"); loadButton = new Button("Display File"); …

Continue reading

Jan 02

Handling Events

*********************************** * ActionExample1.java Inherits from CloseableFrame.java and uses SetSizeButton.java. * ActionExample2.java Inherits from CloseableFrame.java. ********************************************************** ActionExample1.java ******************* import java.awt.*; public class ActionExample1 extends CloseableFrame { public static void main(String[] args) { new ActionExample1(); } public ActionExample1() { super("Handling Events in Component"); setLayout(new FlowLayout()); setFont(new Font("Serif", Font.BOLD, 18)); add(new SetSizeButton(300, 200)); add(new SetSizeButton(400, 300)); add(new SetSizeButton(500, …

Continue reading

Jan 02

Batton’s java

import java.applet.Applet; import java.awt.*; /././././././ public class Buttons extends Applet { private Button button1, button2, button3; public void init() { button1 = new Button("Button One"); button2 = new Button("Button Two"); button3 = new Button("Button Three"); add(button1); add(button2); add(button3); } } /././././././././. Note: Brought from our old site: http://www.salearningschool.com/example_codes/ on Jan 2nd, 2017

Jan 02

ButtonExample.java Uses the following

/./././././././././ # ButtonExample.java Uses the following classes: * CloseableFrame.java * FgReporter.java * BgReporter.java * SizeReporter.java ****************** ButtonExample.java ****************** import java.awt.*; import java.awt.event.*; /././././././././././ public class ButtonExample extends CloseableFrame { public static void main(String[] args) { new ButtonExample(); } public ButtonExample() { super("Using ActionListeners"); setLayout(new FlowLayout()); Button b1 = new Button("Button 1"); Button b2 = new …

Continue reading

Jan 02

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 …

Continue reading

Jan 02

ChoiceTest

import java.applet.Applet; import java.awt.*; /*******/ public class ChoiceTest extends Applet { 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"); add(choice); } } Note: Brought from our old site: http://www.salearningschool.com/example_codes/ on Jan 2nd, 2017

Jan 02

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 …

Continue reading

Jan 02

Insert three circles into an Applet using FlowLayout

import java.awt.*; import java.applet.Applet; /** Insert three circles into an Applet using FlowLayout. * */ public class CircleTest extends Applet { public void init() { setBackground(Color.lightGray); add(new Circle(Color.white, 30)); add(new Circle(Color.gray, 40)); add(new Circle(Color.black, 50)); } } Note: Brought from our old site: http://www.salearningschool.com/example_codes/ on Jan 2nd, 2017

Jan 02

A Circle component built using a Canvas

import java.awt.*; /** A Circle component built using a Canvas. * */ public class Circle extends Canvas { private int width, height; public Circle(Color foreground, int radius) { setForeground(foreground); width = 2*radius; height = 2*radius; setSize(width, height); } public void paint(Graphics g) { g.fillOval(0, 0, width, height); } public void setCenter(int x, int y) { …

Continue reading

Jan 02

Position circles down the diagonal so that their borders

import java.awt.*; import java.applet.Applet; /** Position circles down the diagonal so that their borders * just touch. Illustrates that AWT components are * rectangular and opaque. */ public class CircleTest2 extends Applet { public void init() { setBackground(Color.lightGray); setLayout(null); // Turn off layout manager. Circle circle; int radius = getSize().width/6; int deltaX = round(2.0 * …

Continue reading

Jan 02

Eight ungrouped buttons in an Applet using FlowLayout

import java.applet.Applet; import java.awt.*; ************************** /** Eight ungrouped buttons in an Applet using FlowLayout. * */ public class ButtonTest1 extends Applet { public void init() { String[] labelPrefixes = { "Start", "Stop", "Pause", "Resume" }; for (int i=0; i<4; i++) { add(new Button(labelPrefixes[i] + " Thread1")); } for (int i=0; i<4; i++) { add(new Button(labelPrefixes[i] …

Continue reading

Jan 02

Eight buttons: four each in two panels

import java.applet.Applet; import java.awt.*; *************************** /** Eight buttons: four each in two panels. * */ public class ButtonTest2 extends Applet { public void init() { String[] labelPrefixes = { "Start", "Stop", "Pause", "Resume" }; Panel p1 = new Panel(); for (int i=0; i<4; i++) { p1.add(new Button(labelPrefixes[i] + " Thread1")); } Panel p2 = new …

Continue reading

Skip to toolbar