Advertisement
Svetlana_Ovsjanikova

Java - MoreGUI.source

Sep 21st, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.33 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package main;
  7.  
  8. import java.awt.Color;
  9.  
  10. /**
  11. *
  12. * @author sao_7
  13. */
  14. public class Menu extends javax.swing.JFrame {
  15.  
  16. /**
  17. * Creates new form Menu
  18. */
  19. private boolean stateBtnRed = true;
  20. private boolean stateBtnGreen = true;
  21. private boolean stateBtnBlue = true;
  22. private boolean stateBtnMixed = true;
  23.  
  24. public Menu() {
  25. initComponents();
  26. }
  27.  
  28. /**
  29. * This method is called from within the constructor to initialize the form.
  30. * WARNING: Do NOT modify this code. The content of this method is always
  31. * regenerated by the Form Editor.
  32. */
  33. @SuppressWarnings("unchecked")
  34. // <editor-fold defaultstate="collapsed" desc="Generated Code">
  35. private void initComponents() {
  36.  
  37. btnRed = new javax.swing.JButton();
  38. btnGreen = new javax.swing.JButton();
  39. btnBlue = new javax.swing.JButton();
  40. btnMixed = new javax.swing.JButton();
  41.  
  42. setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  43.  
  44. btnRed.setBackground(new java.awt.Color(102, 153, 255));
  45. btnRed.setText("Sarkans");
  46. btnRed.addActionListener(new java.awt.event.ActionListener() {
  47. public void actionPerformed(java.awt.event.ActionEvent evt) {
  48. btnRedActionPerformed(evt);
  49. }
  50. });
  51.  
  52. btnGreen.setBackground(new java.awt.Color(102, 204, 255));
  53. btnGreen.setText("Zaļš");
  54. btnGreen.addActionListener(new java.awt.event.ActionListener() {
  55. public void actionPerformed(java.awt.event.ActionEvent evt) {
  56. btnGreenActionPerformed(evt);
  57. }
  58. });
  59.  
  60. btnBlue.setBackground(new java.awt.Color(102, 204, 255));
  61. btnBlue.setText("Zils");
  62. btnBlue.addActionListener(new java.awt.event.ActionListener() {
  63. public void actionPerformed(java.awt.event.ActionEvent evt) {
  64. btnBlueActionPerformed(evt);
  65. }
  66. });
  67.  
  68. btnMixed.addActionListener(new java.awt.event.ActionListener() {
  69. public void actionPerformed(java.awt.event.ActionEvent evt) {
  70. btnMixedActionPerformed(evt);
  71. }
  72. });
  73.  
  74. javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  75. getContentPane().setLayout(layout);
  76. layout.setHorizontalGroup(
  77. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  78. .addGroup(layout.createSequentialGroup()
  79. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  80. .addGroup(layout.createSequentialGroup()
  81. .addGap(31, 31, 31)
  82. .addComponent(btnRed, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
  83. .addGap(33, 33, 33)
  84. .addComponent(btnGreen, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
  85. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  86. .addComponent(btnBlue, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE))
  87. .addGroup(layout.createSequentialGroup()
  88. .addGap(54, 54, 54)
  89. .addComponent(btnMixed, javax.swing.GroupLayout.PREFERRED_SIZE, 256, javax.swing.GroupLayout.PREFERRED_SIZE)))
  90. .addContainerGap(82, Short.MAX_VALUE))
  91. );
  92. layout.setVerticalGroup(
  93. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  94. .addGroup(layout.createSequentialGroup()
  95. .addGap(33, 33, 33)
  96. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  97. .addComponent(btnRed, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
  98. .addComponent(btnGreen, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
  99. .addComponent(btnBlue, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))
  100. .addGap(47, 47, 47)
  101. .addComponent(btnMixed, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
  102. .addContainerGap(70, Short.MAX_VALUE))
  103. );
  104.  
  105. pack();
  106. }// </editor-fold>
  107.  
  108. private void btnRedActionPerformed(java.awt.event.ActionEvent evt) {
  109. if (stateBtnRed) {
  110. btnRed.setBackground(Color.red);
  111. btnRed.setForeground(Color.white);
  112.  
  113. } else {
  114. btnRed.setBackground(Color.gray);
  115. btnRed.setForeground(Color.black);
  116. }
  117.  
  118. if (stateBtnRed && stateBtnGreen) {
  119. btnMixed.setBackground(Color.yellow);
  120. }
  121. stateBtnRed = !stateBtnRed;
  122. mixBtnStateChange();
  123.  
  124. }
  125.  
  126. private void btnGreenActionPerformed(java.awt.event.ActionEvent evt) {
  127. if (stateBtnGreen) {
  128. btnGreen.setBackground(Color.green);
  129. btnGreen.setForeground(Color.white);
  130. // if (!stateBtnRed && !stateBtnGreen) {
  131. // btnMixed.setBackground(Color.yellow);
  132. // }
  133. } else {
  134. btnGreen.setBackground(Color.gray);
  135. btnGreen.setForeground(Color.black);
  136. }
  137. stateBtnGreen = !stateBtnGreen;
  138. mixBtnStateChange();
  139. }
  140.  
  141. private void btnBlueActionPerformed(java.awt.event.ActionEvent evt) {
  142. if (stateBtnBlue) {
  143. btnBlue.setBackground(Color.blue);
  144. btnBlue.setForeground(Color.white);
  145. } else {
  146. btnBlue.setBackground(Color.gray);
  147. btnBlue.setForeground(Color.black);
  148. }
  149. stateBtnBlue = !stateBtnBlue;
  150. mixBtnStateChange();
  151.  
  152. }
  153.  
  154. private void btnMixedActionPerformed(java.awt.event.ActionEvent evt) {
  155.  
  156. btnRed.setBackground(Color.gray);
  157. stateBtnRed = false;
  158. btnGreen.setBackground(Color.gray);
  159. stateBtnGreen = false;
  160. btnBlue.setBackground(Color.gray);
  161. stateBtnBlue = false;
  162.  
  163. btnMixed.setBackground(Color.gray);
  164.  
  165. }
  166.  
  167. private void mixBtnStateChange(){
  168. if (!stateBtnGreen && !stateBtnRed && stateBtnBlue) {
  169. btnMixed.setBackground(Color.yellow);
  170. }
  171.  
  172. if (!stateBtnGreen && !stateBtnBlue && stateBtnRed) {
  173. btnMixed.setBackground(Color.cyan);
  174. }
  175. if (!stateBtnRed && !stateBtnBlue && stateBtnGreen) {
  176. btnMixed.setBackground(Color.magenta);
  177. }
  178.  
  179. if (!stateBtnRed && !stateBtnGreen && !stateBtnBlue) {
  180. btnMixed.setBackground(Color.white);
  181. }
  182.  
  183. if (stateBtnRed && stateBtnGreen && stateBtnBlue) {
  184. btnMixed.setBackground(Color.gray);
  185. }
  186.  
  187. if (stateBtnGreen && stateBtnRed && !stateBtnBlue) {
  188. btnMixed.setBackground(Color.blue);
  189. }
  190.  
  191. if (stateBtnGreen && !stateBtnRed && stateBtnBlue) {
  192. btnMixed.setBackground(Color.red);
  193. }
  194. if (!stateBtnGreen && stateBtnRed && stateBtnBlue) {
  195. btnMixed.setBackground(Color.green);
  196. }
  197. differentMixedStateChange();
  198. }
  199.  
  200. private void differentMixedStateChange(){
  201. String result = String.valueOf(stateBtnRed) + String.valueOf(stateBtnGreen)
  202. + String.valueOf(stateBtnBlue);
  203. switch(result){
  204. case "truefalsefalse":
  205. btnMixed.setBackground(Color.red);
  206. break;
  207. case "falsetruefalse":
  208. btnMixed.setBackground(Color.green);
  209. break;
  210. }
  211. }
  212. /**
  213. * @param args the command line arguments
  214. */
  215. public static void main(String args[]) {
  216. /* Set the Nimbus look and feel */
  217. //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  218. /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  219. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  220. */
  221. try {
  222. for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  223. if ("Nimbus".equals(info.getName())) {
  224. javax.swing.UIManager.setLookAndFeel(info.getClassName());
  225. break;
  226. }
  227. }
  228. } catch (ClassNotFoundException ex) {
  229. java.util.logging.Logger.getLogger(Menu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  230. } catch (InstantiationException ex) {
  231. java.util.logging.Logger.getLogger(Menu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  232. } catch (IllegalAccessException ex) {
  233. java.util.logging.Logger.getLogger(Menu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  234. } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  235. java.util.logging.Logger.getLogger(Menu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  236. }
  237. //</editor-fold>
  238.  
  239. /* Create and display the form */
  240. java.awt.EventQueue.invokeLater(new Runnable() {
  241. public void run() {
  242. new Menu().setVisible(true);
  243. }
  244. });
  245. }
  246.  
  247. // Variables declaration - do not modify
  248. private javax.swing.JButton btnBlue;
  249. private javax.swing.JButton btnGreen;
  250. private javax.swing.JButton btnMixed;
  251. private javax.swing.JButton btnRed;
  252. // End of variables declaration
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement