2017-02-25 74 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); 
    } 
} 
相关问题