简体   繁体   中英

How Can I Copy a JLabel From One Panel To Another While Swapping The Image?

I have the following layout...

在此处输入图片说明

What I would like to do ideally is, when a user clicks on a JLabel in the scrolling panel on the bottom right, attach a different version of the image (one without the costs and name) to the mouse cursor. Then when the user clicks inside the giant floor plan panel, it places a copy of a JLabel using the swapped image each time they click. When another label is selected the same thing happens. When they push the selection button (Hand Icon) or press ESC the label is unselected.

Here is the code I currently have unmodified...

// =================================================================
//  Restaurant Builder - Interface
//  CSC - 540
//  10/22/13
//  Kris Purdy, Chris Fletemier
// =================================================================

package layout.manager;

import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

public class Interface extends JFrame implements ActionListener
{
    // Class variables
    private static final int SCREEN_WIDTH = 1366;
    private static final int SCREEN_HEIGHT = 700;
    private static final int FLOORPLAN_WIDTH = 1150;
    private static final int FLOORPLAN_HEIGHT = SCREEN_HEIGHT;
    private static final int BUTTONS_WIDTH = SCREEN_WIDTH - FLOORPLAN_WIDTH;
    private static final int BUTTONS_HEIGHT = 280;
    private static final int OBJECTLIST_WIDTH = BUTTONS_WIDTH;
    private static final int OBJECTLIST_HEIGHT = SCREEN_HEIGHT - BUTTONS_HEIGHT;
    private static final int BTN_WIDTH = 50;
    private static final int BTN_HEIGHT = 50;
    private static final int LABELS_WIDTH = 180;
    private static final int LABELS_HEIGHT = 50;
    private static final int OBJECT_WIDTH = 180;
    private static final int OBJECT_HEIGHT = 225;

    private final Floorplan floorplan;
    private Estimate estimate;

    private final ImageIcon leftRotateImg;
    private final ImageIcon rightRotateImg;
    private final ImageIcon upImg;
    private final ImageIcon downImg;
    private final ImageIcon leftImg;
    private final ImageIcon rightImg;
    private final ImageIcon costImg;
    private final ImageIcon deleteImg;
    private final ImageIcon selectionImg;
    private final ImageIcon clearLayoutImg;
    private final ImageIcon hostessPodiumImg;
    private final ImageIcon roundTableImg;
    private final ImageIcon squareTableImg;
    private final ImageIcon rectangularTableImg;
    private final ImageIcon tableForTwoImg;
    private final ImageIcon boothImg;
    private final ImageIcon buffettBarImg;
    private final ImageIcon toiletPaperDispenserImg;
    private final ImageIcon toiletImg;
    private final ImageIcon towelDispenserImg;
    private final ImageIcon wasteBasketImg;
    private final ImageIcon bathroomSinkImg;
    private final ImageIcon ovenImg;
    private final ImageIcon rangeImg;
    private final ImageIcon counterTopImg;
    private final ImageIcon fridgeImg;
    private final ImageIcon freezerImg;
    private final ImageIcon kitchenSinkImg;
    private final ImageIcon dishwasherImg;
    private final ImageIcon kitchenImg;
    private final ImageIcon diningroomImg;
    private final ImageIcon bathroomImg;  

    private final JButton leftRotateBtn;
    private final JButton rightRotateBtn;
    private final JButton upBtn;
    private final JButton downBtn;
    private final JButton leftBtn;
    private final JButton rightBtn;
    private final JButton costBtn;
    private final JButton deleteBtn;
    private final JButton selectBtn;
    private final JButton clearLayoutBtn;

    private final JPanel buttons;
    private final JPanel objectlist;
    private final JPanel whitespace;

    private final JLabel kitchenLbl;
    private final JLabel diningroomLbl;
    private final JLabel bathroomLbl;

