2015-11-13 67 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