Java 在ActionListener中调用
我正在尝试将我的代码插入到我的GUI中,现在我找不到一种方法来调用ActionListener中的文本,而不中断我的代码。我知道findAndReplace()中有一些东西我需要处理才能使它在我的GUI中工作。。。但现在我只是想解释一下ActionListenerJava 在ActionListener中调用,java,swing,actionlistener,jtextfield,local-variables,Java,Swing,Actionlistener,Jtextfield,Local Variables,我正在尝试将我的代码插入到我的GUI中,现在我找不到一种方法来调用ActionListener中的文本,而不中断我的代码。我知道findAndReplace()中有一些东西我需要处理才能使它在我的GUI中工作。。。但现在我只是想解释一下ActionListener import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import javax.swing.*;
public class HangmanPanel extends JPanel {
static Boolean FOUND;
private static final long serialVersionUID = -5793357804828609325L;
public static String answerKey() {
//get random array element
String array[] = new String[10];
array[0] = "hamlet";
array[1] = "mysts of avalon";
array[2] = "the iliad";
array[3] = "tales from edger allan poe";
array[4] = "the children of hurin";
array[5] = "the red badge of courage";
array[6] = "of mice and men";
array[7] = "utopia";
array[8] = "chariots of the gods";
array[9] = "a brief history of time";
ArrayList<String> list = new ArrayList<String>(Arrays.asList(array));
Collections.shuffle(list);
String s = list.get(0);
return s;
}
public StringBuilder dashReplace(String s) {
//replace non-white space char with dashes and creates StringBuilder Object
String tW = s.replaceAll("\\S", "-");
System.out.print(tW + "\n");
StringBuilder AnswerKey = new StringBuilder(tW);
return AnswerKey;
}
public HangmanPanel(){
this.setLayout(null);
JLabel heading = new JLabel("Welcome to the Hangman App");
JButton Button = new JButton("Ok");
//get input
Button.addActionListener(new input());
JLabel tfLable = new JLabel("Please Enter a Letter:");
JLabel AnswerKey = new JLabel(dashReplace(answerKey()).toString());
JTextField text = new JTextField(10);
heading.setSize(200, 50);
tfLable.setSize(150, 50);
text.setSize(50, 30);
Button.setSize(60, 20);
AnswerKey.setSize(200, 100);
heading.setLocation(300, 10);
tfLable.setLocation(50, 40);
text.setLocation(50, 80);
Button.setLocation(100, 85);
AnswerKey.setLocation(100,85);
this.add(heading);
this.add(tfLable);
this.add(text);
this.add(Button);
this.add(AnswerKey);
}
public class input implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// can't access text
sc = text.getText();
char ch = sc.charAt(0);
findAndReplace(s, AnswerKey, input, ch);
}
}
public static int findAndReplace(String s, StringBuilder AnswerKey, String sc,
char ch) {
//find position of user input and replace
int pos = -1;
FOUND = false;
while(true){
pos = s.indexOf(sc, pos+1);
if(pos < 0){
break;
}else{
FOUND = true;
AnswerKey.setCharAt(pos, ch);
}
}
System.out.println(AnswerKey);
return pos;
}
}
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.util.ArrayList;
导入java.util.array;
导入java.util.Collections;
导入javax.swing.*;
公共类HangmanPanel扩展了JPanel{
发现静态布尔值;
私有静态最终长serialVersionUID=-579335780484828609325L;
公共静态字符串应答器密钥(){
//获取随机数组元素
字符串数组[]=新字符串[10];
数组[0]=“hamlet”;
数组[1]=“阿瓦隆的秘密”;
数组[2]=“伊利亚特”;
数组[3]=“埃德格·爱伦·坡的故事”;
数组[4]=“胡林之子”;
阵法[5]=“红色勇气徽章”;
数组[6]=“人与鼠”;
数组[7]=“乌托邦”;
阵列[8]=“众神战车”;
数组[9]=“时间简史”;
ArrayList list=新的ArrayList(Arrays.asList(array));
集合。洗牌(列表);
字符串s=list.get(0);
返回s;
}
公共StringBuilder dashReplace(字符串s){
//将非空白字符替换为破折号并创建StringBuilder对象
字符串tW=s.replaceAll(“\\s”,“-”);
系统输出打印(tW+“\n”);
StringBuilder AnswerKey=新StringBuilder(tW);
返回应答键;
}
公共刽子手小组(){
此.setLayout(null);
JLabel heading=新JLabel(“欢迎使用Hangman应用程序”);
JButton按钮=新JButton(“确定”);
//获取输入
addActionListener(新输入());
JLabel tfLable=新的JLabel(“请输入一个字母:”);
JLabel AnswerKey=新的JLabel(dashReplace(AnswerKey()).toString());
JTextField text=新的JTextField(10);
标题.设置尺寸(200,50);
t标签设置尺寸(150,50);
文本设置大小(50,30);
按钮。设置大小(60,20);
应答键设置大小(200100);
标题.设置位置(300,10);
t标签设置位置(50,40);
文本设置位置(50,80);
按钮。设置位置(100,85);
应答机设置位置(100,85);
本条增补(标题);
添加(tfLable);
增加(文本);
这个。添加(按钮);
添加(应答键);
}
公共类输入实现ActionListener{
@凌驾
已执行的公共无效操作(操作事件e){
//无法访问文本
sc=text.getText();
char ch=sc.charAt(0);
查找并替换(s、应答键、输入、ch);
}
}
公共静态整型查找和替换(字符串s、StringBuilder应答键、字符串sc、,
字符(ch){
//查找用户输入的位置并替换
int pos=-1;
发现=错误;
while(true){
位置=s.indexOf(sc,位置+1);
如果(位置<0){
打破
}否则{
发现=真;
应答键。设置字符(位置,通道);
}
}
系统输出打印LN(应答键);
返回pos;
}
}
文本JTextField变量在构造函数中声明,并且对于类的其他方法不可见,因为它没有类“作用域”。您需要通过在类中声明该变量,而不是在构造函数中声明该变量,使其成为类字段,以便使其在整个类中可见
e、 g
文本JTextField变量在构造函数中声明,并且对类的其他方法不可见,因为它没有类“作用域”。您需要通过在类中声明该变量,而不是在构造函数中声明该变量,使其成为类字段,以便使其在整个类中可见 e、 g
首先不要使用小写的类名和大写的变量名,你的代码真的很难读 您可以使用匿名内部类添加事件侦听器,如下所示:
Button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// can't access text
sc = text.getText();
char ch = sc.charAt(0);
findAndReplace(s, AnswerKey, input, ch);
}
});
将text
变量标记为final,以便在匿名类中访问它:
final JTextField text = new JTextField(10);
首先不要使用小写的类名和大写的变量名,你的代码真的很难读 您可以使用匿名内部类添加事件侦听器,如下所示:
Button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// can't access text
sc = text.getText();
char ch = sc.charAt(0);
findAndReplace(s, AnswerKey, input, ch);
}
});
将text
变量标记为final,以便在匿名类中访问它:
final JTextField text = new JTextField(10);
1) 请学习类、方法和属性名称的通用(特别是用于名称的大小写),并一致使用它们。2) javagui可能必须在许多平台上工作,在不同的屏幕分辨率上&使用不同的plaf。因此,它们不利于部件的精确放置。对于健壮的GUI,使用布局管理或它们的组合,以及空白的布局填充和边框来组织组件。3) 一行空白就足够了。1)请学习类、方法和属性名称的通用(特别是用于名称的大小写)并一致使用它们。2) javagui可能必须在许多平台上工作,在不同的屏幕分辨率上&使用不同的plaf。因此,它们不利于部件的精确放置。对于健壮的GUI,使用布局管理或它们的组合,以及空白的布局填充和边框来组织组件。3) 一行空白就足够了。@Sage1216:不过最好使用类字段,因为您可能需要通过程序中的多个方法访问文本字段。标记“}”的语法错误,请删除此标记。。。无论如何,这是前一段的结束括号method@Hovercraft满是鳗鱼:我不知道如何使用类字段。。我只是一个java的初学者,很快就被抛入了深渊。。。我有这个刽子手程序完成,但它没有gui界面,这是我的老师想要的。。。我是艾尔