使用 Swing 在 JFrame 上设置背景图像

Setting a background image on a JFrame using Swing(使用 Swing 在 JFrame 上设置背景图像)
本文介绍了使用 Swing 在 JFrame 上设置背景图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经学习 Java 几个星期了,在将背景图像应用到 JFrame 时,我真的陷入了困境.我遇到的每个教程都没有按照我的方式创建框架(我扩展了 JFrame),或者如果他们这样做了,说明不够清晰,我无法理解.

I have been learning Java for a few weeks now and I am really stuck when it comes to applying a background image to a JFrame. Every tutorial I've come across doesn't create Frames the way I do ( I extend JFrame ) or if they do, the instructions aren't clear enough for me to understand.

下面的代码来自我自己的一个项目,因此请帮助我练习目前所学的知识.请您在下面的代码的基础上向我解释要添加的内容和位置,以便我可以将图像作为框架的背景?

The code below is from a project of my own so help me practice what I've learned so far. Please could you build on the code below and explain to me what to add and where, so I may have an image as the background to my frame?

我非常感激的一件事是,如果你能解释一下事情是如何运作的,为什么需要它们以及它们实际在做什么 - 我不喜欢盲目地复制和粘贴你所做的事情而没有任何线索的想法怎么运行的.解释越深入越好;即使听起来很傲慢.

One thing I would really appreciate is if you could explain how things work and why there are needed and what they are actually doing - I don't like the idea of blindly copying and pasting what you've done without any clue of how it works. The more depth in explanation, the better; even if it sounds patronising.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class MiniPad extends JFrame implements ActionListener {

    JPanel pan = new JPanel();
    ClassLoader ldr = this.getClass().getClassLoader();
    ImageIcon closeImg = new ImageIcon(ldr.getResource("\images\buttons\closeBtn.png"));
    JTextArea note = new JTextArea("", 6, 21);
    JScrollPane notes = new JScrollPane(note);
    JButton close = new JButton(closeImg);

    public static void main(String[] args) {
        MiniPad padgui = new MiniPad();
    } //Instance of GUI

    public MiniPad() {
        super("Notepad");
        setSize(265, 191);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        add(pan);
        setVisible(true);
//Specifications
        note.setLineWrap(true);
        note.setWrapStyleWord(true);
        notes.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        close.setBorderPainted(false);
        close.setContentAreaFilled(false);
        close.setOpaque(false);
//Adding to JPanel 'pan'
        pan.add(notes);
        pan.add(close);
        close.addActionListener(this);
    }

    public void actionPerformed(ActionEvent event) {
        if (event.getSource() == close) {
            setVisible(false);
        }
    }
}

推荐答案

至于你的问题,因为它的标题

As to your question as it's titled

您需要设置一个自定义 Component(例如 JPanel)并覆盖 paintComponent 方法

You will want to setup a custom Component (such as a JPanel) and override the paintComponent method

看看

  • 在 Swing 中执行自定义绘画
  • Java 2D 图形

现在,您将遇到一些问题,注释窗格将挡住大部分背景,使图像变得模糊.不过,这不应该阻止尝试;)

Now, you're going to have some issues, the note pane will block most of the background, obscuring the image. This shouldn't discourage from trying though ;)

您需要熟悉如何制作 Swing 组件 透明.

You'll want to familiarise yourself with how Swing components can be made transparent as well.

一些反馈;)

我认为您不需要保留对 Classloader 的引用,因为您使用它的目的只是增加代码的权重和复杂性.这不是一个大问题,但我的第一个问题是,他使用类加载器做什么?"

I don't think you need to keep a reference to the Classloader, for what you're using it for, it's just adding weight and complexity to your code. It's not a massive issue, but my first question was, "What's he using the class loader for??"

我可能也会采用某种命名约定.一般稍微冗长一点会更容易理解代码.

I'd probably adopt some kind of naming convention as well. Generally been a little more verbose will make it easier to understand the code.

关闭 ??这是一个动作吗?也许像closeButton"之类的东西,至少我知道它是某种类型的按钮

close ?? Is that an action? Maybe something like "closeButton", at least I know it's some type of button

这篇关于使用 Swing 在 JFrame 上设置背景图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How can create a producer using Spring Cloud Kafka Stream 3.1(如何使用Spring Cloud Kafka Stream 3.1创建制片人)
Insert a position in a linked list Java(在链接列表中插入位置Java)
Did I write this constructor properly?(我是否正确地编写了这个构造函数?)
Head value set to null but tail value still gets displayed(Head值设置为空,但仍显示Tail值)
printing nodes from a singly-linked list(打印单链接列表中的节点)
Control namespace prefixes in web services?(控制Web服务中的命名空间前缀?)