终于写好了!一个全新的java Swing界面应用的源程序

终于写好了!一个全新的java Swing界面应用的源程序,第1张

这个关于java swing界面的程序是我以前写过的代码。现在只是更新了好多功能和改善了界面,使界面更加的友好,同时也使程序更加的稳定可靠,即使如此这个程序在运行时还是有缺陷。例如加载网络图片时,就显得有点卡,不知道是不是网速的原因,而且对网络图片的 *** 作也使得整个界面变得响应缓慢。如果你是个高手,我相信你完全可以自己去改善这个程序,最终使这个程序变得更完美,这个程序我还要在后续的工作中进行改善。其实java做界面,特别是本地客户端的应用软件,java完全表现失败。试想一下,一个用户为了运行一个java程序,不得不安装一个用户并不想安装的显得又大又笨拙的虚拟机!而且运行时它的速度有时候就像是狗爬!这可真是玩笑,但却是真的,怪不得java会在本地应用方面彻底败给C++。要是脱离了网络这个大环境java的发展或许根本就没有现在这么好!java现在好像也不是很春风得意,毕竟有那么多的语言在虎视眈眈,试图取代java的地位。java的母公司SUN被甲骨文收购,真的不知道是一件好事还是坏事。闲话不多说了!这个程序比较的大,有一千多行(其实也不大啦),我把源程序制成压缩包上传到CSDN上面,其中CutImg类是单独的一个文件,有兴趣的朋友可以自己去下,其中要注意的是剪切功能只能实现对本地图片,还不能实现对网络图片的剪切,java高手可以自己实现这个功能,下面给出转向下载页面的文字链接:

文字链接:http://download.csdn.net/download/zhulike2011/5524851

下面是其中的一些片段:

public void paint(Graphics g){   //小球画图的方法,利用Graphics类进行绘图
        g.setColor(pane.getBackground());  //这条语句用于擦除小球轨迹,让这条语句不起作用你就知道了
        g.fillOval(tempX, tempY, XSIZE, YSIZE);
        g.setColor(c);       
        g.fillOval(x, y, XSIZE, YSIZE);
        tempX=x;tempY=y;
         
    } 

public void move(Rectangle2D  g){   //这个方法用于控制小球运动,用于判断小球是否到达边界
        x += dx;
        y += dy;
        if(x<2){
           dx = -dx;
        }
        if(x+XSIZE>retangleX){
            dx = -dx;
        }
        if(y<g.getMinY()+YSIZE){  
            dy =  -dy;
        }
        if(y+YSIZE+40>=retangleY){            
           dy = -dy;
        }
    }
/**
     * 剪切板中是否有文本数据可供粘贴
     *  
     * @return true为有文本数据
     */  
    public boolean isClipboardString() {  
        boolean b = false;  
        Clipboard clipboard = this.getToolkit().getSystemClipboard();  
        Transferable content = clipboard.getContents(this);  
        try {  
            if (content.getTransferData(DataFlavor.stringFlavor) instanceof String) {  
                b = true;  
            }  
        } catch (Exception e) {  
        }  
        return b;  
    } 

//这个方法用于读取图片

try {
            img=ImageIO.read(
                    new File("D:\\My Documents\\My Pictures\\SB.gif" ));//不能显示动态,只是静态的
            img=img.getScaledInstance(width, height, Image.SCALE_DEFAULT);
            beiJing= new ImageIcon(img );
            
        } catch (IOException e) {        
            e.printStackTrace();
        }

下面是构造器方法,如果你能看懂这个构造器的作用,那这整个程序,你就可以写出来了!

public TestDemo(){
        
        cot=this.getContentPane();
        
        setSize(700,500);
        pane =new BeiJingPane(this.getWidth(),this.getHeight());
        panBall=new XiaoQiuPane(this.getWidth(),this.getHeight());
        clearityPane=new JPanel();
        clearityPane.setVisible(true);   
        clearityPane.setOpaque(false);  //透明效果
        clearityPane.setBounds(new Rectangle(700,450));
        panBall.setLayout(new BorderLayout());
        buttonBallPane.add(changeBackColor);
        buttonBallPane.add(changeBallColor);
        buttonBallPane.add(start_ball);
        buttonBall.setLayout(new BorderLayout());
        buttonBall.setBackground(Color.red);
        buttonBall.add(buttonBallPane, BorderLayout.SOUTH);
        
        label.setSize(100, 150);
        
        
        
        ball=new BallDemo(this.getSize().width-20,this.getSize().height-30,buttonBall);
        
        
        
        clearityPane.add(bbb);
        pane.setLayout(new BorderLayout());       
        bbb.setVisible(false);
        
        panBall.add(clearityPane,BorderLayout.CENTER);  //添加透明面板       
        buttonpane.add(changePicture);
        buttonpane.add(changeColor);
        buttonpane.add(start_retangle);
        
        
        panBall.add(buttonpane, BorderLayout.SOUTH);
        
        
        
        //cot.add(pane);
        panButton.add(bt);
        panButton.add(bt0);
        panButton.add(bt1);
        panButton.add(bt2);
        panButton.add(bt3);
        panButton.add(bt4);
     
        menuBar.add(menuOpen);
        menuBar.add(menuNative);
        menuBar.add(menuNet);
        menuBar.add(menuQuit);
        menuBar.add(menuSave);
        pane.add(panButton,BorderLayout.SOUTH);
        pane.add(menuBar,BorderLayout.NORTH);       
        pane.add(label,BorderLayout.CENTER);
        
        
        
        
        
        changePicture.addActionListener(this);
        changeColor.addActionListener(this);
        changeBallColor.addActionListener(this);
        changeBackColor.addActionListener(this);
        start_retangle.addActionListener(this);  //给矩形小球启动按钮注册监听
        start_ball.addActionListener(this);      //给小球启动按钮注册监听
        
        pane.addMouseListener(this);
        
        bt0.addActionListener(this);
        bt.addActionListener(this);
        bt1.addActionListener(this);
        bt2.addActionListener(this);
        bt3.addActionListener(this);
        bt4.addActionListener(this);
        label.addMouseListener(this);//给图片注册监听
        menuSave.addMouseListener(this);
        menuOpen.addMouseListener(this);       
        menuNative.addMouseListener(this);
        menuNet.addMouseListener(this);
        menuQuit.addMouseListener(this);
        
        
        
        jtp.addChangeListener(this); //给选项卡注册变化接口监听
        jtp.add("默认面板",pane);
        jtp.add("矩形小球运动", panBall);
        jtp.add("小球运动", buttonBall);
        cot.add(jtp);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setVisible(true);    
    }     

下面是效果图:

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/2081902.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-07-22
下一篇 2022-07-22

发表评论

登录后才能评论

评论列表(0条)

保存