允许多个 JLabel 中的文本重叠
Allow text in multiple JLabels to overlap
如果任何答案都需要的话,这里是上下文。我正在构建一个引擎,我将在其中制作视频游戏。它涉及 96 x 54(列 x 行)table 的字母,以保持它们之间的间距均匀。正因为如此,如果任何解决方案都可以在合理可行的情况下尽可能减少资源消耗,那将非常有帮助。我为这个引擎做了一个网络演示,除了有点慢之外,它完全按预期工作。现在我正在将项目迁移到 Java,但有些事情没有按预期工作。
为了模仿 HTML <table>
我在一对 JPanel
中使用了 GridLayout
的 JLabel
来保持大小不变,所有在 JFrame
里面。我面临的问题是,由于更改 table 的大小以改善外观,JLabel 略有重叠。在网络演示中,这很好,因为字母只是简单地进入了下一个框。这就是我在 Java 中想要实现的目标,但我终其一生都无法找到方法。
这是一张图片,可以向您展示我的意思:
在左边的网络演示中,我们在 "a" 圈内有一圈下划线,右边是字母 "pqyjg"。其中一条下划线上还有一个灰色小框。这是下划线下方突出显示的框,显示下划线与它重叠 1 个像素。
当我们将同样的代码放入Java版本时,下划线不见了,字母"pqyjg"的尾巴被剪掉了。期望的效果是让它像左边的例子一样工作。
我搜索了这个网站、互联网的其余部分并浏览了许多 Java class 页面以寻找有用的方法,但无济于事。
任何人都可以指出我可以在 JLabel
或任何其他组件上调用的 class 或方法来实现此效果,而无需更改 table 的大小吗?
这是我当前设置所有内容的代码,以防对任何人有帮助。
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 ");
}
}
如果我没看错的话,你问题的一部分是如何设置 JLabel 的背景颜色?如果是那么:
label.setOpaque(true);
label.setBackground(Color.lightGray);// Background color
我已经修改了您的代码以允许调整整个 window 的大小。然后当字体大小太大以适应标签 space 时,问题就出现了。在我看来,您应该根据标签大小来计算字体大小,类似于:
import javax.swing.*;
import static java.lang.Math.*;
import java.awt.*;
public class testG {
//Declaring constants
//public static final Color[] MAINFRAME = {new Color(0x35ce4a), new Color(0x111111)};
public static final Color[] MAINFRAME = {Color.GREEN, Color.BLACK};
//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(new BorderLayout());
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", JLabel.CENTER);
//tempLabel.setFont(new Font("Courier", Font.PLAIN, 15));
tempLabel.setForeground(Color.GREEN);
tempLabel.setBorder(BorderFactory.createLineBorder(Color.BLUE));
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, BorderLayout.CENTER);
//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++){
JLabel labelTemp = table[(int) floor((double) i / 96)][i % 96];
labelTemp.setText(String.valueOf(s.charAt(i)));
}
}
public static void main(String[] args){
initialise();
testG.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 方法本身中。
如果任何答案都需要的话,这里是上下文。我正在构建一个引擎,我将在其中制作视频游戏。它涉及 96 x 54(列 x 行)table 的字母,以保持它们之间的间距均匀。正因为如此,如果任何解决方案都可以在合理可行的情况下尽可能减少资源消耗,那将非常有帮助。我为这个引擎做了一个网络演示,除了有点慢之外,它完全按预期工作。现在我正在将项目迁移到 Java,但有些事情没有按预期工作。
为了模仿 HTML <table>
我在一对 JPanel
中使用了 GridLayout
的 JLabel
来保持大小不变,所有在 JFrame
里面。我面临的问题是,由于更改 table 的大小以改善外观,JLabel 略有重叠。在网络演示中,这很好,因为字母只是简单地进入了下一个框。这就是我在 Java 中想要实现的目标,但我终其一生都无法找到方法。
这是一张图片,可以向您展示我的意思:
在左边的网络演示中,我们在 "a" 圈内有一圈下划线,右边是字母 "pqyjg"。其中一条下划线上还有一个灰色小框。这是下划线下方突出显示的框,显示下划线与它重叠 1 个像素。
当我们将同样的代码放入Java版本时,下划线不见了,字母"pqyjg"的尾巴被剪掉了。期望的效果是让它像左边的例子一样工作。
我搜索了这个网站、互联网的其余部分并浏览了许多 Java class 页面以寻找有用的方法,但无济于事。
任何人都可以指出我可以在 JLabel
或任何其他组件上调用的 class 或方法来实现此效果,而无需更改 table 的大小吗?
这是我当前设置所有内容的代码,以防对任何人有帮助。
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 ");
}
}
如果我没看错的话,你问题的一部分是如何设置 JLabel 的背景颜色?如果是那么:
label.setOpaque(true);
label.setBackground(Color.lightGray);// Background color
我已经修改了您的代码以允许调整整个 window 的大小。然后当字体大小太大以适应标签 space 时,问题就出现了。在我看来,您应该根据标签大小来计算字体大小,类似于:
import javax.swing.*;
import static java.lang.Math.*;
import java.awt.*;
public class testG {
//Declaring constants
//public static final Color[] MAINFRAME = {new Color(0x35ce4a), new Color(0x111111)};
public static final Color[] MAINFRAME = {Color.GREEN, Color.BLACK};
//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(new BorderLayout());
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", JLabel.CENTER);
//tempLabel.setFont(new Font("Courier", Font.PLAIN, 15));
tempLabel.setForeground(Color.GREEN);
tempLabel.setBorder(BorderFactory.createLineBorder(Color.BLUE));
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, BorderLayout.CENTER);
//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++){
JLabel labelTemp = table[(int) floor((double) i / 96)][i % 96];
labelTemp.setText(String.valueOf(s.charAt(i)));
}
}
public static void main(String[] args){
initialise();
testG.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 方法本身中。