2013-03-19 53 views
0

我有一個JPanel在它JLabel時間,是有可能有一個鼠標點擊JLabel,通過對JPanel任何位置再次用鼠標單擊下面創建的實例JLabel。基本上,我可以單擊JLabel並在JPanel上的任意位置創建新實例。創建新實例的JLabel的每次點擊

+0

問題是什麼? – CAMOBAP 2013-03-19 20:43:40

+0

如果問題是「是否有可能?」好的。 – Aboutblank 2013-03-19 20:45:00

+0

任何想法如何我可以去創建? – CDX 2013-03-19 21:02:10

回答

2

這裏是你一個簡單的例子正在找。你需要的是點擊JLabeldeepCopy,然後將其恢復並將其繪製到JPanel
enter image description here

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(); 
      } 
     }); 
    } 
} 
+0

謝謝!這有幫助!如果我想讓繪圖板中的JLabel可移動,可以做些什麼? – CDX 2013-03-19 22:16:12

1

您可以通過鼠標監聽器連接到您的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並將其添加到您的目標板

+2

雖然技術是正確的,但有兩件事情在我身上跳動,1-不需要調用super,因爲MouseAdapter從MouseListener和MouseMotionListener創建空方法(沒什麼大不了的),2-爲了真正回答這個問題,你會需要能夠克隆被點擊的對象和克隆引用到目標容器上的位置(當我閱讀它時,在與第二次點擊相同的位置)。這個問題可能不是關於鼠標聽衆,但這只是我的馴服 – MadProgrammer 2013-03-19 20:50:04

+0

是的,我想在第二次鼠標點擊的位置有克隆參考,這怎麼可以做? – CDX 2013-03-19 21:01:35

+0

您可以使用MouseInfo.getPointerInfo()。getLocation() – Greg 2013-03-19 21:05:12

相關問題