用PANEL
继承PANEL重写paintComponent方法,
这些控制添加在panel上,就不会出现你说的问题了。
重写paint,会出现你说的问题
直接在窗体上添加了背景图片也会出现你说的问题。
下边是例子。
------------------------------------------------------------------------------------
import javax.swing.ImageIcon;
public class App extends JFrame {
private JTextField textField;
public App() {
getContentPane().setLayout(new BorderLayout(0, 0));
JPanel panel = new ImagePanel();
getContentPane().add(panel);
panel.setLayout(null);
JButton btnNewButton = new JButton("New button");
btnNewButton.setBounds(108, 234, 93, 23);
panel.add(btnNewButton);
textField = new JTextField();
textField.setBounds(214, 176, 96, 19);
panel.add(textField);
textField.setColumns(10);
setSize(460, 350);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new App();
}
class ImagePanel extends JPanel {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
ImageIcon icon = new ImageIcon("D:\\1.jpg");
g.drawImage(icon.getImage(), 0, 0, null);
}
}
}
Image image=new ImageIcon("back.jpg").getImage();
//////
//覆盖JFrame的 paint方法
public void paint(Graphics g){
super.paint(g);
g.drawImage(image, 0,0, null);
}
我是刚想学习不好意思不知道解决