1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
| import java.io.*; import java.awt.*; import javax.imageio.*; import java.awt.event.*; import javax.swing.*; import java.awt.geom.Ellipse2D;
public class NonRegularWindow extends JFrame implements ActionListener {
private static final long serialVersionUID = 8373752799905574018L; JFrame transWin = new JFrame("透明窗口"); JFrame gradientWin = new JFrame("渐变透明窗口"); JFrame bgWin = new JFrame("背景图片窗口"); JFrame shapeWin = new JFrame("椭圆窗口");
public NonRegularWindow() { super("不规则窗口测试"); setLayout(new FlowLayout()); JButton transBn = new JButton("透明窗口"); JButton gradientBn = new JButton("渐变透明窗口"); JButton bgBn = new JButton("背景图片窗口"); JButton shapeBn = new JButton("椭圆窗口"); transBn.addActionListener(this); gradientBn.addActionListener(this); bgBn.addActionListener(this); shapeBn.addActionListener(this); add(transBn); add(gradientBn); add(bgBn); add(shapeBn); transWin.setLayout(new GridBagLayout()); transWin.setSize(300, 200); transWin.add(new JButton("透明窗口里的简单按钮")); transWin.setOpacity(0.65f); gradientWin.setBackground(new Color(0, 0, 0, 0)); gradientWin.setSize(new Dimension(300, 200)); JPanel panel = new JPanel() { private static final long serialVersionUID = -3046929062708838980L;
protected void paintComponent(Graphics g) { if (g instanceof Graphics2D) { final int R = 240; final int G = 240; final int B = 240; Paint p = new GradientPaint(0.0f, 0.0f, new Color(R, G, B, 0), 0.0f, getHeight(), new Color(R, G, B, 255), true); Graphics2D g2d = (Graphics2D) g; g2d.setPaint(p); g2d.fillRect(0, 0, getWidth(), getHeight()); } } }; gradientWin.setContentPane(panel); panel.setLayout(new GridBagLayout()); gradientWin.add(new JButton("渐变透明窗口里的简单按钮")); bgWin.setBackground(new Color(0, 0, 0, 0)); bgWin.setSize(new Dimension(300, 200)); JPanel bgPanel = new JPanel() { private static final long serialVersionUID = -4650113493486706945L;
protected void paintComponent(Graphics g) { try { Image bg = ImageIO.read(new File("images/java.png")); g.drawImage(bg, 0, 0, getWidth(), getHeight(), null); } catch (IOException ex) { ex.printStackTrace(); } } }; bgWin.setContentPane(bgPanel); bgPanel.setLayout(new GridBagLayout()); bgWin.add(new JButton("有背景图片窗口里的简单按钮")); shapeWin.setLayout(new GridBagLayout()); shapeWin.setUndecorated(true); shapeWin.setOpacity(0.7f); shapeWin.addComponentListener(new ComponentAdapter() { public void componentResized(ComponentEvent e) { shapeWin.setShape(new Ellipse2D.Double(0, 0, shapeWin.getWidth(), shapeWin.getHeight())); } }); shapeWin.setSize(300, 200); shapeWin.add(new JButton("椭圆形窗口里的简单按钮")); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); setVisible(true); }
public void actionPerformed(ActionEvent event) { switch (event.getActionCommand()) { case "透明窗口": transWin.setVisible(true); break; case "渐变透明窗口": gradientWin.setVisible(true); break; case "背景图片窗口": bgWin.setVisible(true); break; case "椭圆窗口": shapeWin.setVisible(true); break; } }
public static void main(String[] args) { JFrame.setDefaultLookAndFeelDecorated(true); new NonRegularWindow(); } }
|