【问题标题】:Put JLabel on Component in JPanel将 JLabel 放在 JPanel 中的组件上
【发布时间】:2018-01-23 21:04:06
【问题描述】:

我有一个 JPanel,其中包含一个组件 imageWindow(this 的对象)现在我想在这个 imagewindow 的中心放置一个标签标签,但我不能这样做?

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

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

我想把标签放在中间,但它总是在右边我做错了什么?

【问题讨论】:

  • 澄清一下,您希望标签位于 imageWindow 的顶部吗?而您正试图通过重叠两个独立的 JPanel 来实现这一点?
  • 您可以在 JPanel 中显示图像,该 JPanel 在其 paintComponent 方法中绘制它,给 that 相同的 JPanel 一个 GridBagLayout,然后将 JLabel 添加到它。或者类似地,您可以在 JLabel 中将图像显示为 ImageIcon,给该 JLabel 一个 GridBagLayout,然后向其中添加包含 JLabel 的文本。
  • 顺便问一下,这是带有病毒包涵体的细胞的 EM 图像吗?
  • @LukeThistlethwaite 是的,我的目标是将标签放在 imageWindow 的顶部。
  • @HovercraftFullOfEels 我无法将图像显示为 imageIcon,因为我有一堆图像并且我使用 imageWindow 显示它,我也有下面的滑块,通过滑动下一个图像将从堆栈中显示跨度>

标签: java swing jpanel awt


【解决方案1】:

您可以在 JPanel 中显示图像,该 JPanel 在其 paintComponent 方法中绘制它,为相同的 JPanel 提供 GridBagLayout,然后将 JLabel 添加到其中。或者类似地,您可以在 JLabel 中将图像显示为 ImageIcon,给该 JLabel 一个 GridBagLayout,然后向其添加包含 JLabel 的文本。

例如在下面的代码中,我在 JPanel 的 paintComponent 方法中显示一个图像,给该面板一个 GridBagLayout,然后将一个 JLabel 添加到那个 same JPanel 没有网格包约束,这将使 JLabel 居中于 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());
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-07
    • 2013-12-23
    • 1970-01-01
    相关资源
    最近更新 更多