论坛首页 Java企业应用论坛

我也发个自制win7风格的swing

浏览 21065 次
精华帖 (1) :: 良好帖 (2) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-12-30  
你看一下这个
https://flamingo.dev.java.net/learn.html
0 请登录后投票
   发表时间:2010-12-30  
8错8错,希望swing越来越好。
0 请登录后投票
   发表时间:2010-12-30   最后修改:2010-12-30
不喜欢这种风格,
宽屏很普及了,还疯狂占用y轴的空间,
看看迅雷7的界面明显是适应宽屏的
0 请登录后投票
   发表时间:2010-12-30  
dtstudy 写道
depravedangel 写道
最近也在搞一些Swing的东西,但从开发效率、代码简易度、可维护性来看,跟C#的WinForm的差距还不是一点点,着实佩服微软的工具、设计就是好用

所以个人觉得,做界面,C#更优于Java,前台C#,后台Java的思路或许更值得同行去考虑

想法不错,但不太现实。呵呵

公司前台C# 后台Java的飘过
0 请登录后投票
   发表时间:2010-12-30  
網頁上貌似也是圖片+css弄出來的,swing用圖片+layout+event 做效果也什麽不行。
0 请登录后投票
   发表时间:2010-12-31  
devworks 写道
你看一下这个
https://flamingo.dev.java.net/learn.html

在XP下,演示所做的,很难看
0 请登录后投票
   发表时间:2010-12-31   最后修改:2010-12-31
从作者处提练的,画有水晶味道的按钮代码。供感兴趣的TX。附,这种方法,可用在panel,labtel之类的组件上。
效果图:

其中
1 有水晶味道的背景是用渐变函数进行绘制的(来自LZ的代码)
2 javaapplication2/save.png 是一个保存图标。最好是透明背景的PNG类型。这样效果更好。当然也是来自楼主的。
3 javaapplication2/MainJFrame.java 在下面。这是为了方便阅讯。

package javaapplication2;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.FontMetrics;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.Icon;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MainJFrame {
    private JButton jbutton;
    private JFrame jframe;
    private JPanel jpanel;
    private JButton bt ;
    public MainJFrame(){
        jframe = new JFrame();

        jbutton = new JButton(){
            private static final long serialVersionUID = 32323L;
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Icon pressedIcon = this.getPressedIcon();
                
                ImageIcon img = new ImageIcon(MainJFrame.class.getResource("save.png"));
//                img.paintIcon(this, g, 0, 0);
                //给水景背景,以渐背色填充
                Graphics2D   g2   =   (Graphics2D)g;
                Color c3=  new Color(255,206,105);
                Color c4=new Color(255,255,222);
                g2.setPaint(new  GradientPaint(2,2,new Color(253,236,219),1,this.getHeight() / 3,new Color(253,223,187)));
		g2.fillRect(2,2,this.getWidth() - 5,this.getHeight() / 3);
                g2.setPaint(new   GradientPaint(1,this.getHeight() / 3,c3,1,this.getHeight(),c4));
		g2.fillRect(2,this.getHeight() / 3,this.getWidth() - 5,this.getHeight()/3*2 - 1);
                //绘背景图标,这个最好是透明的PNG格式
                g.drawImage(img.getImage(),10,(getHeight() - img.getImage().getHeight(this))/2, this);
               //绘文字
                                String text=this.getText();
                if (text != null && !text.trim().equals("")) {
                     this.setFont(null);
                    FontMetrics fm = getFontMetrics(getFont());
                    int x = (getWidth() - fm.stringWidth(text)) / 2;
                    int y = (getHeight() - fm.getHeight()) / 2 + 11;
                    g.setColor(Color.RED);
                    g.drawString(text, x, y);
               }
            }
        };
        jbutton.setText("保存");
        jpanel = new JPanel();      

        init();
    }

    private void init(){
        jframe.setTitle("图片按钮测试");
        jframe.add(jpanel);
        jpanel.setLayout(new FlowLayout());
        jbutton.setOpaque(true);
        jbutton.setBorderPainted(true);
        jbutton.setPreferredSize(new Dimension(154, 154));
        jpanel.add(jbutton);
    }

    public void showMe(){
        jframe.setSize(300, 200);
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jframe.setVisible(true);
    }

    public static void main(String[] args) {
        new MainJFrame().showMe();
    }
}
  • 大小: 4.5 KB
0 请登录后投票
   发表时间:2010-12-31   最后修改:2010-12-31
这是来自网上的一个半透明按钮。由于按钮图片是用String,,在用Class.getResource方法得文件路径时,还需要小小的转换,故简单整理后的代码,放在LZ这个贴子里,供感兴趣的TX参考。由于自已选用的图片没美感,也就不贴效果图了。

