简体   繁体   English

如何在 JPanel java swing 中移动标签

[英]How to move labels in a JPanel java swing

I have a panel, and I am trying to have a background image, and two sets of text, a "title" and text area to add text to later how do I get the text area to go on top of the image (label) or if there is a completely better way to achieve this?我有一个面板,我正在尝试有一个背景图像和两组文本,一个“标题”和文本区域以稍后添加文本如何将文本区域添加到图像顶部的 go(标签)或者是否有更好的方法来实现这一目标?

` //For Text (right) ` //对于文本(右)

    JPanel textPanel = new JPanel();
    //textPanel.setLayout(); //I don't understand layouts I tried some non really got what I needed
    textPanel.setBorder(border);
    textPanel.setBounds(190, 0, 1300, 886);
    textPanel.setBackground(Color.BLACK);
    
//Imports For inventory Label
    JLabel textLabel = new JLabel(); //Title label
    arrayTXTA = new JTextArea(0,1);//The text I will add later label
    JLabel textLabelbg = new JLabel(); //Background image label
    ImageIcon Teximage =new ImageIcon("src\\emporium\\pkg\\background.jpg");//sets up image for background      
//Code for background image
    textLabelbg.setIcon(Teximage);//adds an image
    textLabelbg.setBounds(70, 5, 1200, 886);
                //TEXT AT 253
//Code for title
    textLabel.setHorizontalTextPosition(JLabel.CENTER);  icon
    textLabel.setVerticalTextPosition(JLabel.TOP);
    textLabel.setForeground(new Color(0,255,0));
    textLabel.setFont(new Font("Blackadder ITC",Font.BOLD,30));
    //textLabel.setBounds(300, 0, 100, 36);
//Code for Text I will add later
    arrayTXTA.setVisible(true);
    arrayTXTA.append("Words");
    arrayTXTA.append("Words");
    arrayTXTA.append("Words");
    arrayTXTA.setForeground(new Color(255,0,0));
    arrayTXTA.setFont(new Font("Blackadder ITC",Font.BOLD,30));
    arrayTXTA.setBackground(new Color(0, 0, 0, 0));
    arrayTXTA.setBounds(25, 0, 0, 0);
    `

Here is a picture of what I am trying to achieve这是我要实现的目标的图片

Thank you in advance先感谢您

Firstly首先

Don't use null layouts, they aren't helping you.不要使用null布局,它们对您没有帮助。 Take the time to learn and use the available layout managers, see Laying Out Components Within a Container for more details.花时间学习和使用可用的布局管理器,有关更多详细信息,请参阅在容器中布局组件

Secondly第二

JLabel is a bad choice to trying to make background image. JLabel是尝试制作背景图像的错误选择。 There's a few reasons, but lets just say they aren't really designed for the job有几个原因,但可以说它们并不是真正为这项工作而设计的

Thirdly第三

Start with a custom component and paint the background you want via it.从自定义组件开始,并通过它绘制您想要的背景。 Use it as the primary container for the JTextArea .将其用作JTextArea的主要容器。

Configure the JScrollPane and JTextArea to be transparent and you will see the background through itJScrollPaneJTextArea配置为透明,您将通过它看到背景

在此处输入图像描述

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class Test {
    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    JFrame frame = new JFrame();
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                } catch (IOException ex) {
                    Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    public class TestPane extends JPanel {

        private BufferedImage background;

        public TestPane() throws IOException {
            background = ImageIO.read(getClass().getResource("/images/Paper.png"));
            setLayout(new BorderLayout());

            JTextArea ta = new JTextArea("This is some text");
            ta.setOpaque(false);

            JScrollPane sp = new JScrollPane(ta);
            sp.setOpaque(false);
            sp.getViewport().setOpaque(false);

            add(sp);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(background.getWidth(), background.getHeight());
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.drawImage(background, 0, 0, this);
            g2d.dispose();
        }

    }
}

As to the rest of you layout.至于你布局的rest。 Simple, make use of a number of containers/panels with their own layout managers and then bind the all together into a "master" panel with a suitable layout manager ( BorderLayout looks like it would work)很简单,使用一些带有自己的布局管理器的容器/面板,然后将它们绑定到一个带有合适布局管理器的“主”面板中( BorderLayout看起来会起作用)

After thoughts...想了想之后...

Depending on your needs, I might consider having some more logic in the paintComponent method to allow it paint more instances of the background if the container was resized beyond the size of the base background image, but that would depend on the image and your needs.根据您的需要,我可能会考虑在paintComponent方法中添加更多逻辑,以允许它在容器的大小调整到超出基本背景图像的大小时绘制更多背景实例,但这取决于图像和您的需求。

While more difficult, you could look at painting the background directly to the JTextArea (by overriding it), this would allow the background to scroll with the JTextArea , but would also require you to consider the point above虽然更困难,但您可以查看将背景直接绘制到JTextArea (通过覆盖它),这将允许背景与JTextArea一起滚动,但还需要您考虑上述要点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM