java - 制作按钮类

标签 java swing

我遇到的问题是弄清楚如何将我的按钮类链接到我的主程序。我意识到我有很多来自外部类的方法,所以我将尝试给出一个概述。基本上我有一个引号数组列表和一个根据字母表随机排列创建的键。我用这个 key 来“加密”报价。通过此方法:

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 );
   }
}

那么我该如何更新显示的报价以替换字母?我想我可以在按钮类中使用 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 );

    }
}

最佳答案

@MadProgrammer 显然是正确的。为什么你没有子类化JButton??

现在看你的代码,

不清楚您收到了什么错误,或者什么对您不起作用。

你应该有

public class SubstituteButton extends RoundedRectangle implements MouseListener

在某个阶段

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

?这会将您的按钮连接到监听器。

另外,您在哪里将按钮添加到框架中? 请贴出完整的代码。

关于java - 制作按钮类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33703358/

相关文章:

java - 动态 jPanel 按钮 actionCommand

Java 和 JNI 全局变量

java - 如何在java聊天中更新JList

java - 当用户单击 JTextField 时如何显示“打开文件”对话框?

java - 如何使用 JTabbedPane 引用特定选项卡?

java - 将java日期转换为Sql时间戳

java - 如何从 Spring-Devtools 重启周期中排除使用 WSDL 生成的类?

java - JLabel:一起使用 HTML 和方法调用

java - 我的 Java GUI 应该使用哪个 LayoutManager?

java - 想在同一个 servlet 类中编写多个 doget 方法是否可能......如果是......如何......