其中文件结构如下:
   javaapplication1/LimpidButton.java   、javaapplication1/save.png、javaapplication1/2.jpg

原文网址
http://blog.163.com/sean_zwx/blog/static/16903275520106424617106/
package javaapplication1;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Composite;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Transparency;
import java.net.URISyntaxException;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;

import java.awt.Graphics;
import java.net.URI;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * @see 透明按钮
 * 其实方法很简单,首先定义一张半透明画布(透明度可以设置),然后将自己的按钮图片用
 * Graphics2D类画到该透明画布上,形成一张半透明的图片,再来就是将JButton的背景和边
 * 框全 * 部去掉,组后将这张半透明的图片画到按钮上即可
 */
public class LimpidButton extends JButton{
    Image img;
    MediaTracker mt;
    int w;
    int h;
    String imagePath;
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        try{
            img=new ImageIcon(imagePath).getImage(); //读取本地图片
            mt=new MediaTracker(this);//为此按钮添加媒体跟踪器
            mt.addImage(img,0);//在跟踪器添加图片,下标为0
            mt.waitForAll();   //等待加载
            w=img.getWidth(this);//读取图片长度
            h=img.getHeight(this);//读取图片宽度

            GraphicsConfiguration gc = new JFrame().getGraphicsConfiguration(); // 本地图形设备
            Image image = gc.createCompatibleImage(w,h,Transparency.TRANSLUCENT);//建立透明画布
            Graphics2D g2=(Graphics2D)image.getGraphics(); //在画布上创建画笔

            Composite alpha = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .5f); //指定透明度为半透明90%
            g2.setComposite(alpha);
            g2.drawImage(img,0,0,this); //注意是,将image画到g画笔所在的画布上
            //以下写字
            g2.setColor(Color.black);//设置颜色为黑色
            String text=this.getText();
            if (text != null && !text.trim().equals("")) {
                FontMetrics fm = getFontMetrics(getFont());
                int x = (getWidth() - fm.stringWidth(text)) / 2;
                int y = (getHeight() - fm.getHeight()) / 2 ;
                g.drawString(text, x, y);
            }
            g2.dispose(); //释放内存

            Composite alpha2 = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .7f);
            Image image1 =gc.createCompatibleImage(w,h,Transparency.TRANSLUCENT);
            g2=(Graphics2D)image1.getGraphics();
            g2.setComposite(alpha2);
            g2.drawImage(img,2,2,this); //改变图像起始位置,产生动态效果
            //以下写字
            g2.setColor(Color.black);
            if (text != null && !text.trim().equals("")) {
                FontMetrics fm = getFontMetrics(getFont());
                int x = (getWidth() - fm.stringWidth(text)) / 2;
                int y = (getHeight() - fm.getHeight()) / 2 ;
                g2.drawString(text, x, y);
            }
//            g.drawString(getText(),this.getWidth()/2, this.getHeight()/2);
            g2.dispose();


            this.setIgnoreRepaint(true);
            this.setFocusable(false);//设置没有焦点
            this.setBorder(null);//设置不画按钮边框
            this.setContentAreaFilled(false);//设置不画按钮背景
            this.setIcon(new ImageIcon(image1)); //把刚才生成的半透明image变成ImageIcon,贴到按钮上去
            this.setRolloverIcon(new ImageIcon(image1));
            this.setPressedIcon(new ImageIcon(image));//按下去的图标
        }catch(Exception e){
        }
    }


    public LimpidButton(String imagePath){
        this.imagePath=imagePath;         
    }

    public static void main(String[] args) throws URISyntaxException {
        JFrame frame=new JFrame();
        frame.setSize(300,300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //非得转换一下,不然打不开
        URI uri = new URI(LimpidButton.class.getResource("save.png").getPath().toString());
        String path=uri.getPath();
        LimpidButton button=new LimpidButton(path);
        button.setText("半透明");
        JPanel panel=new JPanel(){
            @Override
            public void paintComponent(Graphics g){
                //画窗口背景图
                super.paintComponent(g);
                g.drawImage(new ImageIcon(LimpidButton.class.getResource("2.jpg"))
                        .getImage(),-100, -100, null);
            }
        };
        panel.setLayout(null);
        panel.add(button);
        button.setBounds(100,100,100,100);
        frame.add(panel);
        frame.setVisible(true);
    }

}
0 请登录后投票
   发表时间:2010-12-31  
基于楼主厚道,方便了其他不了解Swing背景贴图、水晶按钮绘制的TX,我投了良好贴。特谢楼主。
0 请登录后投票
   发表时间:2010-12-31  
qianhd 写道
表侮辱swing了
这种贴图做的东西 根本就没体现swing精髓


你有更好的拿出来大家看看啊。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics