Advertisement
YauhenMardan

Untitled

Dec 18th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.74 KB | None | 0 0
  1. package com.company;
  2.  
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6.  
  7. public class WindowApp extends JFrame {
  8. private final static int WINDOW_SIZE_X=700;
  9. private final static int WINDOW_SIZE_Y=400;
  10. private final static int LIST_SIZE_X=100;
  11. private final static int LIST_SIZE_Y=400;
  12.  
  13. private final static int BUTTONS_QUANTITY=25;
  14. private final static int RADIOBUTTONS_QUANTITY=3;
  15.  
  16. private final static int GRIDLAYOUT_ROWS=5;
  17. private final static int GRIDLAYOUT_COLS=5;
  18.  
  19. private final static int GRIDLAYOUT_RADIO_ROWS=3;
  20. private final static int GRIDLAYOUT_RADIO_COLS=1;
  21.  
  22. private final static int ICONS_QUANTITY=4;
  23.  
  24. private enum PanelColors {RED,GREEN,BLUE};
  25. private Color [] colors = {Color.RED,Color.GREEN,Color.BLUE};
  26. JTabbedPane tabbedPane;
  27. JPanel panel1;
  28. JPanel panel2;
  29. JPanel panel3;
  30. JPanel panel;
  31. JButton leftButton;
  32. JButton rightButton;
  33. JList<String> list1;
  34. JList<String> list2;
  35. ImageIcon icons[];
  36. String iconFileNames[] = {"default.png","selected.png","pressed.png","over.png"};
  37. JButton [] buttons;
  38.  
  39. JPanel radioButtonsPanel;
  40. JPanel colorPanel;
  41. JRadioButton [] radioButtons;
  42. ButtonGroup buttonGroup;
  43.  
  44. DefaultListModel<String> model1;
  45. DefaultListModel<String> model2;
  46.  
  47. String tempButtonName;
  48. Color tempButtonColor;
  49.  
  50. WindowApp(){
  51. //tabbed pane
  52. tabbedPane=new JTabbedPane();
  53. //first tab
  54. initFirstTab();
  55. //second tab
  56. initSecondTab();
  57. //third tab
  58. initThirdTab();
  59. //window
  60. setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  61. setResizable(false);
  62. setContentPane(tabbedPane);
  63. pack();
  64. }
  65. private void initThirdTab(){
  66. //main panel
  67. panel3 = new JPanel();
  68. //panel with radiobuttons
  69. radioButtonsPanel=new JPanel();
  70. radioButtonsPanel.setLayout(new GridLayout(GRIDLAYOUT_RADIO_ROWS,GRIDLAYOUT_RADIO_COLS));
  71. radioButtons = new JRadioButton[RADIOBUTTONS_QUANTITY];
  72. for(int i=0;i<RADIOBUTTONS_QUANTITY;i++){
  73. radioButtons[i]=new JRadioButton(PanelColors.values()[i].toString());
  74. radioButtonsPanel.add(radioButtons[i]);
  75. }
  76. panel3.add(radioButtonsPanel,BorderLayout.WEST);
  77. //one group
  78. buttonGroup=new ButtonGroup();
  79. for(int i=0;i<RADIOBUTTONS_QUANTITY;i++){
  80. buttonGroup.add(radioButtons[i]);
  81. }
  82. //color panel
  83. colorPanel=new JPanel();
  84. colorPanel.setPreferredSize(new Dimension(2*LIST_SIZE_X,LIST_SIZE_Y));
  85. panel3.add(colorPanel,BorderLayout.EAST);
  86. //add icons
  87. icons =new ImageIcon[ICONS_QUANTITY];
  88. for(int i=0;i<ICONS_QUANTITY; i++){
  89. icons[i]=new ImageIcon(iconFileNames[i]);
  90. }
  91. for(int i=0;i<RADIOBUTTONS_QUANTITY;i++){
  92. radioButtons[i].setIcon(icons[0]);
  93. radioButtons[i].setSelectedIcon(icons[1]);
  94. radioButtons[i].setPressedIcon(icons[2]);
  95. radioButtons[i].setRolloverIcon(icons[3]);
  96. }
  97. //default button
  98. radioButtons[0].setSelected(true);
  99. colorPanel.setBackground(colors[0]);
  100. //add panel to tabbed pane
  101. tabbedPane.add(panel3);
  102. //listener
  103. for(int i=0;i<RADIOBUTTONS_QUANTITY;i++) {
  104. radioButtons[i].addActionListener(new MyActionListener(i));
  105. }
  106.  
  107. }
  108. private class MyActionListener implements ActionListener{
  109.  
  110. private int i;
  111.  
  112. MyActionListener(int i){
  113. this.i=i;
  114. }
  115. @Override
  116. public void actionPerformed(ActionEvent e) {
  117. colorPanel.setBackground(colors[i]);
  118. }
  119. }
  120. private void initSecondTab(){
  121. //main panel
  122. panel2 = new JPanel();
  123. panel2.setLayout(new GridLayout(GRIDLAYOUT_ROWS,GRIDLAYOUT_COLS));
  124. //buttons
  125. MyMouseAdapter myMouseAdapter = new MyMouseAdapter();
  126. buttons=new JButton[BUTTONS_QUANTITY];
  127. for(int i=0; i<BUTTONS_QUANTITY; i++){
  128. buttons[i]=new JButton(Integer.toString(i));
  129. buttons[i].setOpaque(true);
  130. buttons[i].setBorderPainted(false);
  131. buttons[i].addMouseListener(myMouseAdapter);
  132. panel2.add(buttons[i]);
  133. }
  134. tabbedPane.add(panel2);
  135. }
  136. private void initFirstTab(){
  137. //main panel
  138. panel1 = new JPanel();
  139. panel1.setLayout(new BorderLayout());
  140. //init list
  141. String [] stringArray1 = {"Trump","Obama","Bush","Reagan","Clinton","Kennedy","Hoover"};
  142. String [] stringArray2 = {"Washington","Franklin","Lincoln", "Jackson","Grant"};
  143. model1 = new DefaultListModel<>();
  144. model2 = new DefaultListModel<>();
  145. for(String string : stringArray1){
  146. model1.addElement(string);
  147. }
  148. for(String string : stringArray2){
  149. model2.addElement(string);
  150. }
  151. list1=new JList<>(model1);
  152. list2=new JList<>(model2);
  153. list1.setPreferredSize(new Dimension(LIST_SIZE_X, LIST_SIZE_Y));
  154. list2.setPreferredSize(new Dimension(LIST_SIZE_X, LIST_SIZE_Y));
  155. panel1.add(list1,BorderLayout.WEST);
  156. panel1.add(list2,BorderLayout.EAST);
  157. panel = new JPanel();
  158. panel.setLayout(new BorderLayout());
  159. leftButton = new JButton("<");
  160. rightButton = new JButton(">");
  161. leftButton.setPreferredSize(new Dimension(100,50));
  162. rightButton.setPreferredSize(new Dimension(100,50));
  163. panel.add(leftButton, BorderLayout.SOUTH);
  164. panel.add(rightButton, BorderLayout.NORTH);
  165. panel1.add(panel,BorderLayout.CENTER);
  166. //add panel to tabbed pane
  167. tabbedPane.add(panel1);
  168. //listeners
  169. leftButton.addActionListener(new ActionListener() {
  170. @Override
  171. public void actionPerformed(ActionEvent e) {
  172. int selectedIndices[] = list2.getSelectedIndices();
  173. for(int i=0; i<selectedIndices.length; i++){
  174. String selectedElement = model2.getElementAt(selectedIndices[i] - i);
  175. model2.remove(selectedIndices[i] - i);
  176. model1.addElement(selectedElement);
  177. }
  178. }
  179. });
  180. rightButton.addActionListener(new ActionListener() {
  181. @Override
  182. public void actionPerformed(ActionEvent e) {
  183. int selectedIndices[] = list1.getSelectedIndices();
  184. for(int i=0; i<selectedIndices.length; i++){
  185. String selectedElement = model1.getElementAt(selectedIndices[i] - i);
  186. model1.remove(selectedIndices[i] - i);
  187. model2.addElement(selectedElement);
  188. }
  189. }
  190. });
  191. }
  192. class MyMouseAdapter extends MouseAdapter {
  193. @Override
  194. public void mousePressed(MouseEvent e) {
  195. JButton button = (JButton) e.getSource();
  196. tempButtonName = button.getText();
  197. button.setText("Clicked!");
  198. }
  199.  
  200. @Override
  201. public void mouseReleased(MouseEvent e) {
  202. JButton button = (JButton) e.getSource();
  203. button.setText(tempButtonName);
  204. }
  205.  
  206. @Override
  207. public void mouseEntered(MouseEvent e) {
  208. JButton button = (JButton) e.getSource();
  209. tempButtonColor = button.getBackground();
  210. button.setBackground(Color.RED);
  211. }
  212.  
  213. @Override
  214. public void mouseExited(MouseEvent e) {
  215. JButton button = (JButton) e.getSource();
  216. button.setBackground(tempButtonColor);
  217. }
  218. }
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement