【问题标题】:How to overlap a JPanel(background) and a JTextField如何重叠 JPanel(背景)和 JTextField
【发布时间】:2019-11-04 12:23:09
【问题描述】:

我正在为某人的毕业创建一个登录应用程序,我需要几个文本字段和一个背景,我已经添加了背景,现在需要添加文本字段,问题是它们似乎不会在上面彼此的。

我已经分别尝试了它们,并且没有彼此它们都可以完美地工作,但我无法将它们堆叠起来,我在这个网站上看到了几个解决类似问题的答案,但对于这个应用程序,我需要放置几个背景上的文本字段仅与一个文本字段相对,这是我迄今为止所拥有的...

        //creates the frame with a title as a parameter
        JFrame frame = new JFrame("Sign In Sheet");
        //sets the size
        frame.setSize(1000, 556);
        //makes it so the application stops running when you close it
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //puts it in the center of the screen
        frame.setLocationRelativeTo(null);
        //makes it so you can't resize it
        frame.setResizable(false);

        //setting the background by looking for the image
        try{
            frame.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("C:/Users/Gabriel R. Warner/Desktop/clouds.png")))));
        }catch(IOException e){
            //and prints an error message if it's not found
            System.out.println("well it didn't work");
        }

        //adding text fields with names apropriate to function
        JTextField name1 = new JTextField();
        name1.setPreferredSize(new Dimension(200, 15));
        name1.setBackground(Color.WHITE);
        frame.add(name1);

        //makes frame visible
        frame.setVisible(true);

简单地说,文本字段不会与背景一起显示,所有结果仅提供单个文本字段的答案

【问题讨论】:

  • 1) 请注意您的语言,2) 您只添加了一个JTextField,并没有您提到的那么多,发布一个正确的minimal reproducible example 来证明您的问题。 3)不要显式地setPreferredSizesetSize你的组件,因为JTextField作为参数传入它应该使用多少列(15在你的情况下)并调用pack()而不是你的JFrame .
  • 4) 当您的代码被包装为 JAR 时,图像将成为嵌入式资源,因此明智的做法是将它们视为已经存在,请参阅embedded-resource 标签信息了解如何使用它而不是完整路径。这是example,介绍如何通过在另一个JLabel 中添加JLabel 或使用自定义绘画来添加图像作为背景

标签: java swing jframe jpanel jtextfield


【解决方案1】:

问题出在这一行:frame.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("C:/Users/Gabriel R. Warner/Desktop/clouds.png")))));

在这一行中,您将JLabel 设置为JFrame 的内容窗格。然后,你 frame.add(name1); 所以你正在将一个 JTextField 添加到一个 JLabel 中......好吧,这似乎不对,对吧?

答案是创建一个新的JPanel,将背景图像添加到此面板,将面板设置为框架的内容窗格,最后将文本字段添加到面板/内容窗格。

一个例子:

@SuppressWarnings("serial")
public class FrameWithBackgroundImage extends JFrame {

    public FrameWithBackgroundImage() {
        super("Sign In Sheet");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        try {
            Image bgImage = loadBackgroundImage();
            JPanel backgroundImagePanel = new JPanel() {

                @Override
                protected void paintComponent(Graphics g) {
                    super.paintComponent(g);
                    g.drawImage(bgImage, 0, 0, null);
                }
            };

            setContentPane(backgroundImagePanel);
        } catch (IOException e) {
            e.printStackTrace();
        }

        JTextField textField = new JTextField(10);

        add(textField);

    }

    private Image loadBackgroundImage() throws IOException {
        File desktop = new File(System.getProperty("user.home"), "Desktop");
        File image = new File(desktop, "img.jpg");
        return ImageIO.read(image);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            new FrameWithBackgroundImage().setVisible(true);
        });
    }
}

预览: 值得一读的问题:Simplest way to set image as JPanel background

【讨论】:

  • “所以你正在将一个 JTextField 添加到一个 JLabel...嗯,这似乎不对,对吧?” 我同意这一点,但我应该说,这是可能的, JLabelContainerJButton 也是 Container,你可以这样做 interesting things with them
  • @Frakcool 我知道。我只是不推荐它,因为我觉得它有点棘手。我的意思是,你知道...基于逻辑。
  • 是的,我同意,在这种情况下,自定义绘画方法更好,但有时您可能需要使用它们,因此我留下该评论以供未来读者参考跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-21
  • 2016-05-17
  • 2016-06-13
  • 1970-01-01
  • 2016-03-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多