    private final RLabel hostessPodiumLbl;
    private final RLabel roundTableLbl;
    private final RLabel squareTableLbl;
    private final RLabel rectangularTableLbl;
    private final RLabel tableForTwoLbl;
    private final RLabel boothLbl;
    private final RLabel buffettBarLbl;
    private final RLabel toiletPaperDispenserLbl;
    private final RLabel toiletLbl;
    private final RLabel towelDispenserLbl;
    private final RLabel wasteBasketLbl;
    private final RLabel bathroomSinkLbl;
    private final RLabel ovenLbl;
    private final RLabel rangeLbl;
    private final RLabel counterTopLbl;
    private final RLabel fridgeLbl;
    private final RLabel freezerLbl;
    private final RLabel kitchenSinkLbl;
    private final RLabel dishwasherLbl;    

    private final JLabel recttabLbl;  // Can remove later

    private final JScrollPane scroll;
    private final RestaurantObject recttab;
    private ArrayList<RestaurantObject> list;

    // Default Constructor
    public Interface() throws IOException
    {
    //---------------------------------------------------------------MAIN WINDOW

        // Create and set up the main window
        setTitle("Restaurant Builder");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(null);

    //------------------------------------------------------------MOUSE LISTENER

        MouseListener ml = new MouseListener() {
            @Override
            public void mouseReleased(MouseEvent e) {System.out.println("Released!");}

            @Override
            public void mousePressed(MouseEvent e) {System.out.println("Pressed!");}

            @Override
            public void mouseExited(MouseEvent e) {System.out.println("Exited!");}

            @Override
            public void mouseEntered(MouseEvent e) {System.out.println("Entered!");}

            @Override
            public void mouseClicked(MouseEvent e) 
            {

               if (e.getSource() == hostessPodiumLbl)
               {
                   removeFocus();
                   hostessPodiumLbl.setBorder(BorderFactory.createLineBorder(Color.black, 3));
                   hostessPodiumLbl.setFocus(true);
               }
               else if (e.getSource() == roundTableLbl)
                {
                   removeFocus();
                   roundTableLbl.setBorder(BorderFactory.createLineBorder(Color.black, 3));
                   roundTableLbl.setFocus(true);                    
                }
               else if (e.getSource() == squareTableLbl)
                {
                   removeFocus();
                   squareTableLbl.setBorder(BorderFactory.createLineBorder(Color.black, 3));
                   squareTableLbl.setFocus(true);                    
                }
               else if (e.getSource() == rectangularTableLbl)
                {
                   removeFocus();
                   rectangularTableLbl.setBorder(BorderFactory.createLineBorder(Color.black, 3));
                   rectangularTableLbl.setFocus(true);                    
                }
               else if (e.getSource() == tableForTwoLbl)
                {
                   removeFocus();
                   tableForTwoLbl.setBorder(BorderFactory.createLineBorder(Color.black, 3));
                   tableForTwoLbl.setFocus(true);                    
                }
               else if (e.getSource() == boothLbl)
                {
                   removeFocus();
                   boothLbl.setBorder(BorderFactory.createLineBorder(Color.black, 3));
                   boothLbl.setFocus(true);                    
                }
               else if (e.getSource() == buffettBarLbl)
                {
                   removeFocus();
                   buffettBarLbl.setBorder(BorderFactory.createLineBorder(Color.black, 3));
                   buffettBarLbl.setFocus(true);                    
                }
               else if (e.getSource() == toiletPaperDispenserLbl)
                {
                   removeFocus();
                   toiletPaperDispenserLbl.setBorder(BorderFactory.createLineBorder(Color.black, 3));
                   toiletPaperDispenserLbl.setFocus(true);                    
                }
               else if (e.getSource() == toiletLbl)
                {
                   removeFocus();
                   toiletLbl.setBorder(BorderFactory.createLineBorder(Color.black, 3));
                   toiletLbl.setFocus(true);                    
                }
               else if (e.getSource() == towelDispenserLbl)
                {
                   removeFocus();
                   towelDispenserLbl.setBorder(BorderFactory.createLineBorder(Color.black, 3));
                   towelDispenserLbl.setFocus(true);                    
                }
               else if (e.getSource() == wasteBasketLbl)
                {
                   removeFocus();
                   wasteBasketLbl.setBorder(BorderFactory.createLineBorder(Color.black, 3));
                   wasteBasketLbl.setFocus(true);                    
                }
               else if (e.getSource() == bathroomSinkLbl)
                {
                   removeFocus();
                   bathroomSinkLbl.setBorder(BorderFactory.createLineBorder(Color.black, 3));
                   bathroomSinkLbl.setFocus(true);                    
                }
               else if (e.getSource() == ovenLbl)
                {
                   removeFocus();
                   ovenLbl.setBorder(BorderFactory.createLineBorder(Color.black, 3));
                   ovenLbl.setFocus(true);                    
                }
               else if (e.getSource() == rangeLbl)
                {
                   removeFocus();
                   rangeLbl.setBorder(BorderFactory.createLineBorder(Color.black, 3));
                   rangeLbl.setFocus(true);                    
                }
               else if (e.getSource() == counterTopLbl)
                {
                   removeFocus();
                   counterTopLbl.setBorder(BorderFactory.createLineBorder(Color.black, 3));
                   counterTopLbl.setFocus(true);                    
                }
               else if (e.getSource() == fridgeLbl)
                {
                   removeFocus();
                   fridgeLbl.setBorder(BorderFactory.createLineBorder(Color.black, 3));
                   fridgeLbl.setFocus(true);                    
                }
               else if (e.getSource() == freezerLbl)
                {
                   removeFocus();
                   freezerLbl.setBorder(BorderFactory.createLineBorder(Color.black, 3));
                   freezerLbl.setFocus(true);                    
                }
               else if (e.getSource() == kitchenSinkLbl)
                {
                   removeFocus();
                   kitchenSinkLbl.setBorder(BorderFactory.createLineBorder(Color.black, 3));
                   kitchenSinkLbl.setFocus(true);                    
                }
               else if (e.getSource() == dishwasherLbl)
                {
                   removeFocus();
                   dishwasherLbl.setBorder(BorderFactory.createLineBorder(Color.black, 3));
                   dishwasherLbl.setFocus(true);                    
                }
            }
        };

    //-----------------------------------------------------------FLOORPLAN PANEL

        // Create the floorplan panel
        floorplan = new Floorplan("resources/floorplanPic.png",
                FLOORPLAN_WIDTH, FLOORPLAN_HEIGHT);
        floorplan.setBounds(0, 0, FLOORPLAN_WIDTH, FLOORPLAN_HEIGHT);
        floorplan.setLayout(null);

    // ---------------------------------------------------------------------TEST

        recttab = new RestaurantObject("RectangularTable");

        recttabLbl = new JLabel();
        ImageIcon testImg = new ImageIcon(recttab.getImgPath());
        recttabLbl.setIcon(testImg);
        recttabLbl.setBounds(500, 200, recttab.getImgWidth(), recttab.getImgHeight());
        recttabLbl.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        //recttabLbl.setFocusable(true);
        //recttabLbl.setRequestFocusEnabled(true);
        //recttabLbl.grabFocus();
        floorplan.add(recttabLbl);

        ComponentMover cm = new ComponentMover();
        cm.registerComponent(recttabLbl);
        recttabLbl.addMouseListener(ml);

    // -----------------------------------------------------------------END TEST

    //-------------------------------------------------------------BUTTONS PANEL

        // Create the buttons panel
        buttons = new JPanel();
        buttons.setLayout(null);
        buttons.setBackground(Color.LIGHT_GRAY);
        buttons.setBounds(FLOORPLAN_WIDTH, 0, BUTTONS_WIDTH, BUTTONS_HEIGHT);

        // Create images for the buttons
        leftRotateImg = new ImageIcon("RotateLeft.png");
        rightRotateImg = new ImageIcon("RotateRight.png");
        upImg = new ImageIcon("Up.png");
        downImg = new ImageIcon("Down.png");
        leftImg = new ImageIcon("Left.png");
        rightImg = new ImageIcon("Right.png");
        costImg = new ImageIcon("Cost.png");
        deleteImg = new ImageIcon("Delete.png");
        selectionImg = new ImageIcon("Selection.png");
        clearLayoutImg = new ImageIcon("ClearLayout.png");

        // Create the buttons
        leftRotateBtn = new JButton(leftRotateImg);
        leftRotateBtn.setToolTipText("Rotate to the left.");
        leftRotateBtn.addActionListener(this);
        rightRotateBtn = new JButton(rightRotateImg);
        rightRotateBtn.setToolTipText("Rotate to the Right.");
        rightRotateBtn.addActionListener(this);
        upBtn = new JButton(upImg);
        upBtn.setToolTipText("Move the object up.");
        upBtn.addActionListener(this);
        downBtn = new JButton(downImg);
        downBtn.setToolTipText("Move the object down.");
        downBtn.addActionListener(this);
        leftBtn = new JButton(leftImg);
        leftBtn.setToolTipText("Move the object to the left.");
        leftBtn.addActionListener(this);
        rightBtn = new JButton(rightImg);
        rightBtn.setToolTipText("Move the object to the right.");
        rightBtn.addActionListener(this);
        costBtn = new JButton(costImg);
        costBtn.setToolTipText("Show the current costs for the materials "
                + "and labor.");
        costBtn.addActionListener(this);
        deleteBtn = new JButton(deleteImg);
        deleteBtn.setToolTipText("Delete the object.");
        deleteBtn.addActionListener(this);
        selectBtn = new JButton(selectionImg);
        selectBtn.setToolTipText("Deselects and changes to the select cursor.");
        selectBtn.addActionListener(this);
        clearLayoutBtn = new JButton(clearLayoutImg);
        clearLayoutBtn.setToolTipText("Removes all objects from the layout.");
        clearLayoutBtn.addActionListener(this);

        // Set the location and size of the buttons
        leftRotateBtn.setBounds(25, 25, BTN_WIDTH, BTN_HEIGHT);
        upBtn.setBounds(85, 25, BTN_WIDTH, BTN_HEIGHT);
        rightRotateBtn.setBounds(145, 25, BTN_WIDTH, BTN_HEIGHT);
        leftBtn.setBounds(25, 85, BTN_WIDTH, BTN_HEIGHT);
        costBtn.setBounds(85, 85, BTN_WIDTH, BTN_HEIGHT);
        rightBtn.setBounds(145, 85, BTN_WIDTH, BTN_HEIGHT);
        selectBtn.setBounds(25, 145, BTN_WIDTH, BTN_HEIGHT);
        downBtn.setBounds(85, 145, BTN_WIDTH, BTN_HEIGHT);
        deleteBtn.setBounds(145, 145, BTN_WIDTH, BTN_HEIGHT);
        clearLayoutBtn.setBounds(25, 205, BTN_WIDTH * 3 + 20, BTN_HEIGHT);

        // Add the buttons to the panel
        buttons.add(leftRotateBtn);
        buttons.add(upBtn);
        buttons.add(rightRotateBtn);
        buttons.add(leftBtn);
        buttons.add(costBtn);
        buttons.add(rightBtn);
        buttons.add(selectBtn);
        buttons.add(downBtn);
        buttons.add(deleteBtn);
        buttons.add(clearLayoutBtn);

    //----------------------------------------------------------OBJECTLIST PANEL

        // Create objectlist panel
        objectlist = new JPanel();
        objectlist.setBackground(Color.WHITE);
        objectlist.setLayout(new BoxLayout(objectlist, BoxLayout.Y_AXIS));

        // Create labels to store images of the objects
        hostessPodiumLbl = new RLabel();
        hostessPodiumLbl.addMouseListener(ml);
        roundTableLbl = new RLabel();
        roundTableLbl.addMouseListener(ml);
        squareTableLbl = new RLabel();
        squareTableLbl.addMouseListener(ml);
        rectangularTableLbl = new RLabel();
        rectangularTableLbl.addMouseListener(ml);
        tableForTwoLbl = new RLabel();
        tableForTwoLbl.addMouseListener(ml);
        boothLbl = new RLabel();
        boothLbl.addMouseListener(ml);
        buffettBarLbl = new RLabel();
        buffettBarLbl.addMouseListener(ml);
        toiletPaperDispenserLbl = new RLabel();
        toiletPaperDispenserLbl.addMouseListener(ml);
        toiletLbl = new RLabel();
        toiletLbl.addMouseListener(ml);
        towelDispenserLbl = new RLabel();
        towelDispenserLbl.addMouseListener(ml);
        wasteBasketLbl = new RLabel();
        wasteBasketLbl.addMouseListener(ml);
        bathroomSinkLbl = new RLabel();
        bathroomSinkLbl.addMouseListener(ml);
        ovenLbl = new RLabel();
        ovenLbl.addMouseListener(ml);
        rangeLbl = new RLabel();
        rangeLbl.addMouseListener(ml);
        counterTopLbl = new RLabel();
        counterTopLbl.addMouseListener(ml);
        fridgeLbl = new RLabel();
        fridgeLbl.addMouseListener(ml);
        freezerLbl = new RLabel();
        freezerLbl.addMouseListener(ml);
        kitchenSinkLbl = new RLabel();
        kitchenSinkLbl.addMouseListener(ml);
        dishwasherLbl = new RLabel();
        dishwasherLbl.addMouseListener(ml);

        kitchenLbl = new JLabel();
        diningroomLbl = new JLabel();
        bathroomLbl = new JLabel();

        // Load the images for the objects
        hostessPodiumImg = new ImageIcon("scrlHostessPodium.png");
        roundTableImg = new ImageIcon("scrlRoundTable.png");
        squareTableImg = new ImageIcon("scrlSquareTable.png");
        rectangularTableImg = new ImageIcon("scrlRectangularTable.png");
        tableForTwoImg = new ImageIcon("scrlTableForTwo.png");
        boothImg = new ImageIcon("scrlBooth.png");
        buffettBarImg = new ImageIcon("scrlBuffettBar.png");
        toiletPaperDispenserImg = new ImageIcon("scrlToiletPaper.png");
        toiletImg = new ImageIcon("scrlToilet.png");
        towelDispenserImg = new ImageIcon("scrlTowelDispenser.png");
        wasteBasketImg = new ImageIcon("scrlWasteBasket.png");
        bathroomSinkImg = new ImageIcon("scrlBathroomSink.png");
        ovenImg = new ImageIcon("scrlOven.png");
        rangeImg = new ImageIcon("scrlRange.png");
        counterTopImg = new ImageIcon("scrlCountertop.png");
        fridgeImg = new ImageIcon("scrlFridge.png");
        freezerImg = new ImageIcon("scrlFreezer.png");
        kitchenSinkImg = new ImageIcon("scrlKitchenSink.png");
        dishwasherImg = new ImageIcon("scrlDishwasher.png");
        kitchenImg = new ImageIcon("Kitchen.png");
        diningroomImg = new ImageIcon("DiningRoom.png");
        bathroomImg = new ImageIcon("Bathroom.png");

        // Attach the images to the labels
        hostessPodiumLbl.setIcon(hostessPodiumImg);
        roundTableLbl.setIcon(roundTableImg);
        squareTableLbl.setIcon(squareTableImg);
        rectangularTableLbl.setIcon(rectangularTableImg);
        tableForTwoLbl.setIcon(tableForTwoImg);
        boothLbl.setIcon(boothImg);
        buffettBarLbl.setIcon(buffettBarImg);
        toiletPaperDispenserLbl.setIcon(toiletPaperDispenserImg);
        toiletLbl.setIcon(toiletImg);
        towelDispenserLbl.setIcon(towelDispenserImg);
        wasteBasketLbl.setIcon(wasteBasketImg);
        bathroomSinkLbl.setIcon(bathroomSinkImg);
        ovenLbl.setIcon(ovenImg);
        rangeLbl.setIcon(rangeImg);
        counterTopLbl.setIcon(counterTopImg);
        fridgeLbl.setIcon(fridgeImg);
        freezerLbl.setIcon(freezerImg);
        kitchenSinkLbl.setIcon(kitchenSinkImg);
        dishwasherLbl.setIcon(dishwasherImg);

        kitchenLbl.setIcon(kitchenImg);
        diningroomLbl.setIcon(diningroomImg);
        bathroomLbl.setIcon(bathroomImg);

        // Add all the images to the panel
        objectlist.add(diningroomLbl);
        objectlist.add(hostessPodiumLbl);
        objectlist.add(roundTableLbl);
        objectlist.add(squareTableLbl);
        objectlist.add(rectangularTableLbl);
        objectlist.add(boothLbl);
        objectlist.add(bathroomLbl);
        objectlist.add(toiletPaperDispenserLbl);
        objectlist.add(toiletLbl);
        objectlist.add(towelDispenserLbl);
        objectlist.add(wasteBasketLbl);
        objectlist.add(bathroomSinkLbl);
        objectlist.add(kitchenLbl);
        objectlist.add(ovenLbl);
        objectlist.add(rangeLbl);
        objectlist.add(counterTopLbl);
        objectlist.add(fridgeLbl);
        objectlist.add(freezerLbl);
        objectlist.add(kitchenSinkLbl);
        objectlist.add(dishwasherLbl);

        // Create a scroll bar for the objectlist panel
        scroll = new JScrollPane(objectlist);
        scroll.setBackground(Color.WHITE);
        scroll.setBorder(null);
        scroll.getVerticalScrollBar().setUnitIncrement(16);
        scroll.setBounds(FLOORPLAN_WIDTH + 25, BUTTONS_HEIGHT, 
                OBJECTLIST_WIDTH - 25, OBJECTLIST_HEIGHT);

    //----------------------------------------------------------WHITESPACE PANEL 

        // Create a panel to add some white space
        whitespace = new JPanel();
        whitespace.setBounds(FLOORPLAN_WIDTH, BUTTONS_HEIGHT, 25, OBJECTLIST_HEIGHT);
        whitespace.setBackground(Color.WHITE);

    //---------------------------------------------------------------MAIN WINDOW

        // Display the main window
        add(floorplan);
        add(buttons);
        add(scroll);
        add(whitespace);
        setExtendedState(Frame.MAXIMIZED_BOTH);  
        setVisible(true); 
        setResizable(false);
    }

