//背景随滚动而移动位置 |
import java.awt.Graphics; |
import javax.swing.ImageIcon; |
import javax.swing.JFrame; |
import javax.swing.JScrollPane; |
import javax.swing.JTextArea; |
/** |
* 在JTextArea中显示一个图片背景(背景随滚动而移动位置) |
* |
* @author 五斗米 <如转载请保留作者和出处> |
* @blog <a href="http://blog.csdn.net/mq612">http://blog.csdn.net/mq612 |
*/ |
public class Test extends JFrame { |
private static final long serialVersionUID = 4785452373598819719L; |
private JScrollPane sp = null ; |
private JTextArea text = null ; |
private ImageIcon imageIcon = null ; |
public Test() { |
super ( "JTextArea" ); |
imageIcon = new ImageIcon( "photo.jpg" ); |
text = new JTextArea() { |
private static final long serialVersionUID = -8220994963464909915L; |
{ |
setOpaque( false ); // 设置透明 |
} |
protected void paintComponent(Graphics g) { |
g.drawImage(imageIcon.getImage(), 0 , 0 , this ); |
super .paintComponent(g); |
} |
}; |
sp = new JScrollPane(text); |
this .getContentPane().add(sp); |
this .setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
this .setSize( 360 , 260 ); |
this .setVisible( true ); |
} |
public static void main(String args[]) { |
new Test(); |
} |
} |
//背景不随滚动而移动位置 |
import java.awt.BorderLayout; |
import java.awt.Graphics; |
import javax.swing.ImageIcon; |
import javax.swing.JFrame; |
import javax.swing.JPanel; |
import javax.swing.JScrollPane; |
import javax.swing.JTextArea; |
/** |
* 在JTextArea中显示一个图片背景(背景不随滚动而移动位置) |
* |
* @author 五斗米 <如转载请保留作者和出处> |
* @blog <a href="http://blog.csdn.net/mq612">http://blog.csdn.net/mq612 |
*/ |
public class Test extends JFrame { |
private static final long serialVersionUID = 4785452373598819719L; |
private JScrollPane sp = null ; |
private JTextArea text = null ; |
private ImageIcon imageIcon = null ; |
public Test() { |
super ( "JTextArea" ); |
imageIcon = new ImageIcon( "photo.jpg" ); |
// 构造文本组件并使之透明 |
text = new JTextArea(); |
text.setOpaque( false ); |
// 构造滚动组件并使之透明 |
sp = new JScrollPane(text); |
sp.setOpaque( false ); |
sp.getViewport().setOpaque( false ); |
// 构造一个背景JPanel |
JPanel backdrop = new JPanel() { |
private static final long serialVersionUID = 1957203784117943458L; |
{ |
this .setOpaque( false ); |
this .setLayout( new BorderLayout()); |
} |
public void paintComponent(Graphics g) { |
g.drawImage(imageIcon.getImage(), 0 , 0 , this ); |
super .paintComponents(g); |
} |
}; |
// 将滚动组件加入 |
backdrop.add(sp); |
// 将背景组件加入窗体 |
this .getContentPane().add(backdrop); |
this .setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
this .setSize( 360 , 260 ); |
this .setVisible( true ); |
} |
public static void main(String args[]) { |
new Test(); |
} |
} |
//源代码片段来自云代码http://yuncode.net |
|