Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- public class WindowApp extends JFrame {
- private final static int WINDOW_SIZE_X=700;
- private final static int WINDOW_SIZE_Y=400;
- private final static int LIST_SIZE_X=100;
- private final static int LIST_SIZE_Y=400;
- private final static int BUTTONS_QUANTITY=25;
- private final static int RADIOBUTTONS_QUANTITY=3;
- private final static int GRIDLAYOUT_ROWS=5;
- private final static int GRIDLAYOUT_COLS=5;
- private final static int GRIDLAYOUT_RADIO_ROWS=3;
- private final static int GRIDLAYOUT_RADIO_COLS=1;
- private final static int ICONS_QUANTITY=4;
- private enum PanelColors {RED,GREEN,BLUE};
- private Color [] colors = {Color.RED,Color.GREEN,Color.BLUE};
- JTabbedPane tabbedPane;
- JPanel panel1;
- JPanel panel2;
- JPanel panel3;
- JPanel panel;
- JButton leftButton;
- JButton rightButton;
- JList<String> list1;
- JList<String> list2;
- ImageIcon icons[];
- String iconFileNames[] = {"default.png","selected.png","pressed.png","over.png"};
- JButton [] buttons;
- JPanel radioButtonsPanel;
- JPanel colorPanel;
- JRadioButton [] radioButtons;
- ButtonGroup buttonGroup;
- DefaultListModel<String> model1;
- DefaultListModel<String> model2;
- String tempButtonName;
- Color tempButtonColor;
- WindowApp(){
- //tabbed pane
- tabbedPane=new JTabbedPane();
- //first tab
- initFirstTab();
- //second tab
- initSecondTab();
- //third tab
- initThirdTab();
- //window
- setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
- setResizable(false);
- setContentPane(tabbedPane);
- pack();
- }
- private void initThirdTab(){
- //main panel
- panel3 = new JPanel();
- //panel with radiobuttons
- radioButtonsPanel=new JPanel();
- radioButtonsPanel.setLayout(new GridLayout(GRIDLAYOUT_RADIO_ROWS,GRIDLAYOUT_RADIO_COLS));
- radioButtons = new JRadioButton[RADIOBUTTONS_QUANTITY];
- for(int i=0;i<RADIOBUTTONS_QUANTITY;i++){
- radioButtons[i]=new JRadioButton(PanelColors.values()[i].toString());
- radioButtonsPanel.add(radioButtons[i]);
- }
- panel3.add(radioButtonsPanel,BorderLayout.WEST);
- //one group
- buttonGroup=new ButtonGroup();
- for(int i=0;i<RADIOBUTTONS_QUANTITY;i++){
- buttonGroup.add(radioButtons[i]);
- }
- //color panel
- colorPanel=new JPanel();
- colorPanel.setPreferredSize(new Dimension(2*LIST_SIZE_X,LIST_SIZE_Y));
- panel3.add(colorPanel,BorderLayout.EAST);
- //add icons
- icons =new ImageIcon[ICONS_QUANTITY];
- for(int i=0;i<ICONS_QUANTITY; i++){
- icons[i]=new ImageIcon(iconFileNames[i]);
- }
- for(int i=0;i<RADIOBUTTONS_QUANTITY;i++){
- radioButtons[i].setIcon(icons[0]);
- radioButtons[i].setSelectedIcon(icons[1]);
- radioButtons[i].setPressedIcon(icons[2]);
- radioButtons[i].setRolloverIcon(icons[3]);
- }
- //default button
- radioButtons[0].setSelected(true);
- colorPanel.setBackground(colors[0]);
- //add panel to tabbed pane
- tabbedPane.add(panel3);
- //listener
- for(int i=0;i<RADIOBUTTONS_QUANTITY;i++) {
- radioButtons[i].addActionListener(new MyActionListener(i));
- }
- }
- private class MyActionListener implements ActionListener{
- private int i;
- MyActionListener(int i){
- this.i=i;
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- colorPanel.setBackground(colors[i]);
- }
- }
- private void initSecondTab(){
- //main panel
- panel2 = new JPanel();
- panel2.setLayout(new GridLayout(GRIDLAYOUT_ROWS,GRIDLAYOUT_COLS));
- //buttons
- MyMouseAdapter myMouseAdapter = new MyMouseAdapter();
- buttons=new JButton[BUTTONS_QUANTITY];
- for(int i=0; i<BUTTONS_QUANTITY; i++){
- buttons[i]=new JButton(Integer.toString(i));
- buttons[i].setOpaque(true);
- buttons[i].setBorderPainted(false);
- buttons[i].addMouseListener(myMouseAdapter);
- panel2.add(buttons[i]);
- }
- tabbedPane.add(panel2);
- }
- private void initFirstTab(){
- //main panel
- panel1 = new JPanel();
- panel1.setLayout(new BorderLayout());
- //init list
- String [] stringArray1 = {"Trump","Obama","Bush","Reagan","Clinton","Kennedy","Hoover"};
- String [] stringArray2 = {"Washington","Franklin","Lincoln", "Jackson","Grant"};
- model1 = new DefaultListModel<>();
- model2 = new DefaultListModel<>();
- for(String string : stringArray1){
- model1.addElement(string);
- }
- for(String string : stringArray2){
- model2.addElement(string);
- }
- list1=new JList<>(model1);
- list2=new JList<>(model2);
- list1.setPreferredSize(new Dimension(LIST_SIZE_X, LIST_SIZE_Y));
- list2.setPreferredSize(new Dimension(LIST_SIZE_X, LIST_SIZE_Y));
- panel1.add(list1,BorderLayout.WEST);
- panel1.add(list2,BorderLayout.EAST);
- panel = new JPanel();
- panel.setLayout(new BorderLayout());
- leftButton = new JButton("<");
- rightButton = new JButton(">");
- leftButton.setPreferredSize(new Dimension(100,50));
- rightButton.setPreferredSize(new Dimension(100,50));
- panel.add(leftButton, BorderLayout.SOUTH);
- panel.add(rightButton, BorderLayout.NORTH);
- panel1.add(panel,BorderLayout.CENTER);
- //add panel to tabbed pane
- tabbedPane.add(panel1);
- //listeners
- leftButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- int selectedIndices[] = list2.getSelectedIndices();
- for(int i=0; i<selectedIndices.length; i++){
- String selectedElement = model2.getElementAt(selectedIndices[i] - i);
- model2.remove(selectedIndices[i] - i);
- model1.addElement(selectedElement);
- }
- }
- });
- rightButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- int selectedIndices[] = list1.getSelectedIndices();
- for(int i=0; i<selectedIndices.length; i++){
- String selectedElement = model1.getElementAt(selectedIndices[i] - i);
- model1.remove(selectedIndices[i] - i);
- model2.addElement(selectedElement);
- }
- }
- });
- }
- class MyMouseAdapter extends MouseAdapter {
- @Override
- public void mousePressed(MouseEvent e) {
- JButton button = (JButton) e.getSource();
- tempButtonName = button.getText();
- button.setText("Clicked!");
- }
- @Override
- public void mouseReleased(MouseEvent e) {
- JButton button = (JButton) e.getSource();
- button.setText(tempButtonName);
- }
- @Override
- public void mouseEntered(MouseEvent e) {
- JButton button = (JButton) e.getSource();
- tempButtonColor = button.getBackground();
- button.setBackground(Color.RED);
- }
- @Override
- public void mouseExited(MouseEvent e) {
- JButton button = (JButton) e.getSource();
- button.setBackground(tempButtonColor);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement