设置多JLabel在JPanel中的具体位置
Set specific location of multi JLabel in JPanel
我需要在 JFrame 中全屏显示背景图像,并在其上的两个不同位置写入,像这样
这是我目前的代码,但效果不佳。
JPanel container = new N_BackgroundImage(new ImageIcon(scaledImage), screenIndex);
container.setLayout(new FlowLayout(FlowLayout.RIGHT));
container.add(sign);
container.add(dateAndName);
// Adding the new Container to the JFrame
getContentPane().add(container);
结果是:
有人知道我想做什么吗?
我找到了解决方案,
我将所有 JLabel 拆分到容器 (JPanel) 并按照我的意愿设置它们的视图,之后我将它们添加回 imageConainer
中,就像您将看到的那样,并将 imageContainer
添加到JFrame.
所以我的代码是这样的:
JPanel imageContainer;
JPanel dateContainer = new JPanel();
JPanel signContainer = new JPanel();
imageContainer = new N_BackgroundImage(iconToImage(new RotatedIcon(new ImageIcon(scaledImage), RotatedIcon.Rotate.UP)), (int) picWidth, (int) picHeight, screenIndex);
imageContainer.setLayout(new BorderLayout());
dateContainer.setLayout(new BorderLayout());
dateContainer.setBorder(new EmptyBorder(0, 0, 15, 15));
dateContainer.setBackground(new Color(1f, 0f, 0f, .0f)); // Transparent background
dateContainer.setForeground(Color.ORANGE);
dateContainer.add(dateAndName, BorderLayout.PAGE_END);
signContainer.setLayout(new BorderLayout());
signContainer.setBorder(new EmptyBorder(0, 15, 15, 0));
signContainer.setBackground(new Color(1f, 0f, 0f, .0f)); // Transparent background
signContainer.setForeground(Color.WHITE);
signContainer.add(sign, BorderLayout.PAGE_END);
// Adding the new Layer on too of the image
imageContainer.add(dateContainer, BorderLayout.LINE_END);
imageContainer.add(signContainer, BorderLayout.LINE_START);
// Adding the new Container to the JFrame
getContentPane().add(imageContainer);
我需要在 JFrame 中全屏显示背景图像,并在其上的两个不同位置写入,像这样
这是我目前的代码,但效果不佳。
JPanel container = new N_BackgroundImage(new ImageIcon(scaledImage), screenIndex);
container.setLayout(new FlowLayout(FlowLayout.RIGHT));
container.add(sign);
container.add(dateAndName);
// Adding the new Container to the JFrame
getContentPane().add(container);
结果是:
有人知道我想做什么吗?
我找到了解决方案,
我将所有 JLabel 拆分到容器 (JPanel) 并按照我的意愿设置它们的视图,之后我将它们添加回 imageConainer
中,就像您将看到的那样,并将 imageContainer
添加到JFrame.
所以我的代码是这样的:
JPanel imageContainer;
JPanel dateContainer = new JPanel();
JPanel signContainer = new JPanel();
imageContainer = new N_BackgroundImage(iconToImage(new RotatedIcon(new ImageIcon(scaledImage), RotatedIcon.Rotate.UP)), (int) picWidth, (int) picHeight, screenIndex);
imageContainer.setLayout(new BorderLayout());
dateContainer.setLayout(new BorderLayout());
dateContainer.setBorder(new EmptyBorder(0, 0, 15, 15));
dateContainer.setBackground(new Color(1f, 0f, 0f, .0f)); // Transparent background
dateContainer.setForeground(Color.ORANGE);
dateContainer.add(dateAndName, BorderLayout.PAGE_END);
signContainer.setLayout(new BorderLayout());
signContainer.setBorder(new EmptyBorder(0, 15, 15, 0));
signContainer.setBackground(new Color(1f, 0f, 0f, .0f)); // Transparent background
signContainer.setForeground(Color.WHITE);
signContainer.add(sign, BorderLayout.PAGE_END);
// Adding the new Layer on too of the image
imageContainer.add(dateContainer, BorderLayout.LINE_END);
imageContainer.add(signContainer, BorderLayout.LINE_START);
// Adding the new Container to the JFrame
getContentPane().add(imageContainer);