答案 0 :(得分:3)
不确定。将文本直接绘制到图片上。在标签中显示图片。
E.G。
当然,如果你想要“更简单”,还有其他选择。一个是OverlayLayout
,如@Reimeus所述。这是另一个。它利用了我们可以为标签设置布局并在其中显示组件的事实。这项技术由@mKorbel推广。
import java.awt.*;
import javax.swing.*;
import java.awt.image.BufferedImage;
import java.net.URL;
import javax.imageio.ImageIO;
class TextOnImage {
public static void main(String[] args) throws Exception {
URL url = new URL("http://i.stack.imgur.com/zJ8am.png");
final BufferedImage image = ImageIO.read(url);
Runnable r = new Runnable() {
@Override
public void run() {
JPanel gui = new JPanel(new BorderLayout());
JLabel l = new JLabel(new ImageIcon(image));
l.setLayout(new FlowLayout(FlowLayout.CENTER));
JLabel text = new JLabel("Hi!");
l.add(text);
JOptionPane.showMessageDialog(null, l);
}
};
// Swing GUIs should be created and updated on the EDT
// http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
SwingUtilities.invokeLater(r);
}
}
答案 1 :(得分:3)
一种方法是使用OverlayLayout
。对于文本在两个轴中位于图像的中心,对齐值0.5
应该用于X& Y表示下面显示的JLabel
个组件。
public class OverlayLabelApp {
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame("Overlay App");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
LayoutManager overlay = new OverlayLayout(panel);
panel.setLayout(overlay);
JLabel label1 = new JLabel("Centered Text");
label1.setForeground(Color.GREEN);
label1.setFont(new Font("SansSerif", Font.BOLD, 16));
label1.setAlignmentX(0.5f);
label1.setAlignmentY(0.5f);
panel.add(label1);
JLabel label2 =
new JLabel(new ImageIcon(OverlayLabelApp.class.getResource("/images/sunset.png"))); label2.setAlignmentX(0.5f);
label2.setAlignmentY(0.5f);
panel.add(label2);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
});
}
}
答案 2 :(得分:1)
文本位于图片的中心。
另外4种方法。第一个是最简单的,听起来像你想要的那样:
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
public class LabelImageText extends JPanel
{
public LabelImageText()
{
JLabel label1 = new JLabel( new ColorIcon(Color.ORANGE, 100, 100) );
label1.setText( "Easy Way" );
label1.setHorizontalTextPosition(JLabel.CENTER);
label1.setVerticalTextPosition(JLabel.CENTER);
add( label1 );
//
JLabel label2 = new JLabel( new ColorIcon(Color.YELLOW, 200, 150) );
label2.setLayout( new BoxLayout(label2, BoxLayout.Y_AXIS) );
add( label2 );
JLabel text = new JLabel( "More Control" );
text.setAlignmentX(JLabel.CENTER_ALIGNMENT);
label2.add( Box.createVerticalGlue() );
label2.add( text );
label2.add( Box.createVerticalStrut(10) );
//
JLabel label3 = new JLabel( new ColorIcon(Color.GREEN, 200, 150) );
add( label3 );
JLabel text3 = new JLabel();
text3.setText("<html><center>Text<br>over<br>Image<center></html>");
text3.setLocation(20, 20);
text3.setSize(text3.getPreferredSize());
label3.add( text3 );
//
JLabel label4 = new JLabel( new ColorIcon(Color.CYAN, 200, 150) );
add( label4 );
JTextPane textPane = new JTextPane();
textPane.setText("Add some text that will wrap at your preferred width");
textPane.setEditable( false );
textPane.setOpaque(false);
SimpleAttributeSet center = new SimpleAttributeSet();
StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
StyledDocument doc = textPane.getStyledDocument();
doc.setParagraphAttributes(0, doc.getLength(), center, false);
textPane.setBounds(20, 20, 75, 100);
label4.add( textPane );
}
public static class ColorIcon implements Icon
{
private Color color;
private int width;
private int height;
public ColorIcon(Color color, int width, int height)
{
this.color = color;
this.width = width;
this.height = height;
}
public int getIconWidth()
{
return width;
}
public int getIconHeight()
{
return height;
}
public void paintIcon(Component c, Graphics g, int x, int y)
{
g.setColor(color);
g.fillRect(x, y, width, height);
}
}
private static void createAndShowUI()
{
JFrame frame = new JFrame("LabelImageText");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( new LabelImageText() );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}