把下面g.drawImage(image,0,0,this);和super.paint(g);两行代码位置互换,会出现文本域不能输入编辑,为什么?
public class BackgroundJTextArea extends JTextArea {
private Image image;
public BackgroundJTextArea(String path) {
ImageIcon imageIcon = new ImageIcon(path);
image = imageIcon.getImage();
setOpaque(false);
}
@Override
public void paint(Graphics g) {
g.drawImage(image, 0, 0, this);//这里
super.paint(g);//这里
}
}
///////
public class BackgroundJTextAreaTest extends JFrame {
private JPanel contentPane;
public static void main(String[] args) {
BackgroundJTextAreaTest frame = new BackgroundJTextAreaTest();
frame.setVisible(true);
}
public BackgroundJTextAreaTest() {
setTitle("为文本域设置背景图片");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JScrollPane scrollPane = new JScrollPane();
contentPane.add(scrollPane, BorderLayout.CENTER);
JTextArea textArea = new BackgroundJTextArea("src/b.jpg");
textArea.setLineWrap(true);
textArea.setFont(new Font("微软雅黑", Font.PLAIN, 16));
scrollPane.setViewportView(textArea);
}
}