免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3426 | 回复: 1
打印 上一主题 下一主题

哈哈,没事给button加个gif图片,见笑了 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-05-19 22:12 |只看该作者 |倒序浏览
//import java.awt.BorderLayout;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.URL;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.filechooser.FileFilter;

/*
* Created on 2005-4-6
*
* To change the template for this generated file go to
* Window&ampreferences&Java&Code Generation&Code and Comments
*/

/**
* @author Administrator
*
* To change the template for this generated type comment go to
* Window&ampreferences&Java&Code Generation&Code and Comments
*/
public class Texttest
{
        public static void main(String[] args)
        {
                TextFrame frame = new TextFrame();
                //frame.pack();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.show();
        }
}

class TextFrame extends JFrame
{
        public TextFrame()
        {
                setTitle("TextTest";
                setSize(400, 300);

                JPanel panel = new JPanel();

                text = new JTextArea(30,56);
                text.setLineWrap(true);
                JScrollPane scrollPane = new JScrollPane(text);

                Container content = getContentPane();
                content.add(panel,BorderLayout.NORTH);
                content.add(scrollPane,BorderLayout.CENTER);

                initMenuBar();
                initToolBar();
               
                Image image = getFrameIconifiedImage("server.gif";
                if (image != null)
                        setIconImage(image);    // doesn't change dialog icon

                pack();
        }

        public void setText(String file)
        {
                String line = null;
                String textcontent = null;
                try
                {
                        BufferedReader br = new BufferedReader(new FileReader(file));
                        while ((line = br.readLine()) != null)
                        {
                                text.append(line + "\n";
                        }

                        br.close();
                }
                catch (Exception e)
                {
                        e.printStackTrace();
                }               
        }

        class SysAction implements java.awt.event.ActionListener
        {
                /* (non-Javadoc)
                 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
                 */
                public void actionPerformed(ActionEvent e)
                {
                        // TODO Auto-generated method stub
                        Object obj = e.getSource();
                        if ( openbtn == obj )
                        {
                                openFile();
                        }
                        else if ( clearbtn == obj )
                        {
                                closeFile();
                        }
                        else if ( saveasbtn == obj )
                        {
                                saveFile();
                        }
                }
        }

        class MenuAction implements ActionListener
        {

                /* (non-Javadoc)
                 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
                 */
                public void actionPerformed(ActionEvent e)
                {
                        // TODO Auto-generated method stub
                        Object obj = e.getSource();

                        if ( obj == openmenu )
                        {
                                openFile();
                        }
                        else if ( obj == clearmenu )
                        {
                                closeFile();
                        }
                        else if ( obj == saveasmenu )
                        {
                                saveFile();
                        }
                }       
        }

        public void openFile()
        {
                //int ret = chooser.showOpenDialog(TextFrame.this);
                //System.out.println(ret);

                chooser.setCurrentDirectory(new File(System.getProperty("user.dir" + File.separator + "cfgsavset");
                //ExtensionFileFilter filter = new ExtensionFileFilter(".txt","opentextfile";
                //chooser.addChoosableFileFilter(filter);
                int ret = chooser.showOpenDialog(TextFrame.this);
                if (ret != JFileChooser.APPROVE_OPTION)
                {
                        // cancel.
                        System.out.println("Failed!";
                }

                String dir = chooser.getCurrentDirectory().getPath();
                System.out.println(dir);
                String fileName = chooser.getSelectedFile().getName();
                System.out.println(fileName);
                System.out.println(dir + fileName);
                String finalfile = dir + "\\" + fileName;
                System.out.println(finalfile);

                setText(finalfile);
                /*
                int i = fileName.lastIndexOf('.');
                if ( i == -1 ) {
                        // append .cfg
                        fileName += ".txt"; //$NON-NLS-1$
                } else if ((i+1) == fileName.length() ) {
                        // append cfg (dot is already there)
                        fileName += ".txt"; //$NON-NLS-1$
                }
                */
        }

        public void closeFile()
        {
                text.setText("";
        }

        public void saveFile()
        {
                text.getText();
                //String dir = System.getProperty("user.dir";
                int ret = chooser.showSaveDialog(TextFrame.this);
                if (ret != JFileChooser.APPROVE_OPTION)
                {
                        // cancel.
                        System.out.println("Failed!";
                }
                String dir = chooser.getCurrentDirectory().getPath();
                String fileName = chooser.getSelectedFile().getName();

                String outfile = dir + "\\" + fileName;
                System.out.println(outfile);
                try
                {
                        RandomAccessFile of = new RandomAccessFile(outfile, "rw");
                        of.writeBytes(text.getText());
                        of.close();
                }
                catch(IOException e)
                {
                        e.printStackTrace();
                }
        }

        private void initMenuBar()
        {
                MenuAction amenuaction = new MenuAction();
                filemenu.setText("File");
                filemenu.setMnemonic('F');

                mainmenubar.add(filemenu);
                openmenu.setText("Open");
                openmenu.setMnemonic('O');
                openmenu.addActionListener(amenuaction);
                filemenu.add(openmenu);

                saveasmenu.setText("Save As");
                saveasmenu.setMnemonic('S');
                saveasmenu.addActionListener(amenuaction);
                filemenu.add(saveasmenu);

                clearmenu.setText("Clear");
                clearmenu.setMnemonic('C');
                clearmenu.addActionListener(amenuaction);
                filemenu.add(clearmenu);

                exitmenu.setText("Exit");
                exitmenu.setMnemonic('E');
                exitmenu.addActionListener(amenuaction);
                filemenu.add(exitmenu);

                setJMenuBar(mainmenubar);
        }

        private void initToolBar()
        {
                toolbar.setFloatable(false);
                getContentPane().add(BorderLayout.NORTH, toolbar);
                SysAction aSysAction = new SysAction();
                openbtn.setToolTipText("Open");
                openbtn.addActionListener(aSysAction);
                openbtn.setIcon(new javax.swing.ImageIcon(getURLImagePath("print.gif")));
                toolbar.add(openbtn);
                clearbtn.setToolTipText("Clear");
                clearbtn.addActionListener(aSysAction);
                toolbar.add(clearbtn);
                saveasbtn.setToolTipText("Save As");
                saveasbtn.addActionListener(aSysAction);
                toolbar.add(saveasbtn);
        }

        /* if you need, you can use ToolKit to load this images. */
   public static Image getFrameIconifiedImage(String filename)
   {
           Toolkit toolKit = Toolkit.getDefaultToolkit();
           return toolKit.getImage(getURLImagePath(filename));
   }
   private static Class muteClass = null;
   static
   {
           try
           {
                           muteClass = Class.forName("Texttest");//(new rsimage()).getClass(); //$NON-NLS-1$
           }
           catch (ClassNotFoundException e)
           {
                   muteClass = null;
           }
   }
   public static URL getURLImagePath(String filename)
   {
           URL Location = muteClass.getResource("/images/" + "/" + filename);//("images/" + filename); //$NON-NLS-1$
           if (Location == null)
           {
                   String err = "Can not found file: " + filename; //$NON-NLS-1$
                      //new rsexception(err);
                   System.out.println(err);
           }
           return(Location);
   }
   
        javax.swing.JMenuBar mainmenubar = new  javax.swing.JMenuBar();
        javax.swing.JMenu filemenu = new javax.swing.JMenu();
        javax.swing.JMenuItem openmenu = new javax.swing.JMenuItem();
        javax.swing.JMenuItem clearmenu = new javax.swing.JMenuItem();
        javax.swing.JMenuItem saveasmenu = new javax.swing.JMenuItem();
        javax.swing.JMenuItem exitmenu = new javax.swing.JMenuItem();

        javax.swing.JToolBar toolbar = new javax.swing.JToolBar();
        javax.swing.JToolBar.Separator separator = new javax.swing.JToolBar.Separator();

        private JButton openbtn = new JButton();
        private JButton clearbtn = new JButton();
        private JButton saveasbtn = new JButton();
        private JFileChooser chooser = new JFileChooser();
        private JTextArea text;
}

class ExtensionFileFilter extends FileFilter
{
        String Extension;
        String Description;

        /* the construct accept some ext such cfg, txt*/  
        public ExtensionFileFilter(String Ext, String Descript)
        {
                Extension = Ext;
                Description = Descript;
                System.out.println(Extension + "  "        + Description);
        }

        public boolean accept(File f)
        {
                boolean acceptit = f.isDirectory();

                if (!acceptit) {
                        // is file
                        String suffix = getSuffix(f);
                        if (suffix != null) {
                                acceptit = suffix.equalsIgnoreCase(Extension);
                        }
                }

                return(acceptit);
        }

        public String getDescription()
        {
                return Description;
        }

        private String getSuffix(File f)
        {
                String s = f.getPath();
                String suffix = null;
                int i = s.lastIndexOf('.');

                if (i >; 0 && i < s.length() - 1) {
                        suffix = s.substring(i + 1);
                }
                return suffix;
        }
}

论坛徽章:
0
2 [报告]
发表于 2005-05-20 21:56 |只看该作者

哈哈,没事给button加个gif图片,见笑了

jbutton.setText("<html>;<body>;<img src='fileName'>;</body>;</html>;";
也是可以的,只是fileName需要通过getResource获得.
JLabel、JButton都可以这样用的。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP