将JLabel放在JPanel中的组件上 [英] Put JLabel on Component in JPanel

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

问题描述

我有一个JPanel,其中包含一个组件imageWindow(

I have JPanel which contain a component imageWindow(object of this) now i want to put a label tag in the center of this imagewindow but i am not able to do so?

    JPanel imagePanel = new JPanel();
    imagePanel.setLayout(new GridBagLayout());
    imagePanel.add(ic);  //ic is imageWindow obj
    JLabel l1=new JLabel("First Label.");  

    JPanel example =new JPanel();
    example.add(l1);
    imagePanel.add(example);

我在这里做错的是屏幕显示,我需要在这里放标签.

what i am doing wrong here is screenshow i need to put the label here.

我想将标签放在中间,但是它总是出现在右侧,这是我做错了吗?

i want to put the label at the center but it always coming in the right side what i am doing wrong?

推荐答案

您可以在JPanel中显示图像,并在其PaintComponent方法中对其进行绘制,为该JPanel提供一个GridBagLayout,然后向其中添加JLabel.或者类似地,您可以在JLabel中将图像显示为ImageIcon,为该JLabel提供GridBagLayout,然后向其中添加包含JLabel的文本.

You could display the image in a JPanel that draws it in its paintComponent method, give that same JPanel a GridBagLayout, and add the JLabel to it. Or similarly you could display the image as an ImageIcon in a JLabel, give that JLabel a GridBagLayout, and add a text containing JLabel to it.

例如,在下面的代码中,我在JPanel的paintComponent方法中显示图像,为该面板提供GridBagLayout,然后将JLabel添加到该 same 没有网格袋约束的JPanel,这会将JLabel放在JPanel的中心:

For instance in the code below, I display an image in the paintComponent method of a JPanel, give that panel a GridBagLayout, and then add a JLabel to that same JPanel without gridbag constraints, and this will center the JLabel on the JPanel:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.*;

public class ImageFun extends JPanel {
    private static final String IMG_PATH = "https://aws.labome.com/figure/te-207-12.png";
    private static final String LABEL_TEXT = "Text is Centered";  // JLabel text
    private static final Font LABEL_FONT = new Font(Font.SANS_SERIF, Font.BOLD, 48);
    private static final Color LABEL_FG = Color.YELLOW;
    private BufferedImage img = null;  // our image

    public ImageFun(BufferedImage img) {
        this.img = img;
        setLayout(new GridBagLayout());

        // create JLabel 
        JLabel label = new JLabel(LABEL_TEXT);
        label.setFont(LABEL_FONT);
        label.setForeground(LABEL_FG);
        add(label);  // add without constraints
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (img != null) {
            // only draw if the image is not null
            g.drawImage(img, 0, 0, null);
        }
    }

    @Override
    public Dimension getPreferredSize() {
        if (isPreferredSizeSet() || img == null) {
            return super.getPreferredSize();
        }

        // size the JPanel to match that of the image
        int prefW = img.getWidth();
        int prefH = img.getHeight();
        return new Dimension(prefW, prefH);
    }

    private static void createAndShowGui() {

        BufferedImage bImg = null;
        try {
            // get an image
            URL imgUrl = new URL(IMG_PATH);
            bImg = ImageIO.read(imgUrl);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // shove the image into our JPanel
        ImageFun imageFun = new ImageFun(bImg);

        JFrame frame = new JFrame("Image Fun");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(imageFun);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}

这篇关于将JLabel放在JPanel中的组件上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