2017-02-25 38 views
0

我希望能夠爲中心的JLabel設置定位點,所以我可以根據它移動它。有沒有辦法在JLabel上設置錨點?

+1

請告訴我們更多關於您當前的代碼,你的問題,你的問題的細節。 「錨點」究竟是什麼意思?你的意思是說,如果我點擊那個JLabel,它會移動,使它位於我點擊的中心位置,然後如果我拖動鼠標的中心位置? [mcve]也會有很大的幫助。這不僅僅是不可編譯的代碼片段,還不及整個程序。如果可能的話,還總是向我們展示你試圖解決這個問題的方法,因爲它極大地增加了我們對可能被卡住的地方的理解。 –

+0

此外,還包括我上面提到的更多內容,而不是這個:「我試過用Google搜索它,通常我找到了我需要的,但在這種情況下不是。」因爲它告訴我們沒有什麼能夠幫助我們理解你的問題。它只表明你在Google上搜索錯誤的東西,你沒有將你的問題減少到谷歌可搜索的足夠小的步驟。 –

+0

@HovercraftFullOfEels對不起,這是我第一次提出問題。我希望我能夠擺脫不包括代碼,因爲我目前的情況只允許我使用我的iPad,所以它只是有點困難。通過錨點,我的意思是,如果我使用label.setLocation(100,100);它會成爲那裏的定位點。因此,目前錨是0,0,所以鍵入上面的行將把JLabel的0,0設置爲100,100,但是我希望它將坎特設置爲100,100,因此,詢問如何更改錨點。 – FireStrike289

回答

3

如果您試圖將JLabel居中在某個點上,那麼您只需要簡單的數學就可以完成此操作,僅此而已。

假設您有一個JLabel或任何我們將其命名爲「component」的組件,它保存在JPanel中,或者我們將稱之爲「container」的任何容器,並且假設有一個鼠標Point 在屏幕上,說叫「mousePoint」,那麼數學也很簡單:

Point mousePoint = e.getLocationOnScreen(); 
Point containerLocation = container.getLocationOnScreen(); 
Dimension componentSize = component.getSize(); 

int x = mousePoint.x - componentSize.width/2 - containerLocation.x; 
int y = mousePoint.y - componentSize.height/2 - containerLocation.y; 
component.setLocation(x, y); 

就是這樣。例如,假設您有兩個JLabel,一個帶有圖像,另一個帶有一些文本,那麼您可以將兩個相同的MouseListener和MouseMotionListener添加到這兩個JLabel,並且這可以讓您將它們拖放到它們的中心點。下面是我在我的上述評論提到MCVE的例子:具有登載

import java.awt.Color; 
import java.awt.Container; 
import java.awt.Dimension; 
import java.awt.Image; 
import java.awt.Point; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import java.io.IOException; 
import java.net.URL; 

import javax.imageio.ImageIO; 
import javax.swing.*; 

public class ClickDragLabel extends JPanel { 
    public static final String IMG_PATH = "https://upload.wikimedia.org/wikipedia/commons/thumb/4/43/" 
      + "Theodore_Comnenus-Ducas_cropped.jpg/133px-Theodore_Comnenus-Ducas_cropped.jpg"; 
    private static final int PREF_W = 1000; 
    private static final int PREF_H = 850; 
    private JLabel imageLabel; 
    private JLabel textLabel = new JLabel("Some Random Text"); 

    public ClickDragLabel(Icon icon) { 
     setBackground(Color.WHITE); 
     imageLabel = new JLabel(icon); 

     setLayout(null); 
     imageLabel.setSize(imageLabel.getPreferredSize()); 
     textLabel.setSize(textLabel.getPreferredSize()); 

     imageLabel.setLocation(250, 250); 
     textLabel.setLocation(10, 10); 

     MyMouse myMouse = new MyMouse(); 
     imageLabel.addMouseListener(myMouse); 
     imageLabel.addMouseMotionListener(myMouse); 
     textLabel.addMouseListener(myMouse); 
     textLabel.addMouseMotionListener(myMouse); 

     add(imageLabel); 
     add(textLabel); 

    } 

    @Override 
    public Dimension getPreferredSize() { 
     if (isPreferredSizeSet()) { 
      return super.getPreferredSize(); 
     } 
     return new Dimension(PREF_W, PREF_H); 
    } 

    private class MyMouse extends MouseAdapter { 
     @Override 
     public void mousePressed(MouseEvent e) { 
      center(e); 
     } 

     @Override 
     public void mouseReleased(MouseEvent e) { 
      center(e); 
     } 

     @Override 
     public void mouseDragged(MouseEvent e) { 
      center(e); 
     } 

     private void center(MouseEvent e) { 
      JComponent component = (JComponent) e.getSource(); 
      Container container = component.getParent(); 

      Point mousePoint = e.getLocationOnScreen(); 
      Point containerLocation = container.getLocationOnScreen(); 
      Dimension componentSize = component.getSize(); 

      int x = mousePoint.x - componentSize.width/2 - containerLocation.x; 
      int y = mousePoint.y - componentSize.height/2 - containerLocation.y; 
      component.setLocation(x, y); 
      container.repaint(); 
     } 

    } 

    private static void createAndShowGui() { 
     Image img = null; 
     try { 
      URL imgUrl = new URL(IMG_PATH); 
      img = ImageIO.read(imgUrl); 
     } catch (IOException e) { 
      e.printStackTrace(); 
      System.exit(-1); 
     } 

     Icon icon = new ImageIcon(img); 

     ClickDragLabel mainPanel = new ClickDragLabel(icon); 

     JFrame frame = new JFrame("Click-Drag Label"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(mainPanel); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(() -> createAndShowGui()); 
    } 
} 

,如果我沒有提到使用空佈局的罪惡我會失職。雖然Swing的新手可能看起來像是創建複雜GUI的最簡單也是最好的方式,但更多Swing GUI的創建使用它們時會遇到更嚴重的困難。它們不會在GUI大小調整時調整組件的大小,它們是增強或維護的皇室女巫,當它們放在滾動窗格中時它們會完全失敗,在所有平臺或屏幕分辨率與原始視圖不同時,它們看起來會非常糟糕。我只將它們用於動畫,比如上面的,沒有別的。

3

有沒有辦法在JLabel上設置錨?

您似乎想要的設置可以通過將EmptyBorder設置並更改爲標籤來實現。

enter image description hereenter image description here

import java.awt.*; 
import javax.swing.*; 
import javax.swing.border.EmptyBorder; 
import javax.swing.event.*; 

public class MovableLabel { 

    private JComponent ui = null; 
    String anchorString = new String(Character.toChars(9875)); 
    private JLabel label = new JLabel(anchorString); 
    int pad = 200; 
    SpinnerNumberModel xModel = new SpinnerNumberModel(0, 0, pad, 1); 
    SpinnerNumberModel yModel = new SpinnerNumberModel(0, 0, pad, 1); 

    MovableLabel() { 
     initUI(); 
    } 

    public void initUI() { 
     if (ui != null) { 
      return; 
     } 

     ui = new JPanel(new BorderLayout(4, 4)); 
     ui.setBorder(new EmptyBorder(4, 4, 4, 4)); 

     Font[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts(); 
     Font font = null; 
     for (Font f : fonts) { 
      if (f.canDisplayUpTo(anchorString) < 0) { 
       font = f.deriveFont(40f); 
       break; 
      } 
     } 
     label.setFont(font); 
     label.setBackground(Color.BLUE); 
     setBorder(); 
     ui.add(label); 

     JToolBar tb = new JToolBar(); 
     ui.add(tb, BorderLayout.PAGE_START); 

     ChangeListener changeListener = new ChangeListener() { 

      @Override 
      public void stateChanged(ChangeEvent e) { 
       setBorder(); 
      } 
     }; 

     tb.add(new JLabel("X")); 
     JSpinner xSpinner = new JSpinner(xModel); 
     xSpinner.addChangeListener(changeListener); 
     tb.add(xSpinner); 

     tb.add(new JLabel("Y")); 
     JSpinner ySpinner = new JSpinner(yModel); 
     ySpinner.addChangeListener(changeListener); 
     tb.add(ySpinner); 
    } 

    private void setBorder() { 
     int x = xModel.getNumber().intValue(); 
     int y = yModel.getNumber().intValue(); 
     EmptyBorder border = new EmptyBorder(x, y, pad - x, pad - y); 
     label.setBorder(border); 
    } 

    public JComponent getUI() { 
     return ui; 
    } 

    public static void main(String[] args) { 
     Runnable r = new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (Exception useDefault) { 
       } 
       MovableLabel o = new MovableLabel(); 

       JFrame f = new JFrame(o.getClass().getSimpleName()); 
       f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
       f.setLocationByPlatform(true); 

       f.setContentPane(o.getUI()); 
       f.pack(); 
       f.setMinimumSize(f.getSize()); 

       f.setVisible(true); 
      } 
     }; 
     SwingUtilities.invokeLater(r); 
    } 
} 
相關問題