有没有在JFrame中添加图片内的文字?

时间:2013-08-20 08:29:47

标签: java image swing text jframe

编辑:该死的,我的英语有点偏。我打算问一下如何在文本中间添加文本“内部”图片而不是(上图)文本。 谢谢你以前的帮助:)。

3 个答案:

答案 0 :(得分:3)

不确定。将文本直接绘制到图片上。在标签中显示图片。

E.G。

当然,如果你想要“更简单”,还有其他选择。一个是OverlayLayout,如@Reimeus所述。这是另一个。它利用了我们可以为标签设置布局并在其中显示组件的事实。这项技术由@mKorbel推广。

Text on Image

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个组件。

enter image description here

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();
            }
        });
    }
}