0

我正在尝试开发一个如下所示的摇摆应用程序:-

在此处输入图像描述

(可以在此处找到此应用程序的演示)

黑色方块随着光标的移动而移动。ComboBox 条目选择堆栈中的最顶层,在移动过程中方块将出现在该层之上,但在剩余的下一个顶层之下。

我几乎完成了它,但是当鼠标移动时黑色方块(图像)正在振动(运动不流畅),并且它的运动仅限于小区域并且它没有在完整的分层窗格上移动。也没有改变的效果ComboBox 或 CheckBox 中的设置。

这是代码:-

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


public class JLayeredPaneDemo implements ActionListener, MouseMotionListener{
private String layerString[]={"Red(0)","Yellow(1)","Blue(2)","Green(3)","Orange(4)"};
private Color layerColors[]={Color.RED,Color.YELLOW,Color.BLUE,Color.GREEN,Color.ORANGE};
JComboBox jcb;
JCheckBox jcheck;
JLayeredPane layeredPane;
ImageIcon img;
JLabel cursor;

public static void main(String args[])
{
SwingUtilities.invokeLater(new Runnable(){public void run(){new JLayeredPaneDemo();}});
}

public JLayeredPaneDemo()
{
JFrame frame=new JFrame("JLayeredPane Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(2,1));

frame.add(createControlPanel());
//frame.add(Box.createRigidArea(new Dimension(0, 10)));
frame.add(createLayeredPane());

try
{
    img=new ImageIcon(ImageIO.read(new File("src/watermark.png")));
}catch(IOException e){System.out.println("ERROR LOADING IMAGE :"+ e);}
cursor=new JLabel(img);
cursor.setBounds(45,45,img.getIconWidth(),img.getIconHeight());
cursor.addMouseMotionListener(this);
layeredPane.add(cursor,new Integer(2),0);

frame.pack();
frame.setVisible(true);
}

public JPanel createControlPanel()
{
JPanel panel=new JPanel();
panel.setBorder(BorderFactory.createTitledBorder("Select the Settings"));

jcb=new JComboBox(layerString);
jcb.setSelectedIndex(0);
jcb.setActionCommand("LAYER_SETTING");
jcb.addActionListener(this);

jcheck=new JCheckBox();
jcheck.setSelected(true);
jcheck.setActionCommand("FRONT_BACK");
jcheck.addActionListener(this);

panel.add(jcb);
panel.add(jcheck);

return panel;
}

public JLayeredPane createLayeredPane()
{
layeredPane=new JLayeredPane();
layeredPane.setBorder(BorderFactory.createTitledBorder("Layered Pane : Move Your Mouse"));
layeredPane.setPreferredSize(new Dimension(300,310));

Point origin=new Point(20,20);
int offset=35;

for(int i=0;i<layerString.length;i++)
{
    JLabel label=createColoredLabel(layerString[i],layerColors[i],origin);
    layeredPane.add(label,new Integer[i]);
    origin.x+=offset;
    origin.y+=offset;
}

return layeredPane;
}

private JLabel createColoredLabel(String str,Color color,Point origin)
{
JLabel label =new JLabel(str);
label.setVerticalAlignment(JLabel.TOP);
label.setHorizontalAlignment(JLabel.CENTER);
label.setOpaque(true);
label.setBackground(color);
label.setForeground(Color.BLACK);
label.setBorder(BorderFactory.createLineBorder(Color.BLACK));
label.setBounds(origin.x,origin.y,100,100);

return label;
}
public void actionPerformed(ActionEvent e)
{
String cmd=e.getActionCommand();
if(cmd.equals("LAYER_SETTING"))
{
    int position=jcheck.isSelected() ? 0 :1;
        layeredPane.setLayer(cursor,jcb.getSelectedIndex(),position);
}
else if(cmd.equals("FRONT_BACK"))
{
    if(jcheck.isSelected())
        layeredPane.moveToFront(cursor);
    else
        layeredPane.moveToBack(cursor);
}
}

@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
//Do nothing
}

@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
cursor.setLocation(e.getX(),e.getY());
}
}

请帮我找出问题所在。

4

2 回答 2

2

Your mouseMoved() implementation no longer applies the offsets used in the original; you'll need to supply them based on the geometry of the Icon used in your cursor.

//Adjustments to put Duke's toe at the cursor's tip.
private static final int XFUDGE = 40;
private static final int YFUDGE = 57;
...
//Make Duke follow the cursor.
public void mouseMoved(MouseEvent e) {
    dukeLabel.setLocation(e.getX()-XFUDGE, e.getY()-YFUDGE);
}

Addendum: I don't understand why I didn't get any error when I used the wrong brackets? Can anyone point out the reason, please.

layeredPane.add(label,new Integer(i));

Because the language defines an array as an Object, the compiler chose the matching available add() method:

public void add(Component comp, Object constraints);
于 2013-01-06T10:26:32.720 回答
1

There are two silly mistakes which consumed my 11 precious hours.

1)I set actionListener for 'cursor' instead of layeredPane and that's why the movement was confined to a small area.

2)Secondly,i wrote layeredPane.add(label,new Integer[i]); instead of layeredPane.add(label,new Integer(i)); .(Notice the square brackets).

于 2013-01-06T14:52:16.683 回答