Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.55 KB | None | 0 0
  1.  
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import java.awt.BorderLayout;
  5. import java.awt.Color;
  6. import java.awt.Dimension;
  7.  
  8. import javax.swing.BorderFactory;
  9. import javax.swing.JButton;
  10. import javax.swing.JComponent;
  11. import javax.swing.JFrame;
  12. import javax.swing.JPanel;
  13. import javax.swing.JMenuBar;
  14. import javax.swing.JMenu;
  15. import javax.swing.JMenuItem;
  16. import javax.swing.JColorChooser;
  17.  
  18.  
  19.  
  20.  
  21. public class prog2 extends JPanel
  22. {
  23.  
  24. int i = 0;
  25. Color[] buttonColor = {Color.RED, Color.ORANGE, Color.YELLOW, Color.GREEN, Color.BLUE, Color.MAGENTA};
  26. //Array to hold colors
  27. JButton colbutton = new JButton("Click Here");
  28. //Button that will change colors
  29. JButton bahbutton = new JButton("Don't Click");
  30. //the second placeholderbutton
  31. Dimension size1 = new Dimension(100, 50);
  32. //Dimension for size setting
  33. private JMenuItem colorRed, colorOrange, colorYellow, colorGreen, colorBlue, colorViolet;
  34.  
  35. public prog2()
  36. {
  37.  
  38. super(new BorderLayout());
  39.  
  40. //setting up for color changing
  41. this.add(colbutton, BorderLayout.CENTER);
  42. //places the main button in the center of the layout
  43. colbutton.setMaximumSize(size1);
  44. colbutton.setMinimumSize(size1);
  45. //attempts to set min and max, but at least defines the starting size of the buttons
  46. this.add(bahbutton, BorderLayout.SOUTH);
  47. //places the secondary button below the first
  48. bahbutton.setMinimumSize(size1);
  49. bahbutton.setMaximumSize(size1);
  50. //attempts to set min and max, but at least defines the starting size of the buttons
  51. this.setBorder(BorderFactory.createEmptyBorder(50,50,50,50));
  52. //places empty space around the buttons
  53.  
  54. JMenuBar mainMenu = new JMenuBar();
  55. this.setJMenuBar(mainMenu);
  56. JMenu colorMenuMain = new JMenu ("Colors");
  57. JMenu colorChoose = new JMenu ("Chooser");
  58. mainMenu.add(colorMenuMain);
  59. colorMenuMain.add(colorRed = new JMenuItem("Red"));
  60. colorMenuMain.add(colorOrange = new JMenuItem("Orange"));
  61. colorMenuMain.add(colorYellow = new JMenuItem("Yellow"));
  62. colorMenuMain.add(colorGreen = new JMenuItem("Green"));
  63. colorMenuMain.add(colorBlue = new JMenuItem("Blue"));
  64. colorMenuMain.add(colorViolet = new JMenuItem("Violet"));
  65.  
  66. JColorChooser colchoose = new JColorChooser();
  67. colchoose.setBorder(BorderFactory.createTitledBorder("Choose Text Color"));
  68. colchoose.setPreviewPanel(new JPanel());
  69. colchoose.setColor(this.getForeground());
  70.  
  71.  
  72. colbutton.addActionListener(new ActionListener() {
  73. public void actionPerformed(ActionEvent e)
  74. {
  75. i = ++i < buttonColor.length ? i : 0;
  76. colbutton.setBackground(buttonColor[i]);
  77. //increases I as the action is detected
  78. }
  79. });
  80. bahbutton.addActionListener(new ActionListener() {
  81.  
  82. public void actionPerformed(ActionEvent e)
  83. {
  84. bahbutton.setBackground(buttonColor[i]);
  85. //Turn button color back to default? idk
  86.  
  87. }
  88.  
  89. });
  90.  
  91.  
  92. colorRed.addActionListener(new ActionListener() {
  93. public void actionPerformed(ActionEvent e)
  94. {
  95. bahbutton.setBackground(buttonColor[0]);
  96. }
  97. });
  98.  
  99. colorOrange.addActionListener(new ActionListener() {
  100. public void actionPerformed(ActionEvent e)
  101. {
  102. bahbutton.setBackground(buttonColor[1]);
  103. }
  104. });
  105.  
  106. colorYellow.addActionListener(new ActionListener() {
  107. public void actionPerformed(ActionEvent e)
  108. {
  109. bahbutton.setBackground(buttonColor[2]);
  110. }
  111. });
  112.  
  113. colorGreen.addActionListener(new ActionListener() {
  114. public void actionPerformed(ActionEvent e)
  115. {
  116. bahbutton.setBackground(buttonColor[3]);
  117. }
  118. });
  119. colorBlue.addActionListener(new ActionListener() {
  120. public void actionPerformed(ActionEvent e)
  121. {
  122. bahbutton.setBackground(buttonColor[4]);
  123. }
  124. });
  125.  
  126. colorViolet.addActionListener(new ActionListener() {
  127. public void actionPerformed(ActionEvent e)
  128. {
  129. bahbutton.setBackground(buttonColor[5]);
  130. }
  131. });
  132. colorChoose.addActionListener(new ActionListener() {
  133. public void actionPerformed(ActionEvent e)
  134. {
  135. Color newColor = JColorChooser.showDialog(
  136. prog2.this,
  137. "Choose Background Color",
  138. this.getBackground());
  139. if (newColor != null)
  140. {
  141. this.setForeground(newColor);
  142. }
  143. }
  144. });
  145. colchoose.getSelectionModel().addActionListener(new ActionListener() {
  146. public void actionPerformed(ActionEvent e)
  147. {
  148. //Do I need a new listener to change the color, or?
  149. }
  150. });
  151.  
  152. }
  153.  
  154.  
  155. private static void run() {
  156.  
  157. JFrame frame = new JFrame("Color Changing");
  158. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  159. //standard exiting the program when it's closed
  160.  
  161. JComponent contentPane = new prog2();
  162. //creates content based on the constructor
  163. contentPane.setOpaque(true);
  164. frame.setTitle("Color Changing");
  165. //makes a title! doesn't do much, though.
  166.  
  167. frame.setContentPane(contentPane);
  168. frame.pack();
  169.  
  170.  
  171.  
  172.  
  173. frame.setVisible(true);
  174. //standard pane initialization
  175.  
  176.  
  177.  
  178. }
  179.  
  180. public static void main(String[] args)
  181.  
  182. {
  183. run();
  184. }
  185.  
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement