Advertisement
Guest User

fa

a guest
Feb 8th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  1. package lab;
  2.  
  3. import javax.swing.*;
  4.  
  5. import java.awt.*;
  6. import java.awt.event.*;
  7.  
  8. public class ColorSelectionGUI extends JFrame{
  9.  
  10. private JCheckBox northCB, southCB, eastCB, westCB;
  11. private JRadioButton redRB, greenRB, blueRB, cyanRB;
  12. private ButtonGroup ColorSelectGroup;
  13. private JPanel checkboxPanel, radioPanel, myButtons, northJP, southJP, eastJP, westJP;
  14. private JLabel northJL, southJL, eastJL, westJL;
  15. private BorderLayout borderLayoutMgr;
  16.  
  17. private final int WINDOW_WIDTH = 500;
  18. private final int WINDOW_HEIGHT = 250;
  19.  
  20. public ColorSelectionGUI() {
  21. setTitle("Color Selection GUI");
  22. setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
  23. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  24. buildGUI();
  25. setVisible(true);
  26. }
  27.  
  28. public void buildGUI(){
  29. Color red = new Color(255,0,0);
  30. Color blue = new Color(0,0,255);
  31. Color green = new Color(0,255,0);
  32. Color cyan = new Color(0,255,255);
  33.  
  34. borderLayoutMgr = new BorderLayout();
  35. Container pane = getContentPane();
  36.  
  37. northCB = new JCheckBox("North");
  38. southCB = new JCheckBox("South");
  39. eastCB = new JCheckBox("East");
  40. westCB = new JCheckBox("West");
  41.  
  42. redRB = new JRadioButton("Red");
  43. redRB.setForeground(red);
  44. greenRB = new JRadioButton("Green");
  45. greenRB.setForeground(green);
  46. blueRB = new JRadioButton("Blue");
  47. blueRB.setForeground(blue);
  48. cyanRB = new JRadioButton("Cyan");
  49. cyanRB.setForeground(cyan);
  50.  
  51. northJL = new JLabel("North");
  52. northJL.setHorizontalAlignment(JLabel.CENTER);
  53. northJL.setVerticalAlignment(JLabel.CENTER);
  54. southJL = new JLabel("South");
  55. southJL.setHorizontalAlignment(JLabel.CENTER);
  56. southJL.setVerticalAlignment(JLabel.CENTER);
  57. eastJL = new JLabel("East");
  58. eastJL.setHorizontalAlignment(JLabel.CENTER);
  59. eastJL.setVerticalAlignment(JLabel.CENTER);
  60. westJL = new JLabel("West");
  61. westJL.setHorizontalAlignment(JLabel.CENTER);
  62. westJL.setVerticalAlignment(JLabel.CENTER);
  63.  
  64. northJP = new JPanel();
  65. northJP.add(northJL);
  66. southJP = new JPanel();
  67. southJP.add(southJL);
  68. eastJP = new JPanel();
  69. eastJP.add(eastJL);
  70. westJP = new JPanel();
  71. westJP.add(westJL);
  72.  
  73. checkboxPanel = new JPanel();
  74. checkboxPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Locations"),BorderFactory.createBevelBorder(1)));
  75. checkboxPanel.add(northCB);
  76. checkboxPanel.add(southCB);
  77. checkboxPanel.add(eastCB);
  78. checkboxPanel.add(westCB);
  79. checkboxPanel.setLayout(new GridLayout(4,1));
  80.  
  81. radioPanel = new JPanel();
  82. radioPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Bg Color"), BorderFactory.createBevelBorder(1)));
  83. radioPanel.add(redRB);
  84. radioPanel.add(greenRB);
  85. radioPanel.add(blueRB);
  86. radioPanel.add(cyanRB);
  87. radioPanel.setLayout(new GridLayout(4,1));
  88.  
  89. ColorSelectGroup = new ButtonGroup();
  90. ColorSelectGroup.add(redRB);
  91. ColorSelectGroup.add(greenRB);
  92. ColorSelectGroup.add(blueRB);
  93. ColorSelectGroup.add(cyanRB);
  94. cyanRB.setSelected(true);
  95.  
  96. myButtons = new JPanel();
  97. myButtons.setLayout(new GridLayout(1,2));
  98. myButtons.add(checkboxPanel);
  99. myButtons.add(radioPanel);
  100.  
  101. pane.add(northJP, BorderLayout.NORTH);
  102. pane.add(westJP, BorderLayout.WEST);
  103. pane.add(myButtons, BorderLayout.CENTER);
  104. pane.add(eastJP, BorderLayout.EAST);
  105. pane.add(southJP, BorderLayout.SOUTH);
  106.  
  107. redRB.addActionListener(new RadioButtonListener());
  108. greenRB.addActionListener(new RadioButtonListener());
  109. blueRB.addActionListener(new RadioButtonListener());
  110. cyanRB.addActionListener(new RadioButtonListener());
  111. }
  112.  
  113. class RadioButtonListener implements ActionListener {
  114. public void actionPerformed(ActionEvent arg0) {
  115. Color color = null;
  116. if(redRB.isSelected()){
  117. color = Color.red;
  118. }
  119. if(greenRB.isSelected()){
  120. color = Color.green;
  121. }
  122. if(blueRB.isSelected()){
  123. color = Color.blue;
  124. }
  125. if(cyanRB.isSelected()){
  126. color = Color.cyan;
  127. }
  128.  
  129. if (northCB.isSelected()){
  130. northJP.setBackground(color);
  131. }
  132. if (eastCB.isSelected()){
  133. eastJP.setBackground(color);
  134. }
  135. if (southCB.isSelected()){
  136. southJP.setBackground(color);
  137. }
  138. if (westCB.isSelected()){
  139. westJP.setBackground(color);
  140. }
  141.  
  142. }
  143.  
  144. }
  145.  
  146. public static void main(String[] args) {
  147. ColorSelectionGUI flow = new ColorSelectionGUI();
  148. }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement