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[0] = new JButton("Button 1");
btn[1] = 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 btn:
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 btn array:
btn[0] = new JButton("Button 0");
btn[1] = new JButton("Button 1");
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 JButton objects. Click here 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)
{
MyFrame frame = new MyFrame("Alan Window");
frame.setSize(300, 200);
frame.setLocation(100, 75);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class MyFrame extends JFrame
{
JButton [] buttons = new JButton[10];
public MyFrame(String s)
{
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, 200);
frame.setLocation(100, 75);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class MyFrame extends JFrame
{
JButton [] buttons = new JButton[10];
JTextField status = new JTextField(12);
public MyFrame(String s)
{
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++;
}
add(status);
status.setEnabled(false);
ReportClick handler = new ReportClick();
i = 0;
while (i < 10)
{
buttons[i].addActionListener(handler);
i++;
}
}
class ReportClick implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
JButton btn = (JButton) event.getSource();
String s = btn.getText();
status.setText(s);
}
}
}
When run, the following window is created:
.
Exercises
- Create a Java application that puts 35 text fields in a window. Each text field should be five characters in size.
- Create a Java application that puts 15 buttons in a window with the names "Button 0", "Button 1", etc. Also show a text field with the initial contents of "Times clicked: 0". Each time a button is clicked the "Times clicked" count goes up by one (i.e., after six buttons have been clicked the text field shows "Times clicked: 6").
- Write a program that will act as a simple calculator. Present ten buttons where each button displays a single digit (0 through 9), a non-editable JTextField for showing the current total, and a clear button. Pressing a button will add its value into the running total kept in the JTextField. Clear will set the total back to zero.
- Create a Java application that puts 15 buttons in a window with the names "Button 0", "Button 1", etc. Whenever a button is clicked the text on the face of the button changes to "Clicked!".
|