gpt4 book ai didi

java - 面板顶部的图形

转载 作者:行者123 更新时间:2023-11-30 07:27:47 26 4
gpt4 key购买 nike

我在上面有 JLabel 的面板上绘制图形时遇到问题。我正在使用自定义 paintComponent(Graphics g) 方法在面板上绘制一个字符串。我希望字符串显示在 JLabel 之上而不是 JLabel 之下,这是当前正在发生的事情。如果你想看到它,这里是 paintComponent 方法:

public void paintComponent(Graphics g){
super.paintComponent(g);

if(a.shouldAnnotate()){
FontMetrics size= g.getFontMetrics();
if(getWidth()>=(a.dispX()+size.stringWidth(a.annotationText()))){
g.setColor(Color.white);
g.fillRect(a.dispX()-3,a.dispY()-12,size.stringWidth(a.annotationText())+5,15);
g.setColor(Color.black);
g.drawRect(a.dispX()-3,a.dispY()-12,size.stringWidth(a.annotationText())+5,15);
g.drawString(a.annotationText(), a.dispX(), a.dispY());
}else{
String sub="";
int letters=0;
g.setColor(Color.white);
g.fillRect(a.dispX()-3,a.dispY()-12,getWidth(),15);
g.setColor(Color.black);

for(int i=0;i<a.annotationText().length();i++){
if(a.dispX()+letters+16<=getWidth()){
sub+=a.annotationText().substring(i,i+1);
letters=size.stringWidth(sub);
}else{
sub=sub+"...";
i=a.annotationText().length();
}
}
g.drawRect(a.dispX()-3,a.dispY()-12,size.stringWidth(sub)+3,15);
g.drawString(sub,a.dispX(),a.dispY());
}
}
}

我不太喜欢图形,但我需要学习如何解决这个问题。

最佳答案

要做到这一点,我认为您必须覆盖 paint 或 paintChildren,并在那里绘制文本:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;

import javax.swing.*;

@SuppressWarnings("serial")
public class DrawOverLabel extends JPanel {
private static final int PREF_W = 500;
private static final int PREF_H = 100;
private static final Font TEXT_FONT = new Font(Font.SANS_SERIF, Font.BOLD, 32);
private static final int LABEL_COUNT = 40;

public DrawOverLabel() {
for (int i = 0; i < LABEL_COUNT; i++) {
JLabel label = new JLabel("Label");
label.setForeground(Color.green);
label.setFont(label.getFont().deriveFont(Font.BOLD, 20));
add(label);
}
}

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

g.setColor(Color.blue);
g.setFont(TEXT_FONT);
g.drawString("in paint", 20, 30);

// super.paint(g);
}

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

g.setColor(Color.red);
g.setFont(TEXT_FONT);
g.drawString("in paintComponent", 20, 60);

// super.paintComponent(g);
}

@Override
protected void paintChildren(Graphics g) {
super.paintChildren(g);

g.setColor(Color.gray);
g.setFont(TEXT_FONT);
g.drawString("in paintChildren", 20, 100);

// super.paintChildren(g);
}

@Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}

private static void createAndShowGui() {
JFrame frame = new JFrame("DrawOverLabel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new DrawOverLabel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

但更好的解决方案是使用 JLayeredPane 或玻璃板来实现此效果。

关于java - 面板顶部的图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9490540/

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