2011-09-02 156 views
0

我正在編寫一個加密程序,它會將常規字詞轉換爲特定的「代碼」。一切都完成了,但程序忽略了提交按鈕代碼。我應該怎麼做才能解決這個問題?按鈕邏輯被忽略 - 爲什麼?

import javax.swing.*; 
    import java.io.*; 
    import java.awt.event.*; 
    import java.awt.*; 
    public class decode extends JFrame { 
    private JTextArea textaci; 
    private JTextArea textaclr; 
    private JLabel lclear; 
    private JLabel lcipher; 
    private JButton bsubmit; 
    private String cleartext; 
    private String ciphertext; 
    private boolean clrtoci; 


    public decode(){ 
     super(); 
     setTitle("Decoder"); 
     setSize(300,200); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setLayout(new GridBagLayout()); 
     GridBagConstraints c =new GridBagConstraints(); 
     c.fill=GridBagConstraints.VERTICAL; 
     c.weightx=0.5; 
     textaci=new JTextArea(); 
     textaclr=new JTextArea(); 
     lclear=new JLabel("cleartext:"); 
     lcipher=new JLabel("ciphertext:"); 
     bsubmit=new JButton("Submit"); 
     bsubmit.setActionCommand("enable"); 
     textaci.setEditable(true); 
     textaci.setLineWrap(true); 
     textaclr.setEditable(true); 
     textaclr.setLineWrap(true); 
     textaci.setText(ciphertext); 
     c.gridx=0; 
     c.gridy=0; 
     add(lclear); 
     c.gridx=1; 
     c.gridy=0; 
     add(textaclr); 

     c.gridx=0; 
     c.gridy=2; 
     add(lcipher); 
     c.gridx=3; 
     c.gridy=4; 
     add(textaci); 
     add(bsubmit); 



    //----------------------------------------------------------------------------\\ 
     TextFieldHandler hand=new TextFieldHandler(); 
     bsubmit.addActionListener(hand); 
     setVisible(true); 
    } 
    public class TextFieldHandler implements ActionListener{ 
     public void actionPerformed(ActionEvent event){ 
     if(event.getSource()==bsubmit){ 
      cleartext=textaclr.getText(); 
      int cleartextl=cleartext.length(); 
      if(textaci.getText()==null){ 
      clrtoci=true; 
     } 
     else{ 
      clrtoci=false; 
     } 
      if(clrtoci==true){//if it's cleartext to ciphertext 
       for(int i=0;i>=cleartextl;i++){ 
        if(cleartext.contains("a")){ 
         ciphertext="3"; 
        } 
        if(cleartext.contains("b")){ 
         ciphertext="b"; 
        } 
        //and so on and on to the rest of the alphabet 
             }//end of for statement 
       textaci.setText(ciphertext); 
       setVisible(true); 
       System.out.print(ciphertext); 
      }//if it's cleartext to ciphertext 

     }//bsubmit logic 
    }//end of event 
     }//end of ActionListener 


    public static void main(String[] args){ 
     new decode(); 
    } 
} 

回答

6

"but the program is ignoring the submit button code."

定義 「忽略」。這個按鈕對我來說工作得很好。只需在代碼中添加一些System.out.println(...)語句以查看代碼的哪些部分正在執行。

由於源始終是提交按鈕,因此代碼將始終經歷第一個if條件。

您需要重新組織您的邏輯,因爲else條件將永遠不會執行。

+1

單擊「提交」按鈕後,如果您嘗試重新調整「JFrame」的大小,您會注意到有一個_major_錯誤。我唯一的猜測是它可能無限循環,特別是因爲'print'從不執行。看到我的答案。 – mre

1

我試了一下代碼。在第一次測試後,clrtoci總是錯誤的。然後,我看着textaci.getText(),這顯然不是null。我注意到你之前填寫了ciphertext,所以有可能該字符串不是null

編輯: 另外for (int i = 0; i >= cleartextl; i++)應該是for (int i = 0; i < cleartextl; i++) 這使得它在我的機器上工作。

`

2

JButton例如動作監聽工作就好了。你的循環邏輯有問題。也就是說,你無限循環。

for(int i = 0;i >= cleartextl; i++){ 
    //do stuff 
} 

這應該如下重構:

for(int i = 0;i < cleartextl; i++){ 
    //do stuff 
} 

此外,通過你的代碼的質量來看,我建議您閱讀以下教程: