将JLabel放置在图像下方的JPanel中

将JLabel放置在图像下方的JPanel中

本文介绍了将JLabel放置在图像下方的JPanel中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在给定的示例中,我想将文本移动到图像和形状下方.请帮我做.

I want to move the Text below the image and shape in the given example.Please help me to do it.

    package test;

    import java.awt.BasicStroke;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;


    public class TestLabel extends JPanel {

        private Integer size = 70;
        private String name;
        private Image image;

        public TestLabel(Integer size, String name) {
            this.name = name;
            this.size = size;
            setSize(size, size + size / 4);
            this.image = new ImageIcon(new Node().getClass().getResource("/com/businesslense/topology/images/node1.jpg")).getImage();
            JLabel textLabel = new JLabel(name);
            textLabel.setBounds(100,100,70,30);
            add(textLabel);
            setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 0, 51), 2));
        }



        @Override
        public void paintComponent(Graphics g) {

            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setStroke(new BasicStroke(4, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));

            int imagePaddingVal = (int) ((Math.sqrt(2) * (((size * 115) / 100) / 2))) / 4;
            int imageSize = (size * 85) / 100;
            int imageRadius = (int) (Math.sqrt(2) * (imageSize / 2));

            g2d.drawImage(image, imagePaddingVal, imagePaddingVal, imageRadius, imageRadius, this);

            int shapePaddingVal = (size * 5) / 100;
            int shapeRadius = (size * 90) / 100;

            g2d.drawOval(shapePaddingVal, shapePaddingVal, shapeRadius, shapeRadius);

            g2d.setStroke(new BasicStroke(0, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));


        }

        public static void main(String argv[]){
            TestLabel tl = new TestLabel(70,"Test");
            JFrame jf = new JFrame();

            jf.add(tl);

            jf.setVisible(true);
            jf.setSize(300, 400);
        }
    }

在此示例中,JLable位于面板顶部.我想将其向下移动到图像/形状.

In this example, JLable comes in the top of the panel. I want to move it down to the image/shape.

我尝试了setBounds(),但是没有用.请告诉我我是否缺少任何东西.

I tried with setBounds() but that didnt work. Please tell me if i am missing something.

推荐答案

考虑使用API​​的可用功能...

Consider making use of the API's available functionality...

看看:

  • JLabel#setHorizontalTextPosition
  • JLabel#setVerticalTextPosition
  • How to use labels

例如...

private ImageIcon image;

public TestLabel100(Integer size, String name) {
    //...
    JLabel textLabel = new JLabel(name);
    textLabel.setIcon(image);
    textLabel.setHorizontalTextPosition(JLabel.CENTER);
    textLabel.setVerticalTextPosition(JLabel.SOUTH);
    //textLabel.setBounds(100, 100, 70, 30);
    //...
}

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    //...

这篇关于将JLabel放置在图像下方的JPanel中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 00:58