    //-----------------------------------------------------------ACTION LISTENER
    @Override
    public void actionPerformed(ActionEvent e) 
    {
        int x, y, imgW, imgH;
        x = recttab.getX();
        y = recttab.getY();
        imgW = recttab.getImgWidth();
        imgH = recttab.getImgHeight();

        if (e.getSource() == leftRotateBtn)
        {
            System.out.println("leftRotateBtn");
        }
        else if (e.getSource() == upBtn)
        {
            if (y > 5)
            {
                y -= 5;
                recttab.setY(y);
                recttabLbl.setBounds(x, y, imgW, imgH);
            }

        }
        else if (e.getSource() == rightRotateBtn)
        {
            System.out.println("rightRotateBtn");
        }
        else if (e.getSource() == leftBtn)
        {
            if (x > 5)
            {
                x -= 5;
                recttab.setX(x);
                recttabLbl.setBounds(x, y, imgW, imgH);
            }
        }
        else if (e.getSource() == costBtn)
        {
            estimate = new Estimate();
        }
        else if (e.getSource() == rightBtn)
        {
            if (x < (FLOORPLAN_WIDTH - recttab.getImgWidth() - 5))
            {
                x += 5;
                recttab.setX(x);
                recttabLbl.setBounds(x, y, imgW, imgH);
            }
        }
        else if (e.getSource() == selectBtn)
        {
            removeFocus();
        }
        else if (e.getSource() == downBtn)
        {
            if (y < (SCREEN_HEIGHT - recttab.getImgHeight() - 5))
            {
                y += 5;
                recttab.setY(y);
                recttabLbl.setBounds(x, y, imgW, imgH);
            }
        }
        else if (e.getSource() == deleteBtn)
        {
            System.out.println("deleteBtn");
        }
        else if (e.getSource() == clearLayoutBtn)
        {
            System.out.println("clearLayoutBtn");
        }    
    }

