我这里刚好有个例子
ImageIcon img = new ImageIcon("f:/library.jpg")//这是背景图片JLabel imgLabel = new JLabel(img)//将背景图放在标签里。
this.getLayeredPane().add(imgLabel, new Integer(Integer.MIN_VALUE))//注意这里是关键,将背景标签添加到jfram的LayeredPane面板里。
imgLabel.setBounds(0,0,img.getIconWidth(), img.getIconHeight())//设置背景标签的位置
java编写的一个表格,JTable对象加在JPanel面板上,JPanel加在JFrame上。设置背景色
public class DrawFrame {public static void main(String[] args) {
final JFrame frame = new JFrame()
frame.getContentPane().setBackground(Color.RED)
JTable table = new JTable(4, 4) {
@Override
public void paint(Graphics g) {
super.paint(g)
Graphics2D g2d = (Graphics2D) g
Composite com = g2d.getComposite()
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.1f))
g2d.setColor(Color.RED)
g2d.fill(new Rectangle(0, 0, this.getWidth(), getHeight()))
g2d.setComposite(com)
g2d.setColor(Color.BLACK)
}
}
frame.getContentPane().add(table, "Center")
frame.setVisible(true)
}
}
Table上有一层半透明的红色层
图片也是画上去,你可以试试
import java.awt.*import java.awt.image.*
import javax.swing.*
import javax.imageio.ImageIO
public class LoginExample {
public static void main(final String... args) {
EventQueue.invokeLater(new Runnable(){
@Override public void run(){
try {
final XPanel container = new XPanel(ImageIO.read(LoginExample.class.getResource("/images/background.jpg")))
final JPanel panel = new JPanel()
final GroupLayout layout = new GroupLayout(panel)
container.setLayout(new BorderLayout())
panel.setLayout(layout)
panel.setOpaque(false)
layout.setAutoCreateGaps(true)
layout.setAutoCreateContainerGaps(true)
final JLabel name = new JLabel("用户名")
final JLabel pass = new JLabel("密码")
final JTextField name_field = new JTextField(12)
final JPasswordField pass_field = new JPasswordField(12)
name_field.setOpaque(false)
pass_field.setOpaque(false)
GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup()
hGroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING).
addComponent(name).addComponent(pass))
hGroup.addGroup(layout.createParallelGroup().
addComponent(name_field).addComponent(pass_field))
layout.setHorizontalGroup(hGroup)
GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup()
vGroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE).
addComponent(name).addComponent(name_field))
vGroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE).
addComponent(pass).addComponent(pass_field))
layout.setVerticalGroup(vGroup)
final JFrame frame = new JFrame("Login")
container.add(new JLabel("**欢迎您登录**", JLabel.CENTER), BorderLayout.PAGE_START)
container.add(panel)
frame.setContentPane(container)
frame.pack()
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
frame.setVisible(true)
} catch (Exception e) {
}
}
})
}
public static class XPanel extends JPanel {
private BufferedImage background
public XPanel(BufferedImage background) {
this.background = background
}
@Override public void paintComponent(Graphics g) {
BufferedImage bufimg = new BufferedImage(getWidth(),getHeight(), BufferedImage.TYPE_INT_ARGB)
Graphics2D g2 = bufimg.createGraphics()
// restore the foreground color in case the superclass needs it
g2.setColor(g.getColor())
super.paintComponent(g2)
// do an alpha composite
Graphics2D gx = (Graphics2D) g
gx.drawImage(background,0,0,null)
gx.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,0.8f))
gx.drawImage(bufimg,0,0,null)
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)