gpt4 book ai didi

java - 使用 JLayeredPane 调试简单的 swing 应用程序时需要帮助

转载 作者:行者123 更新时间:2023-12-02 00:04:25 35 4
gpt4 key购买 nike

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

enter image description here

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

黑色方 block 随着光标的移动而移动。组合框条目选择堆栈中的最顶层,在移动过程中该方 block 将出现在其上方,但位于其余的下一个顶层下方。

我已经完成它了,但是当鼠标移动时,黑色方 block (图像)会振动(运动不平滑),而且它的运动仅限于小区域,并且不会在完整的分层 Pane 上移动。而且没有更改 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());
}
}

请帮我找出问题所在。

最佳答案

您的mouseMoved()实现不再应用原始中使用的偏移量;您需要根据光标中使用的Icon的几何形状来提供它们。

//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);
}

附录:我不明白为什么我使用错误的括号时没有收到任何错误?有谁能指出原因吗?

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

因为语言defines将数组作为对象,编译器选择可用的匹配 add()方法:

public void add(Component comp, Object constraints);

关于java - 使用 JLayeredPane 调试简单的 swing 应用程序时需要帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14181095/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com