首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

已经设置了setOpaque如何还不透明

2013-11-18 
已经设置了setOpaque怎么还不透明?public class JFrameBackground extends JFrame {public JFrameBackgrou

已经设置了setOpaque怎么还不透明?


public class JFrameBackground extends JFrame {
public JFrameBackground() {
this.setTitle("我的swing界面");
JPanel panel = new JPanel();
panel.setBackground(null);
panel.setOpaque(false);
this.setSize(300, 300);
this.getContentPane().add(panel);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}

public static void main(String[] args) {
new JFrameBackground();
}
}

已经设置了setOpaque怎么还不透明? swing
[解决办法]

import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class JFrameBackground extends JFrame {
public JFrameBackground() {
this.setTitle("我的swing界面");
JLabel someText = new JLabel("透明的哦");
        GradientPanel gradientPanel = new GradientPanel();
        gradientPanel.add(someText);
        this.add(gradientPanel);

this.setSize(300, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}

public static void main(String[] args) {
new JFrameBackground();
}
}

class GradientPanel extends JPanel {

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        int w = getWidth(); 
        int h = getHeight();
        Graphics2D g2d = (Graphics2D) g;
        g2d.setPaint(new GradientPaint(0, 0, Color.RED, 0, h, Color.WHITE));
        g2d.fillRect(0, 0, w, h);
    }
}

热点排行
Bad Request.