Java Reference
In-Depth Information
48. layoutButtons();
49. initializeEvents();
50. this.setSize(200, 200);
51. this.setVisible(true);
52. }
53.
54. private void initializeEvents() {
55. MyButtonListener m = new MyButtonListener();
56. redBtn.addActionListener(m);
57. whiteBtn.addActionListener(m);
58. blueBtn.addActionListener(m);
59. }
60.
61. protected void layoutButtons() {
62. this.setLayout(new BorderLayout());
63. this.add(redBtn, NORTH);
64. this.add(whiteBtn, SOUTH);
65. this.add(blueBtn, WEST);
66. }
67.
68. public static void main(String [] args) {
69. new ColorChanger(“Click a button”);
70. }
71.}
Here is a breakdown of each of the elements within the ColorChanger class:
Line 8 declares three instance variables, each of type java.awt.Button : redBtn ,
whiteBtn , and blueBtn .
Line 15 declares three class variables, each of type java.awt.Color : RED , WHITE , and
BLUE .
The class has five methods. The main method is static and the other methods are
instance methods: getColors , getButtons , initializeEvents , and layoutButtons .
This class has one constructor defined on line 46. It takes in a single argument of type
String that appears in the title bar of the window.
The ColorChanger class declares one nested class, MyButtonListener , on line 23. This
nested class contains one method, actionPerformed , which gets invoked whenever one
of the three buttons is clicked.
The class declares one instance initializer, which is the block of code on lines 9 to 13.
The class declares one static initializer, which is the block of code on lines 17 to 21.
Search WWH ::




Custom Search