Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import javax.swing.*;
- import java.awt.event.*;
- public class TempConverter extends JFrame
- {
- //Creating a contentPane down inside the inner class
- Container thisContentPane;
- //class scope variables : DO NOT CREATE THIS OBJECTS HERE.
- JButton calculateButton, clearButton;
- JTextField celsiusField, fahrenheitField, kelvinField;
- //menu
- JMenuBar menuBar = new JMenuBar();
- JMenu backgroundColor = new JMenu("Background Color");
- JMenu help = new JMenu("Help");
- JMenuItem lightGray, white, black, blue, howToUse, about;
- //constructor
- TempConverter()
- {
- super("Temperature Converter App");
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setLayout(new BorderLayout());
- this.setSize(400,200);;
- this.setLocationRelativeTo(null);
- //menuBar
- this.setJMenuBar(menuBar);
- menuBar.add(backgroundColor);
- //adding JMenu to JMenuBar
- menuBar.add(backgroundColor);
- menuBar.add(help);
- //adding JMenuItems
- lightGray = backgroundColor.add("LIGHTGRAY");
- white = backgroundColor.add("WHITE");
- black = backgroundColor.add("BLACK");
- blue = backgroundColor.add("BLUE");
- howToUse = help.add("How To Use");
- about = help.add("Help");
- //babysitter
- MaryPoppins babysitter = new MaryPoppins();
- //adding action listener to the menu item
- lightGray.addActionListener(babysitter);
- white.addActionListener(babysitter);
- black.addActionListener(babysitter);
- blue.addActionListener(babysitter);
- howToUse.addActionListener(babysitter);
- about.addActionListener(babysitter);
- //building JPanels
- JPanel topPanel = new JPanel();
- topPanel.setLayout(new GridLayout(3,2,0,20));
- //add this to JFrame in centerzone
- this.add(topPanel, BorderLayout.CENTER);
- //bottom panel
- JPanel bottomPanel = new JPanel();
- bottomPanel.setLayout(new FlowLayout());
- //add this to JFrame in bottom
- this.add(bottomPanel, BorderLayout.SOUTH);
- //add components to the panels
- //add the buttons
- calculateButton = new JButton("Calculate");
- clearButton = new JButton("Clear");
- //add buttons
- bottomPanel.add(calculateButton);
- bottomPanel.add(clearButton);
- //register listeners
- calculateButton.addActionListener(babysitter);
- clearButton.addActionListener(babysitter);
- //add components to the top panel
- JLabel labelOne = new JLabel("Celsius:");
- JLabel secondOne = new JLabel("Fahrenheit:");
- JLabel thirdOne = new JLabel("Kelvin:");
- celsiusField = new JTextField("");
- fahrenheitField = new JTextField("");
- kelvinField = new JTextField("");
- //add the label and text fields
- topPanel.add(labelOne);
- topPanel.add(celsiusField);
- topPanel.add(secondOne);
- topPanel.add(fahrenheitField);
- topPanel.add(thirdOne);
- topPanel.add(kelvinField);
- this.setVisible(true);
- } // end constructor
- public static void main (String[] args) {
- new TempConverter();
- }
- private class MaryPoppins implements ActionListener
- {
- //implement the abstract method from the interface
- public void actionPerformed(ActionEvent ev)
- {
- thisContentPane = getContentPane();
- if(ev.getActionCommand().equals("LIGHTGRAY"))
- {
- thisContentPane.setBackground(Color.lightGray);
- }
- else if (ev.getActionCommand().equals("BLUE"))
- {
- thisContentPane.setBackground(Color.BLUE);
- }
- else if(ev.getActionCommand().equals("WHITE") )
- {
- thisContentPane.setBackground(Color.WHITE);
- }
- else if (ev.getActionCommand().equals("BLACK"))
- {
- thisContentPane.setBackground(Color.BLACK);
- }else if (ev.getActionCommand().equals("Clear"))
- {
- thisContentPane.setBackground(Color.BLACK);
- }
- else if (ev.getActionCommand().equals("BLACK"))
- {
- thisContentPane.setBackground(Color.BLACK);
- }
- }//end ActionPerformed()
- }//end inner class
- } // end class
- if (ev.getActionCommand().equals("LIGHTGRAY")) {
- thisTopPanel.setBackground(Color.lightGray);
- thisBottemPanel.setBackground(Color.lightGray);
- }
- topPanel.setOpaque(false);
- bottomPanel.setOpaque(false);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement