Advertisement
mrkarp

Untitled

Oct 3rd, 2013
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.16 KB | None | 0 0
  1. package colorFactory;
  2.  
  3. import javax.swing.*;
  4.  
  5. import java.awt.*;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8.  
  9. @SuppressWarnings("serial")
  10. public class ColorFactory extends JFrame {
  11.  
  12. private JPanel panel;
  13. private JPanel panel2;
  14. private JPanel panel3;
  15. private JButton redButton;
  16. private JButton orangeButton;
  17. private JButton yellowButton;
  18. private JRadioButton greenButton;
  19. private JRadioButton blueButton;
  20. private JRadioButton cyanButton;
  21. private JLabel messageLabel;
  22. private final int WINDOW_WIDTH = 500; // Window Width
  23. private final int WINDOW_HEIGHT = 300; // Window Height
  24.  
  25. public ColorFactory() {
  26. // Set the title bar text
  27. setTitle("Color Factory");
  28.  
  29. // Set the size of the window
  30. setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
  31.  
  32. // Close window when the exit button is clicked
  33. setDefualtCloseOperation(JFrame.EXIT_ON_CLOSE);
  34.  
  35. // Add a FLowLayout manager to the content pane
  36. setLayout(new BorderLayout());
  37.  
  38. // Create a label
  39. // Using a string to ease the input of the ()
  40. String text = "Top buttons change the panel color and bottom radio buttons change the text color";
  41. // Cheater method of using HTML code to center text, although its not
  42. // working how it should...
  43. messageLabel = new JLabel("<html><div style=\"text-align: center;\">"
  44. + text + "</html>");
  45.  
  46. // My panels
  47. panel = new JPanel();
  48. panel2 = new JPanel();
  49. panel3 = new JPanel();
  50.  
  51. // Display the window
  52. setVisible(true);
  53.  
  54. // Set panels background colors
  55. panel.setBackground(Color.WHITE);
  56. panel2.setBackground(Color.WHITE);
  57. panel3.setBackground(Color.WHITE);
  58.  
  59. // Add a FLowLayout manager to the content pane.
  60. panel.setLayout(new BorderLayout());
  61.  
  62. // main center panel has the label
  63. panel.add(messageLabel);
  64.  
  65. add(panel2, BorderLayout.NORTH);
  66. add(panel, BorderLayout.CENTER);
  67. add(panel3, BorderLayout.SOUTH);
  68.  
  69. // Create the buttons.
  70. redButton = new JButton("Red");
  71. orangeButton = new JButton("Orange");
  72. yellowButton = new JButton("Yellow");
  73.  
  74. // Declaring that panel2 has the buttons
  75. panel2.add(redButton);
  76. panel2.add(orangeButton);
  77. panel2.add(yellowButton);
  78.  
  79. // Adding the action listeners to the buttons
  80. redButton.addActionListener(new RedButtonListener());
  81. orangeButton.addActionListener(new OrangeButtonListener());
  82. yellowButton.addActionListener(new YellowButtonListener());
  83.  
  84. // Add the buttons color.
  85. redButton.setBackground(Color.RED);
  86. orangeButton.setBackground(Color.ORANGE);
  87. yellowButton.setBackground(Color.YELLOW);
  88.  
  89. // Adding Radio Buttons
  90. greenButton = new JRadioButton("Green");
  91. blueButton = new JRadioButton("Blue");
  92. cyanButton = new JRadioButton("Cyan");
  93.  
  94. // Declaring that panel3 has the buttons
  95. panel3.add(blueButton);
  96. panel3.add(greenButton);
  97. panel3.add(cyanButton);
  98.  
  99. blueButton.addActionListener(new BlueButtonListener());
  100. greenButton.addActionListener(new GreenButtonListener());
  101. cyanButton.addActionListener(new CyanButtonListener());
  102. }
  103.  
  104. private class RedButtonListener implements ActionListener {
  105.  
  106. public void actionPerformed(ActionEvent e) {
  107. panel.setBackground(Color.RED);
  108.  
  109. }
  110.  
  111. }
  112.  
  113. private class OrangeButtonListener implements ActionListener {
  114.  
  115. public void actionPerformed(ActionEvent e) {
  116. panel.setBackground(Color.ORANGE);
  117.  
  118. }
  119.  
  120. }
  121.  
  122. private class YellowButtonListener implements ActionListener {
  123.  
  124. public void actionPerformed(ActionEvent e) {
  125. panel.setBackground(Color.YELLOW);
  126.  
  127. }
  128.  
  129. }
  130.  
  131. // Radio Buttons Events
  132. private class GreenButtonListener implements ActionListener {
  133.  
  134. public void actionPerformed(ActionEvent e) {
  135. messageLabel.setForeground(Color.GREEN);
  136.  
  137. }
  138.  
  139. }
  140.  
  141. private class BlueButtonListener implements ActionListener {
  142.  
  143. public void actionPerformed(ActionEvent e) {
  144. messageLabel.setForeground(Color.BLUE);
  145.  
  146. }
  147.  
  148. }
  149.  
  150. private class CyanButtonListener implements ActionListener {
  151.  
  152. public void actionPerformed(ActionEvent e) {
  153. messageLabel.setForeground(Color.CYAN);
  154.  
  155. }
  156.  
  157. }
  158.  
  159. private void setDefualtCloseOperation(int exitOnClose) {
  160. }
  161.  
  162. public static void main(String[] args) {
  163. new ColorFactory();
  164. }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement