Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package colorFactory;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- @SuppressWarnings("serial")
- public class ColorFactory extends JFrame {
- private JPanel panel;
- private JPanel panel2;
- private JPanel panel3;
- private JButton redButton;
- private JButton orangeButton;
- private JButton yellowButton;
- private JRadioButton greenButton;
- private JRadioButton blueButton;
- private JRadioButton cyanButton;
- private JLabel messageLabel;
- private final int WINDOW_WIDTH = 500; // Window Width
- private final int WINDOW_HEIGHT = 300; // Window Height
- public ColorFactory() {
- // Set the title bar text
- setTitle("Color Factory");
- // Set the size of the window
- setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
- // Close window when the exit button is clicked
- setDefualtCloseOperation(JFrame.EXIT_ON_CLOSE);
- // Add a FLowLayout manager to the content pane
- setLayout(new BorderLayout());
- // Create a label
- // Using a string to ease the input of the ()
- String text = "Top buttons change the panel color and bottom radio buttons change the text color";
- // Cheater method of using HTML code to center text, although its not
- // working how it should...
- messageLabel = new JLabel("<html><div style=\"text-align: center;\">"
- + text + "</html>");
- // My panels
- panel = new JPanel();
- panel2 = new JPanel();
- panel3 = new JPanel();
- // Display the window
- setVisible(true);
- // Set panels background colors
- panel.setBackground(Color.WHITE);
- panel2.setBackground(Color.WHITE);
- panel3.setBackground(Color.WHITE);
- // Add a FLowLayout manager to the content pane.
- panel.setLayout(new BorderLayout());
- // main center panel has the label
- panel.add(messageLabel);
- add(panel2, BorderLayout.NORTH);
- add(panel, BorderLayout.CENTER);
- add(panel3, BorderLayout.SOUTH);
- // Create the buttons.
- redButton = new JButton("Red");
- orangeButton = new JButton("Orange");
- yellowButton = new JButton("Yellow");
- // Declaring that panel2 has the buttons
- panel2.add(redButton);
- panel2.add(orangeButton);
- panel2.add(yellowButton);
- // Adding the action listeners to the buttons
- redButton.addActionListener(new RedButtonListener());
- orangeButton.addActionListener(new OrangeButtonListener());
- yellowButton.addActionListener(new YellowButtonListener());
- // Add the buttons color.
- redButton.setBackground(Color.RED);
- orangeButton.setBackground(Color.ORANGE);
- yellowButton.setBackground(Color.YELLOW);
- // Adding Radio Buttons
- greenButton = new JRadioButton("Green");
- blueButton = new JRadioButton("Blue");
- cyanButton = new JRadioButton("Cyan");
- // Declaring that panel3 has the buttons
- panel3.add(blueButton);
- panel3.add(greenButton);
- panel3.add(cyanButton);
- blueButton.addActionListener(new BlueButtonListener());
- greenButton.addActionListener(new GreenButtonListener());
- cyanButton.addActionListener(new CyanButtonListener());
- }
- private class RedButtonListener implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- panel.setBackground(Color.RED);
- }
- }
- private class OrangeButtonListener implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- panel.setBackground(Color.ORANGE);
- }
- }
- private class YellowButtonListener implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- panel.setBackground(Color.YELLOW);
- }
- }
- // Radio Buttons Events
- private class GreenButtonListener implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- messageLabel.setForeground(Color.GREEN);
- }
- }
- private class BlueButtonListener implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- messageLabel.setForeground(Color.BLUE);
- }
- }
- private class CyanButtonListener implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- messageLabel.setForeground(Color.CYAN);
- }
- }
- private void setDefualtCloseOperation(int exitOnClose) {
- }
- public static void main(String[] args) {
- new ColorFactory();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement