window下java如何去除label的背景色以达到透明效果
我在windows下用java的JLabel编写了个小小的程序用于动态显示时间,加了背景图片,JLabel也设置了透明为false,但是JLabel那老是有一片白色的区域挡住了背景图片,请问该如何去除啊,还有就是上面那个JLabel总是会消失。求高手给点指点。 java label 设置透明 --------------------编程问答-------------------- 不知道楼主怎么设置的,没代码,也不知道你哪里设置错了。貌似我的这个不会。。另外JLabel默认就是透明色的。。
效果图:
public class Test extends JPanel{
public Test() {
setBounds(300, 300, 400, 400);
}
public void paintComponent(Graphics g) {
Image img=null;
try {
img = ImageIO.read(new File("D:\\1.jpg"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
g.drawImage(img,0,0,null);
}
public static void main(String[] args) {
JFrame j=new JFrame();
j.setBounds(300, 300, 400, 400);
Test t=new Test();
t.add(new Show());
j.add(t);
j.show();
}
}
class Show extends JLabel{
public Show() {
this.setBounds(400, 400, 200, 200);
this.setFont(new java.awt.Font("Dialog", 1, 50));
this.setForeground(Color.red);
this.setText("好好好\n好好好");
}
@Override
public void paintComponents(Graphics arg0) {
// TODO Auto-generated method stub
super.paintComponents(arg0);
}
}
--------------------编程问答--------------------
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.*;
/**
*
* @author ljg
*/
public class TimeTest extends JFrame implements ActionListener {
private Timer timer;//
private Calendar calendar;//
private JLabel label1, label2;
private String str1, str2;
public static BufferedImage background;
static {
try {
background = ImageIO.read(TimeTest.class.getResource("bg.jpg"));
} catch (IOException ex) {
Logger.getLogger(TimeTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
TimeTest(){
super();
flesh();
timer.start();
setSize(400, 200);
setLocation(400, 300);
label1 = new JLabel();
// text1.setText("Text1");
label1.setText(str1);
label1.setSize(320, 30);
reSetFont(label1);
// label1.setOpaque(false);
// label1.setVisible(true);
label1.setLocation(0, 50);
label2 = new JLabel();
// text2.setText("Text2");
label2.setText(str2);
label2.setSize(300, 30);
label2.setLocation(0, 100);
reSetFont(label2);
// label2.setOpaque(false);
JPanel jpanel = new JPanel();
jpanel.setLayout(null);
add(jpanel);
jpanel.add(label1);
jpanel.add(label2);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
validate();
}
public void flesh() {
timer = new Timer(1000, this);
}
public void reSetFont(JLabel label) {
Font font = label.getFont();
font = new Font(font.getName(), Font.BOLD, 20);
label.setForeground(Color.RED);
// label.setBackground(new Color(240,240,240));
// label.setBorder(new MatterBorder(0,0,0,0,new Color(0,0,0)));
label.setOpaque(false);
label.setFont(font);
}
public void paint(Graphics g){
g.drawImage(background, 0, 0, null);
}
public void actionPerformed(ActionEvent e) {
//获取calendar对象
calendar = Calendar.getInstance();
//获得当前系统日期和时间
str1 = "当前日期: " + calendar.get(Calendar.YEAR) + " 年 " + calendar.get(Calendar.MONTH) + " 月 " + calendar.get(Calendar.DATE) + " 日 ";
str2 = "北京时间: " + calendar.get(Calendar.HOUR_OF_DAY) + " 时 " + calendar.get(Calendar.MINUTE) + " 分 " + calendar.get(Calendar.SECOND) + " 秒";
this.label1.setText(str1);
this.label2.setText(str2);
}
}
这是我的代码。麻烦帮我看看哪不对啊,多谢了! --------------------编程问答--------------------
对你装载两个JLabel的JPanel重写
public void paint(Graphics g){
g.drawImage(background, 0, 0, null);
}
方法,
不要对JFrame重写。。这样影响显示效果,就成了你显示的这样了。。
补充:Java , Java相关