最近在学习swing,分享下自己的成果——与setUndecorated相关的几个特效 例子1、渐变透明 this.setUndecorated(true); JPanel gradPanel = new JPanel() { Color colorA = new Color(255, 0, 0 , 0); // Solid red Color colorB = new Color(255, 0, 0, 255); protected void paintComponent(Graphics g) { Graphics2D g2d = (Graphics2D) g; GradientPaint gp; gp = new GradientPaint(0.0f, 0.0f, colorA, 0.0f, getHeight(), colorB, true); g2d.setPaint(gp); g2d.fillRect(0, 0, getWidth(), getHeight()); } }; gradPanel.setPreferredSize(new Dimension(300, 200)); gradPanel.setLayout(new BoxLayout(gradPanel, BoxLayout.Y_AXIS)); JButton btnClose = new JButton("Close"); ActionListener al = new ActionListener() { public void actionPerformed(ActionEvent ae) { System.exit(0); } }; btnClose.addActionListener(al); btnClose.setAlignmentX(0.5f); gradPanel.add(Box.createVerticalGlue()); gradPanel.add(btnClose); gradPanel.add(Box.createVerticalGlue()); setContentPane(gradPanel); if (!getGraphicsConfiguration().isTranslucencyCapable()) { System.err.println("per-pixel translucency not in effect for " + "this graphics configuration"); System.exit(0); } setBackground(new Color(0, 0, 0, 0)); pack(); setLocationRelativeTo(null); setVisible(true);
例子2、滑竿控制窗体透明 this.setUndecorated(true); this.setLocationRelativeTo(null); JSlider slider = new JSlider(40, 100, 100); ChangeListener cl = new ChangeListener() { public void stateChanged(ChangeEvent ce) { JSlider source = (JSlider) ce.getSource(); // 原来只要用这个方法来控制透明度的啊, setOpacity(source.getValue() / 100.0f); } }; slider.addChangeListener(cl); getContentPane().setLayout(new FlowLayout()); getContentPane().add(new JLabel("向透明")); getContentPane().add(new JPanel() { { add(slider); } }); getContentPane().add(new JLabel("向不透明")); getRootPane().setDoubleBuffered(false); pack(); setVisible(true);
例子3、颜色渐变的椭圆窗体 setUndecorated(true); // 去掉外面的边框修饰。 this.setSize(500,400); JPanel gradPanel = new JPanel() { Color colorA = new Color(255, 255, 255); Color colorB = new Color(255, 100, 200); protected void paintComponent(Graphics g) { Graphics2D g2d = (Graphics2D) g; GradientPaint gp; gp = new GradientPaint(0.0f, 0.0f, colorA, 0.0f, getHeight(), colorB, true); g2d.setPaint(gp); g2d.fillRect(0, 0, getWidth(), getHeight()); } }; gradPanel.setPreferredSize(new Dimension(300, 200)); gradPanel.setLayout(new BoxLayout(gradPanel, BoxLayout.Y_AXIS)); JButton btnClose = new JButton("Close"); ActionListener al; al = new ActionListener() { public void actionPerformed(ActionEvent ae) { System.exit(0); } }; btnClose.addActionListener(al); btnClose.setAlignmentX(0.5f); gradPanel.add(Box.createVerticalGlue()); gradPanel.add(btnClose); gradPanel.add(Box.createVerticalGlue()); setContentPane(gradPanel); pack(); Ellipse2D dd = new Ellipse2D.Double(0,0 , getWidth(), getHeight()); setShape(dd); setLocationRelativeTo(null); // 设置窗体位置为居中 setVisible(true);
例子4、自定义窗体边框(最小化,最大化,关闭等按钮和图标)
目前还没有做,不过原理知道。利用两个面板(JPanel),一个作为上面的边框面板,一个作为组件Component的载体,如果要能够移动窗体的话,那么就在边框面板添加鼠标事件,来控制窗体在桌面上的位置。 在这三个例子中,都必须setUndecorated(true);意思是去掉窗体外面的边框修饰,如果没有这限制的话,那么就会抛出异常。
对于这些装饰可能还不能达到真正想要的,毕竟这是有限的。如果想要真正修饰Component的话,可以用JDk7中的 JLayer。 对前三个是否支持还要看电脑环境是否支持故需要判断: GraphicsEnvironment ge; // 用来判断上下文环境是否支持的 ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); if (!ge.getDefaultScreenDevice().isWindowTranslucencySupported( GraphicsDevice.WindowTranslucency.PERPIXEL_TRANSPARENT)) { System.err.println("per-pixel transparency isn't " + "supported"); return; } new jframe(); jframe继承了JFrame
不论是哪个,反正都要 setUndecorated(true); // 去掉外面的边框修饰。