2012-11-07 14 views
1

我想開發一個十六進制編輯器,其中編輯器面板提供了按鍵監聽器,並且通過鍵盤輸入被轉換爲相應的輸入。然而,問題是關於保存轉換的輸入。只有一個關鍵的衝程值是現在一次轉換的。我想在字符串中使用這個轉換後的input()作爲一堆,並將其用於其他目的。我嘗試將轉換後的文本保存在字節數組中,但它返回我輸入的第一個值,而不管之後的輸入是什麼。如何將動態輸入的文本存儲在一個字符串中

package gui.hex; 

import javax.swing.*; 

import cryptool.Test; 

import java.awt.*; 
import java.awt.event.*; 
import java.nio.charset.Charset; 
import java.nio.charset.CharsetEncoder; 

public class JHexEditorASCII extends JComponent implements MouseListener, 
     KeyListener { 

    private static final long serialVersionUID = 5636121664420538046L; 
    private JHexEditor he; 
    private static CharsetEncoder encoder = Charset.forName("UTF-8") 
      .newEncoder(); 
    public static String modifiedText; 
    public static byte temp[]; 

    public static byte[] getTemp() { 
     return temp; 
    } 

    public void setTemp(byte[] temp) { 
     this.temp = temp; 
    } 

    protected JHexEditorASCII(JHexEditor he) { 
     this.he = he; 
     addMouseListener(this); 
     addKeyListener(this); 
     addFocusListener(he); 
    } 

    public Dimension getPreferredSize() { 
     debug("getPreferredSize()"); 
     return getMinimumSize(); 
    } 

    public Dimension getMinimumSize() { 
     debug("getMinimumSize()"); 

     Dimension d = new Dimension(); 
     FontMetrics fn = getFontMetrics(JHexEditor.font); 
     int h = fn.getHeight(); 
     int nl = he.getLines(); 
     d.setSize((fn.stringWidth(" ") + 1) * (16) + (he.border * 2) + 1, h 
       * nl + (he.border * 2) + 1); 
     return d; 
    } 

    public void paint(Graphics g) { 
     debug("paint(" + g + ")"); 
     debug("cursor=" + he.cursor + " buff.length=" + he.buff.length); 
     Dimension d = getMinimumSize(); 

     // sets the color to the background. 
     g.setColor(Color.orange); 
     g.fillRect(0, 0, d.width, d.height); 
     g.setColor(Color.black); 

     g.setFont(JHexEditor.font); 

     // ascii data 
     int ini = he.getInitial() * 16; 
     int fin = ini + (he.getLines() * 16); 
     if (fin > he.buff.length) 
      fin = he.buff.length; 

     int x = 0; 
     int y = 0; 
     for (int n = ini; n < fin; n++) { 
      if (n == he.cursor) { 

       // sets color for the focus cursor it used to be in blue color. 
       g.setColor(Color.red); 
       if (hasFocus()) 
        he.background(g, x, y, 1); 
       else 
        he.table(g, x, y, 1); 
       if (hasFocus()) 
        g.setColor(Color.white); 
       else 
        g.setColor(Color.black); 
      } else { 
       g.setColor(Color.black); 
      } 

      // builds a character object 
      String s; 

      if (encoder.isLegalReplacement(new byte[] { he.buff[n] })) 
       s = "" + new Character((char) he.buff[n]); 
      else 
       s = "."; 

      // compares the input supplied 
      // if ((he.buff[n] < 20) || (he.buff[n] > 126)) 

      // converts the input entered into string 
      // s = "."; 
      // displaying of the text goes here. 

      // displays the input 

      System.out.println("the typed in string is:" + s); 

      temp = s.getBytes(); 
      setTemp(temp); 

      setModifiedText(s); 

      he.printString(g, s, (x++), y); 

      if (x == 16) { 
       x = 0; 
       y++; 
      } 
     } 

    } 

    private void debug(String s) { 
     if (he.DEBUG) 
      System.out.println("JHexEditorASCII ==> " + s); 
    } 

    public void dispaly(Graphics g) { 
     int x = 0; 
     int y = 0; 

     he.printString(g, Test.getK(), (x++), y); 
     if (x == 16) { 
      x = 0; 
      y++; 
     } 
    } 

    // calculate the position of the mouse 
    public int calculateMousePosition(int x, int y) { 
     FontMetrics fn = getFontMetrics(JHexEditor.font); 
     x = x/(fn.stringWidth(" ") + 1); 
     y = y/fn.getHeight(); 
     debug("x=" + x + " ,y=" + y); 
     return x + ((y + he.getInitial()) * 16); 
    } 

    // mouselistener 
    public void mouseClicked(MouseEvent e) { 
     if (e.getClickCount() == 2) { 
      // double clicked 
      he.setNewTextListener.actionPerformed(new ActionEvent(this, 0, 
        "NEWASCII")); 
     } 

     debug("mouseClicked(" + e + ")"); 
     he.cursor = calculateMousePosition(e.getX(), e.getY()); 
     this.requestFocus(); 
     he.repaint(); 
    } 

    public void mousePressed(MouseEvent e) { 
    } 

    public void mouseReleased(MouseEvent e) { 
    } 

    public void mouseEntered(MouseEvent e) { 
    } 

    public void mouseExited(MouseEvent e) { 
    } 

    // KeyListener 
    public void keyTyped(KeyEvent e) { 
     debug("keyTyped(" + e + ")"); 

     // only add content, if buffer is large enough 
     if (he.buff.length > he.cursor) { 
      he.buff[he.cursor] = (byte) e.getKeyChar(); 

      if (he.cursor != (he.buff.length - 1)) 
       he.cursor++; 
      // change this 
      // System.out.println(he.buff.toString()); 

      he.repaint(); 
     } 
    } 

    public void keyPressed(KeyEvent e) { 
     debug("keyPressed(" + e + ")"); 
     he.keyPressed(e); 
    } 

    public void keyReleased(KeyEvent e) { 
     debug("keyReleased(" + e + ")"); 
    } 

    public boolean isFocusTraversable() { 
     return true; 
    } 

    public static String getModifiedText() { 
     return modifiedText; 
    } 

    public void setModifiedText(String modifiedText) { 
     this.modifiedText = modifiedText; 
    } 
} 

回答

1
  • 開發一個十六進制編輯器(encoder.isLegalReplacement...

  • 使用JTextFieldDocumentFilter,或者最簡單的可以通過使用paint()JComponent

  • 使用paintComponentJFormatterTextField用塗料字符(而不是paint()Swing JComponents

  • 添加DocumentListenerJTextField with DocumentFilterJFormatterTextField

  • 有趣的方式可能是建立/製作/創建ArrayList of hex()和使用AutoComplete JTextField

+1

參見本['HexSpinnerTest'(HTTP:/ /stackoverflow.com/a/13121724/230513)。 – trashgod

+1

@trashgod感謝您爲'JFormatterTextField' /'JSpinner'提供清晰簡單的方法:-) – mKorbel

+0

感謝您的所有投入:) – kirankar

相關問題