Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java程序分享区 » 工具  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
reply to postflat modethreaded modego to previous topicgo to next topicgo to back
话题被移动
该话题已被移动 - tzutolin , 2005-12-13 14:12
如果您尚不清楚该话题被移动的原因,请参考论坛规则以及本版公告或者联系本版版主。
作者 Re:color4j [Re:Jove]
freeman_z





发贴: 8
于 2004-03-25 04:30 user profilesend a private message to userreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
001 package jove.color4j;
002 
003 import java.awt.*;
004 import java.awt.datatransfer.*;
005 import java.awt.event.*;
006 
007 import javax.swing.*;
008 import javax.swing.event.*;
009 
010 import snoozesoft.systray4j.*;
011 
012 /**
013  * @author Jove
014  */
015 public class Color4jGUI extends JFrame implements SysTrayMenuListener {
016 
017     public static void main(String[] args) {
018         /*
019         使用exe4j来保证单实例
020         File f = new File("file.lock");
021         if (f.exists()) {
022           showMessage("Warning: A instance has existed.");
023           System.exit(0);
024         }
025         try {
026           f.createNewFile();
027         } catch (IOException e) {
028           e.printStackTrace();
029           System.exit(1);
030         }
031         f.deleteOnExit();
032         */
033         try {
034             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
035         } catch (Exception e) {}
036 
037         new Color4jGUI().show();
038     }
039 
040     private static void showMessage(String msg) {
041         JFrame dummy = new JFrame("Color4j");
042         dummy.setLocation(400, 300);
043         dummy.show();
044         JOptionPane.showMessageDialog(dummy, msg);
045         dummy.dispose();
046     }
047     private CheckableMenuItem checkMenuUbb, checkMenuAnsi;
048     private Color4j color4j;
049     private SysTrayMenuIcon icon;
050     private Color[] labelColor = { Color.RED, Color.BLUE };
051 
052     private String[] labelText =
053         {
054             "  Press F9 to convert and save result to clipboard",
055             "  Press F8 to read data from clipboard and convert" };
056     private boolean labelTextFlag = true;
057     private SysTrayMenu menu;
058     private JRadioButton radioUbb, radioAnsi;
059 
060     private Color4jGUI() {
061         super("Java Code Formatter");
062         setBounds(100, 100, 500, 400);
063         addWindowListener(new WindowListener() {
064             public void windowActivated(WindowEvent e) {}
065             public void windowClosed(WindowEvent e) {}
066             public void windowClosing(WindowEvent e) {
067                 //hide();
068                 dispose();
069                 System.exit(0);
070             }
071             public void windowDeactivated(WindowEvent e) {}
072             public void windowDeiconified(WindowEvent e) {}
073             public void windowIconified(WindowEvent e) {
074                 hide();
075             }
076             public void windowOpened(WindowEvent e) {}
077         });
078 
079         Container con = getContentPane();
080         Box box = Box.createHorizontalBox();
081         final JLabel label = new JLabel(labelText[0]);
082         label.setForeground(labelColor[0]);
083         box.add(label);
084         Timer timer = new Timer(5000, new ActionListener() {
085             public void actionPerformed(ActionEvent e) {
086                 if (labelTextFlag) {
087                     label.setText(labelText[1]);
088                     label.setForeground(labelColor[1]);
089                 } else {
090                     label.setText(labelText[0]);
091                     label.setForeground(labelColor[0]);
092                 }
093                 labelTextFlag = !labelTextFlag;
094             }
095         });
096         timer.start();
097         box.add(Box.createHorizontalGlue());
098         radioUbb = new JRadioButton("UBB"true);
099         radioAnsi = new JRadioButton("ANSI");
100         ChangeListener cListener = new ChangeListener() {
101             public void stateChanged(ChangeEvent e) {
102                 boolean isUbb = radioUbb.isSelected();
103                 checkMenuUbb.setState(isUbb);
104                 checkMenuAnsi.setState(!isUbb);
105             }
106         };
107         radioUbb.addChangeListener(cListener);
108         radioAnsi.addChangeListener(cListener);
109         box.add(radioUbb);
110         box.add(radioAnsi);
111         box.add(Box.createHorizontalStrut(5));
112         ButtonGroup group = new ButtonGroup();
113         group.add(radioUbb);
114         group.add(radioAnsi);
115         con.add(box, BorderLayout.SOUTH);
116         final JTextArea textSource = new JTextArea();
117         JScrollPane sp = new JScrollPane(textSource);
118         JPanel p = new JPanel();
119         p.setLayout(new BorderLayout());
120         p.setBorder(BorderFactory.createTitledBorder("Source Code"));
121         p.add(sp);
122         con.add(p);
123         color4j = new Color4j();
124         KeyListener listener = new KeyAdapter() {
125             public void keyPressed(KeyEvent e) {
126                 if (e.getKeyCode() == KeyEvent.VK_F9) {
127                     convert(textSource.getText());
128                 } else if (e.getKeyCode() == KeyEvent.VK_F8) {
129                     convertFromCB();
130                 }
131             }
132         };
133         textSource.addKeyListener(listener);
134         radioAnsi.addKeyListener(listener);
135         radioUbb.addKeyListener(listener);
136 
137         icon = new SysTrayMenuIcon(getClass().getResource("icon.ico"));
138         icon.addSysTrayMenuListener(this);
139         createMenu();
140     }
141 
142     private void convert(String code) {
143       String oldTitle=getTitle();
144       setTitle(oldTitle+" <converting>");
145         if (radioUbb.isSelected()) {
146             color4j.setStyleSet(StyleSet.UBB);
147         } else {
148             color4j.setStyleSet(StyleSet.ANSI);
149         }
150         code = color4j.convert(code);
151         Toolkit.getDefaultToolkit().getSystemClipboard().setContents(
152             new StringSelection(code),
153             null);
154         //不再显示Message  
155         //showMessage("data have been sent to system clipboard.");
156         setTitle(oldTitle);
157     }
158 
159     private void convertFromCB() {
160         Transferable clip =
161             Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
162         String code = null;
163         try {
164             code = (Stringclip.getTransferData(DataFlavor.stringFlavor);
165         } catch (Exception e) {
166             e.printStackTrace();
167         }
168         convert(code);
169     }
170 
171     private void createMenu() {
172         // create an exit item
173         SysTrayMenuItem itemExit = new SysTrayMenuItem("Exit""exit");
174         itemExit.addSysTrayMenuListener(this);
175 
176         // create an about item
177         SysTrayMenuItem itemAbout = new SysTrayMenuItem("About...""about");
178         itemAbout.addSysTrayMenuListener(this);
179 
180         SysTrayMenuItem itemConvert =
181             new SysTrayMenuItem("Convert code from clipboard""convert");
182         itemConvert.addSysTrayMenuListener(this);
183 
184         SubMenu subMenu = new SubMenu("Type");
185         checkMenuUbb = new CheckableMenuItem("UBB""ubb");
186         checkMenuUbb.addSysTrayMenuListener(this);
187         checkMenuUbb.setState(true);
188         checkMenuAnsi = new CheckableMenuItem("Ansi""ansi");
189         checkMenuAnsi.addSysTrayMenuListener(this);
190         subMenu.addItem(checkMenuAnsi);
191         subMenu.addItem(checkMenuUbb);
192 
193         menu = new SysTrayMenu(icon, "Color4j");
194         menu.addItem(itemExit);
195         menu.addSeparator();
196         menu.addItem(itemAbout);
197         menu.addSeparator();
198         menu.addItem(itemConvert);
199         menu.addItem(subMenu);
200     }
201 
202     public void iconLeftClicked(SysTrayMenuEvent e) {
203         if (isVisible()) {
204             hide();
205         } else {
206             show();
207             setState(Frame.NORMAL);
208             toFront();
209         }
210     }
211 
212     public void iconLeftDoubleClicked(SysTrayMenuEvent e) {}
213 
214     public void menuItemSelected(SysTrayMenuEvent e) {
215         if (e.getActionCommand().equals("exit"))
216             System.exit(0);
217         else if (e.getActionCommand().equals("about")) {
218             JOptionPane.showMessageDialog(
219                 this,
220                 "Color4j. \nWith SysTray for Java v" + SysTrayMenu.VERSION);
221         } else if (e.getActionCommand().equals("ubb")) {
222             checkMenuUbb.setState(true);
223             checkMenuAnsi.setState(false);
224             radioUbb.setSelected(true);
225         } else if (e.getActionCommand().equals("ansi")) {
226             checkMenuUbb.setState(false);
227             checkMenuAnsi.setState(true);
228             radioAnsi.setSelected(true);
229         } else if (e.getActionCommand().equals("convert")) {
230             convertFromCB();
231         } else
232             JOptionPane.showMessageDialog(this, e.getActionCommand());
233     }
234 }




话题树型展开
人气 标题 作者 字数 发贴时间
15438 [精华] color4j Jove 4926 2004-03-17 14:03
13170 Re:color4j Jove 0 2004-03-17 14:04
12633 Re:color4j chenyajun5 2218 2005-01-26 10:59
12582 Re:color4j chenyajun5 14 2005-01-26 11:00
13237 Re:color4j alexyu2000 24 2005-01-26 11:14
13106 Re:color4j dapan 52 2004-03-17 14:59
13180 Re:color4j Jove 102 2004-03-17 15:08
12998 Re:color4j rainman 23 2004-03-17 15:14
13176 Re:color4j Jove 214 2004-03-17 15:19
12941 Re:color4j rainman 18 2004-03-17 15:46
13292 Re:color4j freeman_z 34868 2004-03-25 04:30
13064 Re:color4j nothing 5 2004-03-25 05:31
13064 Re:color4j Starcraft 13 2004-04-11 12:13
12949 Re:color4j alexyu2000 179 2004-04-13 09:06
12878 Re:color4j 文夕 572 2004-04-22 19:58
13009 Re:color4j ditty 61863 2004-04-22 20:20
12720 Re:color4j fnhufoz 2 2004-05-22 00:05
12640 Re:color4j littledeer1974 60 2005-01-06 12:39

reply to postflat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923