    // New label class to allow lables to have focus
    private class RLabel extends JLabel{
        private boolean focus;

        // Does the object have focus
        @Override
        public boolean hasFocus()
        {
            return this.focus;
        }

        // Set the focus
        public void setFocus(boolean f)
        {
            this.focus = f;
        }
    }

    // Removes focus from all labels
    private void removeFocus()
    {
        hostessPodiumLbl.setBorder(null);
        hostessPodiumLbl.setFocus(true);
        roundTableLbl.setBorder(null);
        roundTableLbl.setFocus(true);
        squareTableLbl.setBorder(null);
        squareTableLbl.setFocus(true);
        rectangularTableLbl.setBorder(null);
        rectangularTableLbl.setFocus(true);
        tableForTwoLbl.setBorder(null);
        tableForTwoLbl.setFocus(true);
        boothLbl.setBorder(null);
        boothLbl.setFocus(true);
        buffettBarLbl.setBorder(null);
        buffettBarLbl.setFocus(true);
        toiletPaperDispenserLbl.setBorder(null);
        toiletPaperDispenserLbl.setFocus(true);
        toiletLbl.setBorder(null);
        toiletLbl.setFocus(true);
        towelDispenserLbl.setBorder(null);
        towelDispenserLbl.setFocus(true);
        wasteBasketLbl.setBorder(null);
        wasteBasketLbl.setFocus(true);
        bathroomSinkLbl.setBorder(null);
        bathroomSinkLbl.setFocus(true);
        ovenLbl.setBorder(null);
        ovenLbl.setFocus(true);
        rangeLbl.setBorder(null);
        rangeLbl.setFocus(true);
        counterTopLbl.setBorder(null);
        counterTopLbl.setFocus(true);
        fridgeLbl.setBorder(null);
        fridgeLbl.setFocus(true);
        freezerLbl.setBorder(null);
        freezerLbl.setFocus(true);
        kitchenSinkLbl.setBorder(null);
        kitchenSinkLbl.setFocus(true);
        dishwasherLbl.setBorder(null);  
        dishwasherLbl.setFocus(true);
    }
}

I don't have much experience at all with GUIs in general so it's a wonder I got this far. Has anyone ever worked on something like this? Any suggestions how to make it work?

I think it's answer for you.

1) Get all your Images and cut from they cost and name data.

2) Add your components with help of LayoutManager, I think GridBagLayout is suitable for that purpose.

3) Add your Image with help of(JLabel) then add JLabel with name property under the first and next add JLabel with cost.

If you do all in that way you can drag only Images without additional information, it solve problem with different images for one Entity.

As recommendation:

1) Change your mouseClicked method in next way:

public void mouseClicked(MouseEvent e) {
    removeFocus();
    ((RLabel)e.getSource()).setBorder(BorderFactory.createLineBorder(Color.black, 3));
    ((RLabel)e.getSource()).setFocus(true);
}

It clears method from duplication code.

2) instead of using setBounds() method use LayoutManager it do all work with component size.

3) Never do that setLayout(null);

4) Split your constructor method for some simple and clear methods. It's really looks awful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM