Java Reference
In-Depth Information
20. Container content = mainFrame.getContentPane();
21. content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
22.
23. displayPanel = new JPanel();
24. display = new JTextArea(20, 60);
25. display.setEditable(false);
26. displayPanel.add(display);
27. content.add(displayPanel);
28.
29. controlPanel = new JPanel();
30. newContact = new JButton("Create contact");
31. newAppointment = new JButton("Create appointment");
32. undo = new JButton("Undo");
33. refresh = new JButton("Refresh");
34. exit = new JButton("Exit");
35. controlPanel.add(newContact);
36. controlPanel.add(newAppointment);
37. controlPanel.add(undo);
38. controlPanel.add(refresh);
39. controlPanel.add(exit);
40. content.add(controlPanel);
41.
42. newContact.addActionListener(this);
43. newAppointment.addActionListener(this);
44. undo.addActionListener(this);
45. refresh.addActionListener(this);
46. exit.addActionListener(this);
47.
48. mainFrame.addWindowListener(new WindowCloseManager());
49. mainFrame.pack();
50. mainFrame.setVisible(true);
51. }
52.
53. public void refreshDisplay(String actionMessage){
54. display.setText(actionMessage + "\nCOMMAND HISTORY:\n" +
55. HistoryList.getInstance().toString());
56. }
57.
58. public void actionPerformed(ActionEvent evt){
59. Object originator = evt.getSource();
60. if (originator == newContact){
61. addCommand(" New Contact");
62. }
63. else if (originator == newAppointment){
64. addCommand(" New Appointment");
65. }
66. else if (originator == undo){
67. undoCommand();
68. }
69. else if (originator == refresh){
70. refreshDisplay("");
71. }
72. else if (originator == exit){
73. exitApplication();
74. }
75. }
76.
77. private class WindowCloseManager extends WindowAdapter{
78. public void windowClosing(WindowEvent evt){
79. exitApplication();
80. }
81. }
82.
83. private void addCommand(String message){
84. HistoryList.getInstance().addCommand((++historyCount) + message);
85. refreshDisplay("Add Command: " + message);
86. }
87.
88. private void undoCommand(){
89. Object result = HistoryList.getInstance().undoCommand();
90. historyCount--;
91. refreshDisplay("Undo Command: " + result);
92. }
93.
94. private void exitApplication(){
95. System.exit(0);
96. }
97. }
Search WWH ::




Custom Search