gpt4 book ai didi

java - 在图像下方的 JPanel 中定位 JLabel

转载 作者:行者123 更新时间:2023-11-29 05:29:35 25 4
gpt4 key购买 nike

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

    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 出现在面板的顶部。我想将其向下移动到图像/形状。

我尝试使用 setBounds() 但没有用。如果我遗漏了什么,请告诉我。

最佳答案

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

看看:

例如……

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);
//...

关于java - 在图像下方的 JPanel 中定位 JLabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21587309/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com