日期:2014-05-20 浏览次数:20848 次
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();
}
}
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);
}
}