Advertisement
Guest User

Untitled

a guest
Apr 20th, 2014
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.58 KB | None | 0 0
  1. import java.awt.*;
  2. import javax.swing.*;
  3. import java.awt.event.*;
  4.  
  5. public class TempConverter extends JFrame
  6. {
  7. //Creating a contentPane down inside the inner class
  8. Container thisContentPane;
  9. //class scope variables : DO NOT CREATE THIS OBJECTS HERE.
  10. JButton calculateButton, clearButton;
  11. JTextField celsiusField, fahrenheitField, kelvinField;
  12.  
  13. //menu
  14. JMenuBar menuBar = new JMenuBar();
  15. JMenu backgroundColor = new JMenu("Background Color");
  16. JMenu help = new JMenu("Help");
  17.  
  18. JMenuItem lightGray, white, black, blue, howToUse, about;
  19.  
  20.  
  21. //constructor
  22. TempConverter()
  23. {
  24. super("Temperature Converter App");
  25.  
  26. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  27. this.setLayout(new BorderLayout());
  28. this.setSize(400,200);;
  29. this.setLocationRelativeTo(null);
  30.  
  31.  
  32. //menuBar
  33. this.setJMenuBar(menuBar);
  34.  
  35.  
  36. menuBar.add(backgroundColor);
  37.  
  38. //adding JMenu to JMenuBar
  39. menuBar.add(backgroundColor);
  40. menuBar.add(help);
  41.  
  42. //adding JMenuItems
  43. lightGray = backgroundColor.add("LIGHTGRAY");
  44. white = backgroundColor.add("WHITE");
  45. black = backgroundColor.add("BLACK");
  46. blue = backgroundColor.add("BLUE");
  47.  
  48. howToUse = help.add("How To Use");
  49. about = help.add("Help");
  50.  
  51.  
  52. //babysitter
  53. MaryPoppins babysitter = new MaryPoppins();
  54.  
  55. //adding action listener to the menu item
  56. lightGray.addActionListener(babysitter);
  57. white.addActionListener(babysitter);
  58. black.addActionListener(babysitter);
  59. blue.addActionListener(babysitter);
  60. howToUse.addActionListener(babysitter);
  61. about.addActionListener(babysitter);
  62.  
  63.  
  64. //building JPanels
  65. JPanel topPanel = new JPanel();
  66. topPanel.setLayout(new GridLayout(3,2,0,20));
  67.  
  68. //add this to JFrame in centerzone
  69. this.add(topPanel, BorderLayout.CENTER);
  70.  
  71. //bottom panel
  72. JPanel bottomPanel = new JPanel();
  73. bottomPanel.setLayout(new FlowLayout());
  74.  
  75. //add this to JFrame in bottom
  76. this.add(bottomPanel, BorderLayout.SOUTH);
  77.  
  78. //add components to the panels
  79. //add the buttons
  80. calculateButton = new JButton("Calculate");
  81. clearButton = new JButton("Clear");
  82.  
  83. //add buttons
  84. bottomPanel.add(calculateButton);
  85. bottomPanel.add(clearButton);
  86.  
  87. //register listeners
  88. calculateButton.addActionListener(babysitter);
  89. clearButton.addActionListener(babysitter);
  90.  
  91. //add components to the top panel
  92. JLabel labelOne = new JLabel("Celsius:");
  93. JLabel secondOne = new JLabel("Fahrenheit:");
  94. JLabel thirdOne = new JLabel("Kelvin:");
  95.  
  96. celsiusField = new JTextField("");
  97. fahrenheitField = new JTextField("");
  98. kelvinField = new JTextField("");
  99.  
  100. //add the label and text fields
  101. topPanel.add(labelOne);
  102. topPanel.add(celsiusField);
  103. topPanel.add(secondOne);
  104. topPanel.add(fahrenheitField);
  105. topPanel.add(thirdOne);
  106. topPanel.add(kelvinField);
  107.  
  108. this.setVisible(true);
  109.  
  110. } // end constructor
  111.  
  112. public static void main (String[] args) {
  113. new TempConverter();
  114. }
  115.  
  116.  
  117. private class MaryPoppins implements ActionListener
  118. {
  119.  
  120. //implement the abstract method from the interface
  121. public void actionPerformed(ActionEvent ev)
  122. {
  123. thisContentPane = getContentPane();
  124.  
  125. if(ev.getActionCommand().equals("LIGHTGRAY"))
  126. {
  127. thisContentPane.setBackground(Color.lightGray);
  128. }
  129. else if (ev.getActionCommand().equals("BLUE"))
  130. {
  131. thisContentPane.setBackground(Color.BLUE);
  132. }
  133. else if(ev.getActionCommand().equals("WHITE") )
  134. {
  135. thisContentPane.setBackground(Color.WHITE);
  136. }
  137. else if (ev.getActionCommand().equals("BLACK"))
  138. {
  139. thisContentPane.setBackground(Color.BLACK);
  140. }else if (ev.getActionCommand().equals("Clear"))
  141. {
  142. thisContentPane.setBackground(Color.BLACK);
  143. }
  144. else if (ev.getActionCommand().equals("BLACK"))
  145. {
  146. thisContentPane.setBackground(Color.BLACK);
  147. }
  148.  
  149.  
  150.  
  151. }//end ActionPerformed()
  152.  
  153. }//end inner class
  154.  
  155. } // end class
  156.  
  157. if (ev.getActionCommand().equals("LIGHTGRAY")) {
  158. thisTopPanel.setBackground(Color.lightGray);
  159. thisBottemPanel.setBackground(Color.lightGray);
  160. }
  161.  
  162. topPanel.setOpaque(false);
  163. bottomPanel.setOpaque(false);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement