Java 制作一个按钮类
我遇到的问题是如何将我的button类链接到我的主程序。我意识到我有很多来自外部类的方法,所以我将尝试给出一个概述。基本上,我有一个引用的数组列表和一个由字母表的随机排列创建的键。我使用此密钥“加密”报价。通过这种方法:Java 制作一个按钮类,java,swing,Java,Swing,我遇到的问题是如何将我的button类链接到我的主程序。我意识到我有很多来自外部类的方法,所以我将尝试给出一个概述。基本上,我有一个引用的数组列表和一个由字母表的随机排列创建的键。我使用此密钥“加密”报价。通过这种方法: public static String translate ( String text, String key ) { String newText = ""; for( int i = 0; i < text.length();
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;
}
HSo如何更新显示的报价,以替换字母?我想我可以在button类中使用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 );
}
}
导入wheelsunh.users.*;
导入java.util.*;
导入java.lang.Character;
/**
*显示带字母的分块引号和不带分块的标点符号。
*如果某个字母已从原始字母更改,则该字母将高亮显示。
*显示的行必须断开才能停留在帧上。
*
*
*@作者斯科特
*/
公共类CryptogramApp扩展了ShapeGroup
{
私有ArrayListblockQuote;
私人国际报价长度;
私有替换按钮替换按钮;
私有布尔值newState=true;
私有字符串密钥,引号,encryptedQuote;
/**
*创建首字母为initialX,initialY的块引号
*与引文。
*
*@param initialX int
*@param initial int
*@param引号字符串
*/
//--------------------------------------------------------------------------
公共CryptogramApp(int initialX,int initialY)
{
if(newState==true)
newQuote();
int newx=初始值x;
对于(int i=0;i400)
{
newx=初始值x;
初始值+=40;
}
}
newState=false;
}
public void newQuote()
{
blockQuote=newarraylist();
key=StringUtilities.getRandomKey();
quote=getRandomQuote();
系统输出打印项次(键);
encryptedQuote=StringUtilities.translate(quote,key);
System.out.println(加密报价);
取代按钮=新的取代按钮(425350);
}
//--------------------------------------------------------------------------
/**
*返回第j个字符替换为键的字符串文本。
*
*@param文本字符串
*@param键字符串
*@param j int
*
*@返回字符串
*/
公共字符串getRandomQuote()
{
Random gen=新的Random();
ArrayListlist=StringUtilities.getQuotes();
字符串quote=list.get(gen.nextInt(6));
返回报价;
}
//--------------------------------------------------------------------------
/**
*运行CryptogramApp的简单测试。
*
*@param args字符串[]
*/
公共静态void main(字符串参数[])
{
新框架(700500);
新的CryptogramApp(20,50);
}
}
@MadProgrammer显然是正确的。为什么您没有子类化JButton
现在来看看你的代码
现在还不清楚你收到了什么错误,或者什么不适合你
你应该
public class SubstituteButton extends RoundedRectangle implements MouseListener
在某个阶段
SubstituteButton button=new SubstituteButton();
button.addMouseListener(button)
??这会将您的按钮连接到侦听器
另外,在哪里将按钮添加到框架?
请发布完整的代码。替换按钮如何知道有关引号
或键
的任何信息?也许你应该设计一个按钮,你的CryptogramApp
可以向它注册一个监听器,这样当它被激活时,你可以得到通知,这样CryptogramApp
,它有键和引号
值你显然已经决定不使用JButton
作为基类,我认为这是对你自己的严重打击,因为你试图复制的所有功能都已经用它处理好了。使用JButton
作为基类和简单的自定义绘制来呈现不同的形状我没有得到任何错误,我仍在学习java,不知道如何获得我所看到的。我意识到我应该使用JButton,但当我意识到它不是一个很好的方法时,我已经编写了代码。我在newQuote()中添加按钮。这不是一个很好的地方,我会在添加更多功能后移动它。谢谢你和桑杰,我真的很感谢你的帮助。
SubstituteButton button=new SubstituteButton();
button.addMouseListener(button)