java - 允许多个 JLabel 中的文本重叠

标签 java swing jlabel

如果任何答案都需要的话,这里是上下文。我正在构建一个引擎,我将在其中制作视频游戏。它涉及一个 96 x 54(列 x 行)的字母表,以保持它们之间的间距均匀。正因为如此,如果任何解决方案都可以在合理可行的情况下尽可能减少资源消耗,那将非常有帮助。我为这个引擎做了一个网络演示,除了有点慢之外,它完全按预期工作。现在我正在将项目迁移到 Java,但有些事情并没有按预期工作。

模仿 HTML <table>我用过 GridLayoutJLabel在一对夫妇里面 JPanel s 保持大小不变​​,都在 JFrame 内.我面临的问题是,由于更改表格的大小以改善外观,JLabel 略有重叠。在网络演示中,这很好,因为字母只是简单地进入了下一个框。这就是我试图在 Java 中实现的目标,但我一生都无法找到实现的目标。

这是一张图片,向您展示我的意思:

Here's an image to show you what I mean.

在左边的网络演示中,我们在“a”圆圈内有一圈下划线,右边是字母“pqyjg”。其中一条下划线上还有一个灰色小框。这是下划线下方的突出显示框,表明下划线与其重叠 1 个像素。

当我们将这段相同的代码放入 Java 版本时,下划线无处可寻,字母“pqyjg”的尾部也被剪掉了。期望的效果是让它像左边的示例一样工作。

我搜索了这个网站、互联网的其余部分以及许多 Java 类页面以寻找一种有用的方法,但无济于事。

任何人都可以指出我可以在 JLabel 上调用的类或方法吗? s 或任何其他组件来实现这种效果,而不改变表格的大小?

这是我当前设置所有内容的代码,以防对任何人有帮助。

import javax.swing.*;
import static java.lang.Math.*;
import java.awt.*;

public class transparencyExample{

    //Declaring constants
    public static final Color[] MAINFRAME = {new Color(0x35ce4a), new Color(0x111111)};

    //Creating static variables and methods
    private static JLabel tempLabel;
    private static JLabel[][] table = new JLabel[54][96];
    private static JPanel layout = new JPanel(new GridLayout(54,96));
    private static JPanel background = new JPanel();
    private static BoxLayout box = new BoxLayout(background, 0);
    private static JFrame frame = new JFrame("Transparency Example");
    private static void initialise(){

        //Adding labels to table
        for (int i = 0; i < 5184; i++){
            tempLabel = new JLabel("M", SwingConstants.CENTER);
            tempLabel.setFont(new Font("Courier", Font.PLAIN, 15));
            table[(int) floor((double) i / 96)][i % 96] = tempLabel;
        }

        //Laying out the table
        layout.setPreferredSize(new Dimension(1056, 594));
        layout.setOpaque(false);
        for(int i = 0; i < 5184; i++){
            layout.add(table[(int) floor((double) i / 96)][i % 96]);
        }
        background.setBackground(MAINFRAME[1]);
        background.add(layout);

        //Laying out the frame
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(background);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    //Fill table
    public static void fill(String s){
        for(int i = 0; i < 5184; i++){
            table[(int) floor((double) i / 96)][i % 96].setText(String.valueOf(s.charAt(i)));
        }
    }
    public static void main(String[] args){
        initialise();
        transparencyExample.fill("                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         aaaaaaaa                                                                                       a   a    aa                                                                                    a _____     a                                                                                  a _     ___  a                                                                                 a_         __  a                                                                               a _           _ a     pqyjg                                                                   aa _            _ a                                                                             a  _             _ a                                                                           a   _             _ a                                                                           a   _             _ a                                                                           a   _             _ a                                                                           a    _           _  a                                                                           a    __          _ a                                                                            a      __      __  a                                                                             a       _______   a                                                                             a                a                                                                              a               aa                                                                               aa            a                                                                                   aa       aa                                                                                       aa aaaa                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  ");
    }
}

最佳答案

根据我的评论:

... and I'm not 100% as to the effect you're trying to create and the problems that you may be having, but often problems in this category are due to trying to artificially constrain the sizes of components and containers

是的,您的问题是您使用 layout.setPreferredSize(new Dimension(1056, 594)); 人为地限制了布局 JPanel 的大小,这阻止了所有组件它应该得到很好的展示。如果您删除该行,然后打包并设置 Visible(true) 您的 JFrame,用数据填充它之后,您将看到完整的文本。


您的评论:

I was trying to keep the size of the table the same, but instead let the JLabels overlap so that their text can overflow from one into the other.

考虑选项 2:不使用 JLabel,而是将所需文本直接绘制到显示在 JPanel 的 paintComponent 方法中的 BufferedImage 中,或者直接绘制到 JPanel 的 paintComponent 方法本身中。

关于java - 允许多个 JLabel 中的文本重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38745943/

相关文章:

JavaH 无法为 JNI 类创建 C 头文件

java - 使用 Xuggler 进行音频转换

java - 为什么我点击的地方不绘制矩形?

java - 如何在 jframe 上使用 Imageicon 打开图像

java - 在标签中显示%

java - Tomcat 7.0.79 服务器认证问题

java - 为什么 Hibernate 使用序列

java - Swing的KeyListener和同时按下的多个键

java - 从网络驱动器加载图像?

java - JLabel + 图片