在JFrame使用Swing的设置背景图片 [英] Setting a background image on a JFrame using Swing

查看:206
本文介绍了在JFrame使用Swing的设置背景图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在学习Java的几个星期,我真的卡住,当涉及到应用的背景图像的JFrame。每个教程中我遇到不创建框架怎么办?(我延长JFrame的),或者如果他们这样做,指示不够清晰,我理解的方式。

在code以下是从我自己的项目,所以帮我练什么我已经学会为止。请你可以建立下面的code和向我解释如何添加和在哪里,所以我可能有一个图像为背景,以我的框架?

有一件事我真的AP preciate是,如果你能解释一下如何工作以及为什么有必要和他们实际上做什么 - 我不喜欢盲目照搬的想法,并粘贴你做了什么没有它是如何工作的任何线索。在解释更深入,更好;即使这听起来光顾。

 进口的javax.swing *。
进口java.awt中的*。
java.awt.event中导入*。类MiniPad扩展JFrame中实现的ActionListener {    泛的JPanel =新JPanel();
    ClassLoader的LDR = this.getClass()getClassLoader()。
    ImageIcon的closeImg =新的ImageIcon(ldr.getResource(\\\\ \\\\图像按钮\\\\ closeBtn.png));
    JTextArea中注=新的JTextArea(,6,21);
    JScrollPane中指出=新JScrollPane的(注);
    JButton的密切=的新的JButton(closeImg);    公共静态无效的主要(字串[] args){
        MiniPad padgui =新MiniPad();
    } // GUI的实例    公共MiniPad(){
        超级(记事本);
        的setSize(265,191);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        加(PAN);
        调用setVisible(真);
//规格
        note.setLineWrap(真);
        note.setWrapStyleWord(真);
        notes.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_​​ALWAYS);
        close.setBorderPainted(假);
        close.setContentAreaFilled(假);
        close.setOpaque(假);
//添加到JPanel的'锅'
        pan.add(注);
        pan.add(接近);
        close.addActionListener(本);
    }    公共无效的actionPerformed(ActionEvent的事件){
        如果(event.getSource()==接近){
            调用setVisible(假);
        }
    }
}


解决方案

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

您想设置一个自定义的组件(如的JP​​anel ),并覆盖的paintComponent

看看

现在,你将有一些问题,注释窗格中会阻止大多数的背景下,模糊图像。这不应该试图劝阻虽然;)

您会希望熟悉Swing组件是如何进行的透明为好。

一些反馈;)

我不认为你需要保持一个参考类加载器,为你使用它,它只是增加重量和复杂性,以您的$ C什么$ C。这不是一个巨大的问题,但我的第一个问题是,什么是使用类加载器为他 -

我可能会采取某种形式的命名约定为好。一般都有点更详细的会更容易理解code。

关闭 ??那是一个行动?也许像closeButton,至少我知道这是某种类型的按钮

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

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

Take a look at

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 ;)

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

Some feedback ;)

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.

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