添加背景图片为什么setlayout(null)后就会覆盖按钮,要用流式布局才不会覆盖哦?
packagetupian;importjava.awt.Dimension;importjava.awt.FlowLayout;importjava.awt.Graph...
package tupian;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.print.DocFlavor.URL;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class beijing2 extends JFrame {
JButton jb=new JButton("yundfj");
JButton jb1=new JButton("dsfskdjf");
JLabel jl=new JLabel("dsfdsfksdfsj");
beijing2(){
this.setContentPane(new MyPanel());
setBounds(300, 300, 300, 300);
jb.setBounds(300, 300, 100, 50);
jb1.setBounds(400, 400, 100, 50);
add(jl);
add(jb1);
add(jb);
setLayout(null);
// setLayout(new FlowLayout());
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new beijing2();
}
private class MyPanel extends JPanel {
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
super.paintComponent(g);
Image img = Toolkit.getDefaultToolkit().getImage("httpi.jpg");
g2.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);
}
}
}这样图片会覆盖按钮的,要把setLayout(null);改成setLayout(new FlowLayout());
才行哦,怎么样改采能够自由设置按钮的位置又不会被图片覆盖哦?那位高手帮帮小弟哦,谢谢哦 展开
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.print.DocFlavor.URL;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class beijing2 extends JFrame {
JButton jb=new JButton("yundfj");
JButton jb1=new JButton("dsfskdjf");
JLabel jl=new JLabel("dsfdsfksdfsj");
beijing2(){
this.setContentPane(new MyPanel());
setBounds(300, 300, 300, 300);
jb.setBounds(300, 300, 100, 50);
jb1.setBounds(400, 400, 100, 50);
add(jl);
add(jb1);
add(jb);
setLayout(null);
// setLayout(new FlowLayout());
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new beijing2();
}
private class MyPanel extends JPanel {
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
super.paintComponent(g);
Image img = Toolkit.getDefaultToolkit().getImage("httpi.jpg");
g2.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);
}
}
}这样图片会覆盖按钮的,要把setLayout(null);改成setLayout(new FlowLayout());
才行哦,怎么样改采能够自由设置按钮的位置又不会被图片覆盖哦?那位高手帮帮小弟哦,谢谢哦 展开
1个回答
展开全部
public class beijing2 extends JFrame {
....
.......
beijing2(){
this.setContentPane(new MyPanel());//调用MyPanel
........
..............
}
public static void main(String[] args) {
new beijing2();
}
private class MyPanel extends JPanel {
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
super.paintComponent(g);
Image img = Toolkit.getDefaultToolkit().getImage("httpi.jpg");
g2.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);
}
}
}
把整个图片加在标签上,然后把标签的大小和窗口一样也可以充当背景图片//如果不能显示jpg的图片就把jpg图片转化为png格式的
....
.......
beijing2(){
this.setContentPane(new MyPanel());//调用MyPanel
........
..............
}
public static void main(String[] args) {
new beijing2();
}
private class MyPanel extends JPanel {
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
super.paintComponent(g);
Image img = Toolkit.getDefaultToolkit().getImage("httpi.jpg");
g2.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);
}
}
}
把整个图片加在标签上,然后把标签的大小和窗口一样也可以充当背景图片//如果不能显示jpg的图片就把jpg图片转化为png格式的
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询