在java 的frame中如何设置背景图片
2个回答
展开全部
如果认可代码,请给加分50,谢谢!
import javax.swing.*;
import java.awt.*;
final public class ImageBackground extends JFrame {
private final String imgePath = "1x.png";
private JPanel pane;
public ImageBackground(String title) throws HeadlessException {
super(title);
init();
setSize(100,150);
setVisible(true);
}
private void init() {
final Image img = Toolkit.getDefaultToolkit().createImage(imgePath);
pane = new JPanel(){
protected void paintChildren(Graphics g) {
g.drawImage(img,0,0,this);
super.paintChildren(g);
}
};
pane.add(new JButton("adfasdfasdf"));
JLabel label = new JLabel("red text");
label.setForeground(Color.RED);
pane.add(label);
getContentPane().add(pane,BorderLayout.CENTER);
}
public static void main(String[] args) {new ImageBackground("image background");}
}
import javax.swing.*;
import java.awt.*;
final public class ImageBackground extends JFrame {
private final String imgePath = "1x.png";
private JPanel pane;
public ImageBackground(String title) throws HeadlessException {
super(title);
init();
setSize(100,150);
setVisible(true);
}
private void init() {
final Image img = Toolkit.getDefaultToolkit().createImage(imgePath);
pane = new JPanel(){
protected void paintChildren(Graphics g) {
g.drawImage(img,0,0,this);
super.paintChildren(g);
}
};
pane.add(new JButton("adfasdfasdf"));
JLabel label = new JLabel("red text");
label.setForeground(Color.RED);
pane.add(label);
getContentPane().add(pane,BorderLayout.CENTER);
}
public static void main(String[] args) {new ImageBackground("image background");}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询