Advertisement
Svetlana_Ovsjanikova

Java = MoreGUI.task

Sep 21st, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.94 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 Viktorija
  13. */
  14. public class Menu extends javax.swing.JFrame {
  15.  
  16. /**
  17. * Creates new form Menu
  18. */
  19. private boolean stateBtnBlue = true;
  20. private boolean stateBtnRed = true;
  21. private boolean stateBtnGreen = true;
  22.  
  23. public Menu() {
  24. initComponents();
  25.  
  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. btnNew = new javax.swing.JButton();
  41. txtIevade = new javax.swing.JTextField();
  42. btnForText = new javax.swing.JButton();
  43.  
  44. setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  45.  
  46. btnRed.setFont(new java.awt.Font("Times New Roman", 1, 11)); // NOI18N
  47. btnRed.setText("Sarkans");
  48. btnRed.setActionCommand("btnred");
  49. btnRed.addActionListener(new java.awt.event.ActionListener() {
  50. public void actionPerformed(java.awt.event.ActionEvent evt) {
  51. btnRedActionPerformed(evt);
  52. }
  53. });
  54.  
  55. btnGreen.setFont(new java.awt.Font("Times New Roman", 1, 11)); // NOI18N
  56. btnGreen.setText("Zaļš");
  57. btnGreen.setToolTipText("");
  58. btnGreen.addActionListener(new java.awt.event.ActionListener() {
  59. public void actionPerformed(java.awt.event.ActionEvent evt) {
  60. btnGreenActionPerformed(evt);
  61. }
  62. });
  63.  
  64. btnBlue.setFont(new java.awt.Font("Times New Roman", 1, 11)); // NOI18N
  65. btnBlue.setText("Zils");
  66. btnBlue.addActionListener(new java.awt.event.ActionListener() {
  67. public void actionPerformed(java.awt.event.ActionEvent evt) {
  68. btnBlueActionPerformed(evt);
  69. }
  70. });
  71.  
  72. btnForText.setText("OK");
  73. btnForText.addActionListener(new java.awt.event.ActionListener() {
  74. public void actionPerformed(java.awt.event.ActionEvent evt) {
  75. btnForTextActionPerformed(evt);
  76. }
  77. });
  78.  
  79. javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  80. getContentPane().setLayout(layout);
  81. layout.setHorizontalGroup(
  82. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  83. .addGroup(layout.createSequentialGroup()
  84. .addGap(30, 30, 30)
  85. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
  86. .addComponent(btnNew, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  87. .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
  88. .addComponent(btnRed, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
  89. .addGap(18, 18, 18)
  90. .addComponent(btnGreen, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
  91. .addGap(28, 28, 28)
  92. .addComponent(btnBlue, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE))
  93. .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
  94. .addComponent(txtIevade, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
  95. .addGap(18, 18, 18)
  96. .addComponent(btnForText)))
  97. .addContainerGap(187, Short.MAX_VALUE))
  98. );
  99. layout.setVerticalGroup(
  100. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  101. .addGroup(layout.createSequentialGroup()
  102. .addGap(47, 47, 47)
  103. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  104. .addComponent(btnRed, javax.swing.GroupLayout.DEFAULT_SIZE, 44, Short.MAX_VALUE)
  105. .addComponent(btnBlue, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  106. .addComponent(btnGreen, javax.swing.GroupLayout.DEFAULT_SIZE, 44, Short.MAX_VALUE))
  107. .addGap(18, 18, 18)
  108. .addComponent(btnNew, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
  109. .addGap(33, 33, 33)
  110. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  111. .addComponent(btnForText)
  112. .addComponent(txtIevade))
  113. .addContainerGap(139, Short.MAX_VALUE))
  114. );
  115.  
  116. pack();
  117. }// </editor-fold>
  118.  
  119. private void btnBlueActionPerformed(java.awt.event.ActionEvent evt) {
  120. if (stateBtnBlue) {
  121. btnBlue.setBackground(Color.blue);
  122. btnBlue.setForeground(Color.black);
  123.  
  124. } else {
  125.  
  126. btnBlue.setBackground(Color.gray);
  127. btnBlue.setForeground(Color.yellow);
  128. }
  129. stateBtnBlue = !stateBtnBlue;
  130. changeColour();
  131.  
  132. }
  133.  
  134. private void btnRedActionPerformed(java.awt.event.ActionEvent evt) {
  135. if (stateBtnRed) {
  136. btnRed.setBackground(Color.red);
  137. btnRed.setForeground(Color.black);
  138. } else {
  139. btnRed.setBackground(Color.GRAY);
  140. btnRed.setForeground(Color.YELLOW);
  141. }
  142.  
  143. stateBtnRed = !stateBtnRed;
  144. changeColour();
  145.  
  146.  
  147. }
  148.  
  149. private void btnGreenActionPerformed(java.awt.event.ActionEvent evt) {
  150. if (stateBtnGreen) {
  151. btnGreen.setBackground(Color.green);
  152. btnGreen.setForeground(Color.black);
  153. } else {
  154. btnGreen.setBackground(Color.gray);
  155. btnGreen.setForeground(Color.yellow);
  156. }
  157. stateBtnGreen = !stateBtnGreen;
  158. changeColour();
  159.  
  160. }
  161.  
  162. private void btnForTextActionPerformed(java.awt.event.ActionEvent evt) {
  163. int a = Integer.parseInt(txtIevade.getText());
  164.  
  165. switch (a) {
  166. case 1:
  167. btnNew.setBackground(Color.red);
  168. break;
  169. case 2:
  170. btnNew.setBackground(Color.green);
  171. break;
  172. case 3:
  173. btnNew.setBackground(Color.blue);
  174. break;
  175. case 4:
  176. btnNew.setBackground(Color.yellow);
  177. break;
  178. case 5:
  179. btnNew.setBackground(Color.cyan);
  180. break;
  181. case 6:
  182. btnNew.setBackground(Color.magenta);
  183. break;
  184. case 7:
  185. btnNew.setBackground(Color.white);
  186. break;
  187. case 8:
  188. btnNew.setBackground(Color.gray);
  189. break;
  190. default:
  191. btnNew.setBackground(Color.gray);
  192. }
  193.  
  194. }
  195.  
  196. private void changeColour() {
  197. if (!stateBtnGreen && !stateBtnRed && stateBtnBlue) {
  198. btnNew.setBackground(Color.yellow);
  199. }
  200.  
  201. if (!stateBtnGreen && !stateBtnBlue && stateBtnRed) {
  202. btnNew.setBackground(Color.cyan);
  203. }
  204. if (!stateBtnRed && !stateBtnBlue && stateBtnGreen) {
  205. btnNew.setBackground(Color.magenta);
  206. }
  207.  
  208. if (!stateBtnRed && !stateBtnGreen && !stateBtnBlue) {
  209. btnNew.setBackground(Color.white);
  210. }
  211.  
  212. if (stateBtnRed && stateBtnGreen && stateBtnBlue) {
  213. btnNew.setBackground(Color.gray);
  214. }
  215.  
  216. if (stateBtnGreen && stateBtnRed && !stateBtnBlue) {
  217. btnNew.setBackground(Color.blue);
  218. }
  219.  
  220. if (stateBtnGreen && !stateBtnRed && stateBtnBlue) {
  221. btnNew.setBackground(Color.red);
  222. }
  223. if (!stateBtnGreen && stateBtnRed && stateBtnBlue) {
  224. btnNew.setBackground(Color.green);
  225. }
  226. }
  227.  
  228. /**
  229. * @param args the command line arguments
  230. */
  231. public static void main(String args[]) {
  232. /* Set the Nimbus look and feel */
  233. //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  234. /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  235. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  236. */
  237. try {
  238. for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  239. if ("Nimbus".equals(info.getName())) {
  240. javax.swing.UIManager.setLookAndFeel(info.getClassName());
  241. break;
  242. }
  243. }
  244. } catch (ClassNotFoundException ex) {
  245. java.util.logging.Logger.getLogger(Menu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  246. } catch (InstantiationException ex) {
  247. java.util.logging.Logger.getLogger(Menu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  248. } catch (IllegalAccessException ex) {
  249. java.util.logging.Logger.getLogger(Menu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  250. } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  251. java.util.logging.Logger.getLogger(Menu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  252. }
  253. //</editor-fold>
  254.  
  255. /* Create and display the form */
  256. java.awt.EventQueue.invokeLater(new Runnable() {
  257. public void run() {
  258. new Menu().setVisible(true);
  259.  
  260. }
  261. });
  262.  
  263. }
  264.  
  265. // Variables declaration - do not modify
  266. private javax.swing.JButton btnBlue;
  267. private javax.swing.JButton btnForText;
  268. private javax.swing.JButton btnGreen;
  269. private javax.swing.JButton btnNew;
  270. private javax.swing.JButton btnRed;
  271. private javax.swing.JTextField txtIevade;
  272. // End of variables declaration
  273.  
  274. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement