最近はAndroidアプリの開発をしているのでEclipseなのだけれど
JavaFXしたいな~と思ってNetBeans。
ところがJavaFXする前に…
awtのデザインをGUIで構築できるのが便利すぎてつい遊んでしまった。
BlogCode.java
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ /* * BlogCode.java * * Created on 2010/05/20, 2:11:47 */ package com.blogspot.takumakei.blogcode; import java.awt.Toolkit; import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.UnsupportedFlavorException; import java.io.IOException; import javax.swing.UIManager; /** * * @author Kei */ public class BlogCode extends javax.swing.JFrame { /** Creates new form BlogCode */ public BlogCode() { initComponents(); setTitle("BlogCode"); jTextArea1.setText(getClipboardString()); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); jScrollPane1 = new javax.swing.JScrollPane(); jTextArea1 = new javax.swing.JTextArea(); jTextField1 = new javax.swing.JTextField(); jButton3 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jButton1.setText("Reset"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jButton2.setText("Copy"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); jTextArea1.setColumns(20); jTextArea1.setRows(5); jScrollPane1.setViewportView(jTextArea1); jButton3.setText("Clear"); jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton3ActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 269, Short.MAX_VALUE) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup() .addComponent(jButton1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButton3) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 83, Short.MAX_VALUE) .addComponent(jButton2)) .addComponent(jTextField1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 269, Short.MAX_VALUE)) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap() .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 172, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton1) .addComponent(jButton2) .addComponent(jButton3)) .addContainerGap()) ); pack(); }// </editor-fold> private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { final String title = jTextField1.getText(); final String body = jTextArea1.getText(); if (0 == body.length()) { return; } StringBuilder sb = new StringBuilder(); if (0 < title.length()) { sb.append("<pre class=\"mycodeT\" onclick=\"toggleMyCode(this)\">"); sb.append(escapeHTML(title)); sb.append("</pre>"); } sb.append("<pre class=\"mycodeB\">"); sb.append(escapeHTML(body)); sb.append("</pre>\n"); setClipboardString(sb.toString()); } private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { jTextField1.setText(""); jTextArea1.setText(getClipboardString()); } private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) { jTextField1.setText(""); jTextArea1.setText(getClipboardString()); } public static String escapeHTML(String s){ // http://www.rgagnon.com/javadetails/java-0306.html StringBuilder sb = new StringBuilder(s.length()); int n = s.length(); for (int i = 0; i < n; i++) { char c = s.charAt(i); switch (c) { case '\t': sb.append(" "); break; case '<': sb.append("<"); break; case '>': sb.append(">"); break; case '&': sb.append("&"); break; case '"': sb.append("""); break; case 'à': sb.append("à");break; case 'À': sb.append("À");break; case 'â': sb.append("â");break; case 'Â': sb.append("Â");break; case 'ä': sb.append("ä");break; case 'Ä': sb.append("Ä");break; case 'å': sb.append("å");break; case 'Å': sb.append("Å");break; case 'æ': sb.append("æ");break; case 'Æ': sb.append("Æ");break; case 'ç': sb.append("ç");break; case 'Ç': sb.append("Ç");break; case 'é': sb.append("é");break; case 'É': sb.append("É");break; case 'è': sb.append("è");break; case 'È': sb.append("È");break; case 'ê': sb.append("ê");break; case 'Ê': sb.append("Ê");break; case 'ë': sb.append("ë");break; case 'Ë': sb.append("Ë");break; case 'ï': sb.append("ï");break; case 'Ï': sb.append("Ï");break; case 'ô': sb.append("ô");break; case 'Ô': sb.append("Ô");break; case 'ö': sb.append("ö");break; case 'Ö': sb.append("Ö");break; case 'ø': sb.append("ø");break; case 'Ø': sb.append("Ø");break; case 'ß': sb.append("ß");break; case 'ù': sb.append("ù");break; case 'Ù': sb.append("Ù");break; case 'û': sb.append("û");break; case 'Û': sb.append("Û");break; case 'ü': sb.append("ü");break; case 'Ü': sb.append("Ü");break; case '®': sb.append("®");break; case '©': sb.append("©");break; case '€': sb.append("€"); break; // be carefull with this one (non-breaking whitee space) //case ' ': sb.append(" ");break; default: sb.append(c); break; } } return sb.toString(); } // public static String stringToHTMLString(String string) { // // http://www.rgagnon.com/javadetails/java-0306.html // StringBuilder sb = new StringBuilder(string.length()); // // true if last char was blank // boolean lastWasBlankChar = false; // int len = string.length(); // char c; // // for (int i = 0; i < len; i++) // { // c = string.charAt(i); // if (c == ' ') { // // blank gets extra work, // // this solves the problem you get if you replace all // // blanks with , if you do that you loss // // word breaking // if (lastWasBlankChar) { // lastWasBlankChar = false; // sb.append(" "); // } // else { // lastWasBlankChar = true; // sb.append(' '); // } // } // else { // lastWasBlankChar = false; // // // // HTML Special Chars // if (c == '"') // sb.append("""); // else if (c == '&') // sb.append("&"); // else if (c == '<') // sb.append("<"); // else if (c == '>') // sb.append(">"); // else if (c == '\n') // // Handle Newline // sb.append("<br/>"); // else { // int ci = 0xffff & c; // if (ci < 160 ) // // nothing special only 7 Bit // sb.append(c); // else { // // Not 7 Bit use the unicode system // sb.append("&#"); // sb.append(new Integer(ci).toString()); // sb.append(';'); // } // } // } // } // return sb.toString(); // } // http://www.ne.jp/asahi/hishidama/home/tech/java/clipboard.html public static String getClipboardString() { Toolkit toolkit = Toolkit.getDefaultToolkit(); Clipboard clipboard = toolkit.getSystemClipboard(); try { return (String)clipboard.getData(DataFlavor.stringFlavor); } catch (UnsupportedFlavorException e) { return ""; } catch (IOException e) { return ""; } } // http://www.ne.jp/asahi/hishidama/home/tech/java/clipboard.html public static void setClipboardString(String str) { Toolkit toolkit = Toolkit.getDefaultToolkit(); Clipboard clipboard = toolkit.getSystemClipboard(); StringSelection selection = new StringSelection(str); clipboard.setContents(selection, selection); } /** * @param args the command line arguments */ public static void main(String args[]) { try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); } catch (Exception e) { } java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new BlogCode().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JButton jButton3; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTextArea jTextArea1; private javax.swing.JTextField jTextField1; // End of variables declaration }
0 件のコメント:
コメントを投稿