在 Java 中使用 .actionPerformed 实现变量

发布于 2024-12-11 13:01:38 字数 1244 浏览 0 评论 0原文

我绝不是一名程序员,我所知道的大部分知识都是通过在论坛上修改和查看大多数由你们优秀的人编写的示例来学习的。我已经用java达到了我试图在框架中创建按钮的目的。基本上我想做的是从文件中读取单词,为每个单词创建一个按钮,然后让我选择该按钮来显示选项(稍后将进行编码)。我知道以前已经回答过这个问题,但没有一个答案真正给了我任何澄清(我再次对此有点菜鸟,所以非常感谢非编程术语/简单化的解释,但当然不是必需的:))。

我创建变量按钮的代码(desireFile 是从程序前面的系统中读取的):

File fileIn = new File(desireFile);
Scanner fileReader = new Scanner (fileIn);
int wordNum = 0;

while(fileReader.hasNext()){
    String wordReader = fileReader.next();
        buttonVal = new JButton (wordReader);
    buttonVal.addActionListener(new ButtonListener());
    buttonVal.setEnabled(true);
    contentPane.add(buttonVal, BorderLayout.NORTH);
    wordNum++;
}

我使用 wordNum 只是为了确保我的循环正确递增。然后我有我的 ButtonListener 类:

class ButtonListener implements ActionListener {
  ButtonListener() {
  }

  public void actionPerformed(ActionEvent e) {
     System.out.println("You clicked the button");
  }
}

我让它工作,它创建我的变量按钮,每当我单击这些按钮中的任何一个时,它都会在控制台中打印“您单击了该按钮”。我遇到的问题是,这不是我最终想要做的。我需要根据主类的结果来改变操作(例如,如果文件中的文本显示“这是一只狗”,我需要“this”按钮执行与“is”按钮不同的操作) 。另一个问题是,由于文件输入每次都是可变的,我不知道如何设置 if/else 语句(因为这个词实际上可以是任何东西,因此按钮可以是任何东西,不是吗?)

我再次享受编程作为一种爱好,并感谢所有帮助,如果您能为我简化任何解释(或给我很好的链接),我将不胜感激。到目前为止,我对 Java 中的一切都很满意,但这些按钮让我难住了。感谢各位高手的帮助

I am by no means a programmer and have learned most of what I know by tinkering around on forums and looking through examples that are mostly written by you fine people. I have gotten to the point with java that I am trying to create buttons in a frame. Basically what I want to do is to read words from a file, create a button for each word, and then let me select that button to display options (that will be coded later). I know that this has been answered before, but none of the answers have really given me any clarification (again I am kinda a noob at this so non-programming jargon/simplistic explanation is greatly appreciated but of course not required :) ).

My code to create my variable buttons (desireFile is read from System in earlier in the program):

File fileIn = new File(desireFile);
Scanner fileReader = new Scanner (fileIn);
int wordNum = 0;

while(fileReader.hasNext()){
    String wordReader = fileReader.next();
        buttonVal = new JButton (wordReader);
    buttonVal.addActionListener(new ButtonListener());
    buttonVal.setEnabled(true);
    contentPane.add(buttonVal, BorderLayout.NORTH);
    wordNum++;
}

Where I use wordNum just to ensure that my loop is incrementing correctly. Then I have my ButtonListener class:

class ButtonListener implements ActionListener {
  ButtonListener() {
  }

  public void actionPerformed(ActionEvent e) {
     System.out.println("You clicked the button");
  }
}

I get this to work, it creates my variable buttons and whenever I click on any one of those buttons it prints "You clicked the button" in the console. The issue I am having is, that isn't what I want it to do in the end. I need the action to be variable depending on results from the main class (example if the text in the file reads "this is a dog", I need the button for "this" to do something different than the button for "is"). Another problem is that, with the file input being variable each time, I don't know how I would set up if/else statements (since the word could literally be anything, thus the button anything, no?)

Again I am just enjoying programming as a hobby and appreciate all help, if you could dumb down any explanations for me (or give me links that do a good job of it) I would appreciate it. I have been fine with everything in Java up to this point but these buttons have me stumped. Thanks for the help gurus

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谈下烟灰 2024-12-18 13:01:39

您可以通过以下两种方式之一轻松获取 JButton 的文本:通过 ActionEvent 的 actionCommand 属性获取

public void actionPerformed(ActionEvent e) {
   String buttonText = e.getActionCommand();
}

或通过其 getSource

public void actionPerformed(ActionEvent e) {
   String buttonText = ((JButton)e.getSource()).getText();
}

如何处理返回的 String 将取决于您想用它做什么以及如何确定要做什么用什么词做。你会用字典来查这个词然后决定做什么吗?鉴于目前的信息很难说。

You can easily get the text of the JButton in one of two ways, either from the ActionEvent's actionCommand property obtained via

public void actionPerformed(ActionEvent e) {
   String buttonText = e.getActionCommand();
}

or via its getSource

public void actionPerformed(ActionEvent e) {
   String buttonText = ((JButton)e.getSource()).getText();
}

How you deal with the String returned will depend on what you want to do with it and how you determine what to do with what word. Will you have a dictionary to look up the word and then decide what to do? Hard to say given the info so far.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文