我有一个JPanel
在它JLabel
时间,是有可能有一个鼠标点击JLabel
,通过对JPanel
任何位置再次用鼠标单击下面创建的实例JLabel
。基本上,我可以单击JLabel
并在JPanel
上的任意位置创建新实例。创建新实例的JLabel的每次点击
回答
这里是你一个简单的例子正在找。你需要的是点击JLabel
的deepCopy
,然后将其恢复并将其绘制到JPanel
。
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.awt.*;
import javax.swing.border.*;
class CopyLabel extends JFrame
{
JPanel panel ;
JPanel centerPanel;
int clickCount = 0;
ByteArrayOutputStream baos;
ByteArrayInputStream bins;
public void createAndShowGUI()
{
setTitle("Copy JLabel");
JLabel label1 = new JLabel("JLabel1");
JLabel label2 = new JLabel("JLabel2");
panel = new JPanel();
label1.setForeground(Color.blue);
label2.setForeground(Color.red);
panel.add(label1);
panel.add(label2);
class MyMouseAdapter extends MouseAdapter
{
@Override
public void mouseClicked(MouseEvent evt)
{
clickCount = 1;
try
{
deepCopy((JLabel)evt.getSource());
}
catch (Exception ex){}
}
}
label1.addMouseListener(new MyMouseAdapter());
label2.addMouseListener(new MyMouseAdapter());
panel.setBorder(BorderFactory.createTitledBorder("Controllers"));
getContentPane().add(panel,BorderLayout.SOUTH);
centerPanel = new JPanel();
centerPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createRaisedBevelBorder(),"Drawing Pad",TitledBorder.CENTER,TitledBorder.TOP));
centerPanel.setLayout(null);
centerPanel.addMouseListener(new MouseAdapter()
{
@Override
public void mouseClicked(MouseEvent evt)
{
if (clickCount == 1)
{
try
{
pasteLabel(evt.getX(),evt.getY());
}
catch (Exception ex){}
}
}
});
getContentPane().add(centerPanel);
setSize(300,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void deepCopy(JLabel label)throws Exception
{
baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(label);
oos.close();
}
public void pasteLabel(int x, int y)throws Exception
{
if (clickCount == 1)
{
bins = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream oins = new ObjectInputStream(bins);
JLabel obj = (JLabel)oins.readObject();
centerPanel.add(obj);
obj.setBounds(x,y,obj.getWidth(),obj.getHeight());
}
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
CopyLabel cl = new CopyLabel();
cl.createAndShowGUI();
}
});
}
}
谢谢!这有帮助!如果我想让绘图板中的JLabel可移动,可以做些什么? – CDX 2013-03-19 22:16:12
您可以通过鼠标监听器连接到您的JLabel这样
final JLabel jlabel = new JLabel("Test");
jlabel.addMouseListener(new MouseAdapter(){
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("Hello : "+ e);
Point location = MouseInfo.getPointerInfo().getLocation();
targetPanel.add(cloneLabelAt(jlabel, location));
}
});
private JLabel cloneLabelAt(JLabel label, Point location)
{
JLabel cloned = new JLabel(label.getText());
cloned.setLocation(location);
return cloned;
}
内,您的鼠标点击的处理程序,您可以创建其他的JLabel并将其添加到您的目标板
虽然技术是正确的,但有两件事情在我身上跳动,1-不需要调用super,因为MouseAdapter从MouseListener和MouseMotionListener创建空方法(没什么大不了的),2-为了真正回答这个问题,你会需要能够克隆被点击的对象和克隆引用到目标容器上的位置(当我阅读它时,在与第二次点击相同的位置)。这个问题可能不是关于鼠标听众,但这只是我的驯服 – MadProgrammer 2013-03-19 20:50:04
是的,我想在第二次鼠标点击的位置有克隆参考,这怎么可以做? – CDX 2013-03-19 21:01:35
您可以使用MouseInfo.getPointerInfo()。getLocation() – Greg 2013-03-19 21:05:12
- 1. 每次点击一个按钮创建新的随机句子
- 2. `auto`每次都创建一个新的变量实例?
- 3. 运算符++(int x)是否每次都创建新的实例?
- 4. 在每次迭代中创建Campaign的新实例
- 5. 在每次运行时都创建了BackgroundTasks的新实例吗?
- 6. 每次android都通过Intent创建新的实例活动?
- 7. 为什么Rectangle.Size在每次调用时创建新的实例?
- 8. 通过点击jlabel创建活动
- 9. 每次新的点击新动作
- 10. 是否每次调用新的Handler(Looper.getMainLooper())创建它的新实例?
- 11. MediaBrowserServiceCompat实例每次运行时更改都会重新创建
- 12. 每次点击更新的日期
- 13. 每次生成ViewController的新实例
- 14. 创建KClass的新实例
- 15. ViewScoped bean重新创建,每次我点击我的数据表中的commandButton
- 16. ExecutorService - 在方法中创建新实例vs每个类创建一个实例
- 17. VBA - 命令按钮点击子 - 当工作表中创建新的每次
- 18. 如何防止每次调用主机时创建类的新实例?
- 19. Ruby应用程序中的无状态服务和每次创建新实例
- 20. 每次点击角
- 21. 用D3创建可点击的图例
- 22. NLog为类的每个实例创建新的日志文件
- 23. GWTP创建新的主持人每次
- 24. Javascript新随机数每次点击
- 25. 在jquery里面创建对象的实例点击功能
- 26. Morphia:为每个数据库创建新的Morphia实例?
- 27. PyQt在每次点击后创建添加对话框
- 28. 我如何在每次点击时创建一个文本框
- 29. 防止每次点击bottomnavigation选项卡时重新创建片段
- 30. 如何避免每次创建新实例时从文件中读取数据
问题是什么? – CAMOBAP 2013-03-19 20:43:40
如果问题是“是否有可能?”好的。 – Aboutblank 2013-03-19 20:45:00
任何想法如何我可以去创建? – CDX 2013-03-19 21:02:10