2015-11-13 70 views
2

我遇到的問題是搞清楚如何將我的按鈕類鏈接到我的主程序。我意識到我有很多來自外部類的方法,所以我會盡量給出一個概述。基本上我有一個引用的arrayList和從字母表的隨機排列創建的一個鍵。我使用這個密鑰來「加密」報價。通過這種方法:製作一個按鈕類

public static String translate (String text, String key) 
    { 
     String newText = ""; 
     for(int i = 0; i < text.length(); i++) 
     { 
      char currentLetter = text.charAt(i); 
      if(ALPHA.contains(Character.toString(currentLetter))) 
      { 
       int index = ALPHA.indexOf(currentLetter); 
       char newLetter = key.charAt(index); 
       newText = newText + text.substring(i, i + 1).replace 
        (currentLetter, newLetter) ; 
      } 
      else 
       newText = newText + currentLetter; 
     } 
     return newText; 
    } 

所以我想要做的就是有一個按鈕,需要用戶輸入並與輸入引號替換字母。我沒有使用JButton,我正在使用庫來製作正方形,然後使用mouseEvent

import wheelsunh.users.*; 
import java.awt.Color; 
import java.awt.event.MouseEvent; 
import javax.swing.JOptionPane; 

/** 
* Creates a button 
* 
* @author Scott 
*/ 
public class SubstituteButton extends RoundedRectangle 
{ 
    String response; 

    public SubstituteButton(int x, int y) 
    { 
     super(x, y); 
     super.setSize(20, 20); 
     super.setFillColor(Color.LIGHT_GRAY); 
     super.setFrameColor(Color.BLACK); 
    } 

    public void mousePressed(MouseEvent e) 
    { 
     super.setFillColor(new Color(131,111,255)); 

     try 
     { 
     response = JOptionPane.showInputDialog("Which letter" 
      + " would you like to replace? Ex. ab would replace all a's" 
      + " with b's"); 
     } 
     catch(NullPointerException exeception) 
     { 
      JOptionPane.showMessageDialog(null, "Input Error"); 
     } 

     super.setFillColor(Color.LIGHT_GRAY);  
    } 

    public String getInput() 
    { 
     if(response.length() == 2 && 
      Character.isLetter(response.charAt(0)) && 
      Character.isLetter(response.charAt(1))) 
     { 
      return response; 
     } 
     return null; 
    } 
    public static void main(String args[]) 
    { 
     new Frame(); 
     new SubstituteButton(100, 100); 
    } 
} 

HSO我怎麼會更新顯示的報價,從而替代的字母:我在一個單獨的類在這裏所作的按鈕?我以爲我可以在按鈕類中使用replace()方法,但它不會更新顯示的引用。這裏是主程序:

import wheelsunh.users.*; 
import java.util.*; 
import java.lang.Character; 

/** 
* Displays a quote with letters in blocks and punctuation without blocks. 
* If a letter has been changed from the original then it is highlighted. 
* Displayed lines must be broken to stay on frame. 
* 
* 
* @author Scott 
*/ 
public class CryptogramApp extends ShapeGroup 
{ 
    private ArrayList<String> blockQuote; 
    private int quoteLength; 
    private SubstituteButton substituebutton; 
    private boolean newState = true; 
    private String key, quote, encryptedQuote; 

    /** 
    * Creates a block-quote with first letter at initialX,initialY 
    * with the text from quote. 
    * 
    * @param initialX int 
    * @param initialY int 
    * @param quote String 
    */ 
    //-------------------------------------------------------------------------- 
    public CryptogramApp(int initialX, int initialY) 
    {   
     if(newState == true) 
      newQuote(); 

     int newx = initialX; 

     for(int i = 0; i < quote.length(); i++) 
     { 
      String letter = Character.toString(encryptedQuote.charAt(i)); 
      BlockLetter b = new BlockLetter(newx, initialY, letter); 
      newx += BlockLetter.WIDTH; 

      if(letter.equals(" ") && b.getXLocation() > 400) 
      { 
       newx = initialX; 
       initialY += 40; 
      } 
     } 
     newState = false; 

    } 
    public void newQuote() 
    { 
     blockQuote = new ArrayList<String>(); 
     key = StringUtilities.getRandomKey(); 
     quote = getRandomQuote(); 
     System.out.println(key); 
     encryptedQuote = StringUtilities.translate(quote, key); 
     System.out.println(encryptedQuote); 
     substituebutton = new SubstituteButton(425, 350); 
    } 
    //-------------------------------------------------------------------------- 
    /** 
    * Returns the String text with the jth character replaced with key. 
    * 
    * @param text String 
    * @param key String 
    * @param j int 
    * 
    * @return String 
    */ 
    public String getRandomQuote() 
    { 
     Random gen = new Random(); 
     ArrayList<String> list = StringUtilities.getQuotes(); 
     String quote = list.get(gen.nextInt(6)); 
     return quote; 
    } 

    //-------------------------------------------------------------------------- 
    /** 
    * Runs a simple test of CryptogramApp. 
    * 
    * @param args String[] 
    */ 
    public static void main(String args[]) 
    { 
     new Frame(700, 500); 
     new CryptogramApp(20, 50); 

    } 
} 
+1

'SubstituteButton'如何知道'quote'或'key'的任何內容?也許你應該爲按鈕設計一個[Observer Pattern](http://www.oodesign.com/observer-pattern.html),你的'CryptogramApp'可以在其中註冊一個監聽器,所以當它激活時,你可以被通知,這種方式'CryptogramApp',它有'key'和'quote'的值 – MadProgrammer

+2

你明顯地決定不使用'JButton'作爲你的基類,我認爲你認真地投射自己在因爲所有你想要複製的功能都已經被處理掉了。對於[示例](http://stackoverflow.com/questions/15846937/painting-a-particular-button-from-a-grid-of-buttons/15847188#15847188)它使用'JButton'作爲基類和簡單自定義繪畫來呈現不同的形狀 – MadProgrammer

回答

2

@MadProgrammer顯然是正確的。你爲什麼沒有分類JButton ??

現在你的代碼,

目前尚不清楚,你收到了什麼錯誤,或者什麼都不爲你工作。

你應該有

public class SubstituteButton extends RoundedRectangle implements MouseListener 

,並在某些階段

SubstituteButton button=new SubstituteButton(); 
button.addMouseListener(button) 

?這會將你的按鈕連接到監聽器。

另外,你在哪裏添加按鈕到框架? 請發佈完整的代碼。

+0

我沒有收到任何錯誤,我仍然在學習java,不確定如何獲得我所看到的內容。 。我意識到我應該使用JButton,但是當我意識到這不是一個好方法時,我已經寫了代碼。我在newQuote()中添加按鈕。它不是一個好地方,而且在添加更多功能後我會移動它。謝謝MadProgrammer和Sanjay,我非常感謝你的幫助。 – Sc0tty