java 如何更改 jdesktoppane 默认背景图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13814704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change jdesktoppane default background image?
提问by Ayaz Ali
How to change jdesktoppane background image in MDI (Multiple Documents interface) using java netbeans? Means I added the jdesktoppane to java MDI so now I want to change default background image of that jdesktoppane which I'm using in java MDI. Any easy way?
如何使用 java netbeans 在 MDI(多文档界面)中更改 jdesktoppane 背景图像?意味着我将 jdesktoppane 添加到 java MDI,所以现在我想更改我在 java MDI 中使用的 jdesktoppane 的默认背景图像。有什么简便的方法吗?
Check attached snapshot link may be you will better understand my question what I want.
检查附加的快照链接可能会让您更好地理解我的问题我想要什么。
回答by David Kroukamp
+1 to MadProgrammers comment.
+1 到 MadProgrammers 评论。
Simply override JDesktopPane
paintComponent(..)
and call drawImage(Image img,int x,int y,ImageObserver io)
to draw an image.
只需覆盖JDesktopPane
paintComponent(..)
并调用drawImage(Image img,int x,int y,ImageObserver io)
即可绘制图像。
Dont forget to honor the paint chainand call super.paintComponent(g)
as first call in overridden paintComponent(..)
method
不要忘记尊重油漆链并super.paintComponent(g)
在重写的paintComponent(..)
方法中作为第一次调用调用
Here is an example:
下面是一个例子:
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;
public class JInternalFrameDemo {
private JDesktopPane jdpDesktop;
private static int openFrameCount = 0;
private BufferedImage img;
public JInternalFrameDemo() {
JFrame frame = new JFrame("JInternalFrame Usage Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
img = ImageIO.read(new URL("http://images1.wikia.nocookie.net/__cb20120817224359/villains/images/6/6a/Nine-Tailed_Fox_(Naruto).jpg"));
} catch (Exception ex) {
ex.printStackTrace();
}
// A specialized layered pane to be used with JInternalFrames
jdpDesktop = new JDesktopPane() {
@Override
protected void paintComponent(Graphics grphcs) {
super.paintComponent(grphcs);
grphcs.drawImage(img, 0, 0, null);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(img.getWidth(), img.getHeight());
}
};
createFrame(); // Create first window
frame.setContentPane(jdpDesktop);
frame.setJMenuBar(createMenuBar());
// Make dragging faster by setting drag mode to Outline
jdpDesktop.putClientProperty("JDesktopPane.dragMode", "outline");
frame.pack();
frame.setVisible(true);
}
protected JMenuBar createMenuBar() {
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("Frame");
menu.setMnemonic(KeyEvent.VK_N);
JMenuItem menuItem = new JMenuItem("New IFrame");
menuItem.setMnemonic(KeyEvent.VK_N);
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
createFrame();
}
});
menu.add(menuItem);
menuBar.add(menu);
return menuBar;
}
protected void createFrame() {
MyInternalFrame frame = new MyInternalFrame();
frame.setVisible(true);
// Every JInternalFrame must be added to content pane using JDesktopPane
jdpDesktop.add(frame);
try {
frame.setSelected(true);
} catch (java.beans.PropertyVetoException e) {
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new JInternalFrameDemo();
}
});
}
class MyInternalFrame extends JInternalFrame {
static final int xPosition = 30, yPosition = 30;
public MyInternalFrame() {
super("IFrame #" + (++openFrameCount), true, // resizable
true, // closable
true, // maximizable
true);// iconifiable
setSize(300, 300);
// Set the window's location.
setLocation(xPosition * openFrameCount, yPosition
* openFrameCount);
}
}
}
回答by Ahmed Salem
I resolve it in a separate function to create a desktop object.
我在一个单独的函数中解析它来创建一个桌面对象。
Code as below
代码如下
private JDesktopPane intializeDesktop(JDesktopPane mydesktop,String imagePath,int scalx,int scaly) {
// A specialized layered pane to be used with JInternalFrames
mydesktop = new JDesktopPane() {
ImageIcon icon = new ImageIcon(imagePath);
Image image = icon.getImage();
Image newimage = image.getScaledInstance(scalx, scaly, Image.SCALE_SMOOTH);
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(newimage, 0, 0, this);
}
};
return mydesktop;
}