我已经学习 Java几个星期了,在将背景图像应用到JFrame时我真的很困惑.我遇到的每个教程都没有按照我的方式创建框架(我扩展JFrame),或者如果他们这样做,说明不够清楚,我不能理解.
下面的代码来自我自己的项目,所以帮助我练习到目前为止我学到的东西.请你可以在下面的代码的基础上,向我解释添加什么和在哪里,所以我可能有一个图像作为我的框架的背景?
我真的很感激的一件事是,如果你能解释事情是如何运作的,为什么需要它们以及它们实际上在做什么 – 我不喜欢盲目地复制和粘贴你所做的事情的想法,而不知道它是如何工作的.解释越深入越好;即使它听起来很光顾.
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);
}
}
}