我有以下布局...
理想情况下,当用户单击右下角滚动面板中的 JLabel 时,将图像的不同版本(没有成本和名称的图像)附加到鼠标光标上。然后,当用户在巨大的平面图面板内单击时,它会在每次单击时使用交换的图像放置一个 JLabel 的副本。When another label is selected the same thing happens. 当他们按下选择按钮(手形图标)或按下 ESC 时,标签未被选中。
这是我目前未修改的代码...
// =================================================================
// 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);
}
}
一般来说,我对 GUI 没有太多经验,所以能走到这一步真是个奇迹。有没有人做过这样的事情?任何建议如何使它工作?