{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Intro to Java Web-Notes_Part65

Intro to Java Web-Notes_Part65 - Arrays of Objects It takes...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Arrays of Objects It takes several steps to create an array of objects in Java. In the example shown below JButton btn; btn : new JButton[3]; btn[O] = new JButton("Button l"); btn[l] = new JButton("Button 2"); btn[2] = new JButton("Button 3"); three distinct actions occured. First, a reference to the array was created: JButton btn; Then the array object was created, and a reference to that array object was saved in bm: btn : new JButton[3]; The array object contains three references to JButton objects. No JButton objects have yet been created -- there are only three JButton references at this point. Next, the three JButton objects are created and their references are saved in the bm array: btn[0] : new JButton("Button O"); btn[l] : new JButton("Button l"); btn[2] = new JButton("Button 2"); Note that loop would be more appropriate here, but I wanted to spell out the creation of the three J Button objects. Click here igijijfitg for a discussion of the differences between arrays of built-in types and arrays of objects. Here is a Java application that creates ten JButton objects and places them in a window: import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ArrayOfButtons { public static void main(String args) l MyFrame frame : new MyFrame("Alan Window"); frame.setSize(300, ZOO); frame.setLocation(lOO, 75); frame.setDefaultCloseOperation(JFrame.3XITiONiCLOSE); frame.setVisible(true); l class MyFrame extends JFrame i JButton buttons : new JButton[lO]; public MyFrame(String s) i super(s); setLayout(new FlowLayout()); int i : 0; while (i < 10) { buttons[i] : new JButton("Button " + i); // Create each JButton object add(buttons[i]); i++; This code creates the following frame Window: In this next example, a text field has been added to the Window, and event handling for the buttons that reports which button has been clicked: import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ArrayOfButtons { public static void main(String args) { MyFrame frame = new MyFrame("Alan Window"); frame.setSize(300, ZOO); frame.setLocation(lOO, 75); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisib;e(true); class MyFrame extends JFrame { JButton buttons = new JButton[lO]; JTextField status = new JTextField(l2); public MyFrame(String s) { super(s); setLayout(new FlowLayout()); int i : 0; while (i < 10) l buttons[i] = new JButton("Button " + i); // Create each JButton object add(buttons[i]); i++; ...
View Full Document

{[ snackBarMessage ]}