【问题标题】:Implementing a way to change a JLabel from another class in my game Part 3?在我的游戏第 3 部分中实现一种从另一个类更改 JLabel 的方法?
【发布时间】:2014-06-09 04:24:55
【问题描述】:

我目前无法更改choiceDeclaration JLabel。我在choiceDeclaration JLabel 背后的想法是简单地根据单击的JButton 显示文本。

这是我当前的项目代码:

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;

import java.awt.event.*;

public class prompt {       
    public static void main(String []args) {

        /* Setting up the JPanel and its necessities for this program */    

        JFrame choicePrompt = new JFrame("Rock, Paper, Scissors Game");
            JPanel choicePanel = new JPanel();
            JButton rockButton = new JButton("ROCK");
            JButton scissorsButton = new JButton("SCISSORS");
            JButton paperButton = new JButton("PAPER");
            JLabel choiceDeclaration = new JLabel();

        choicePrompt.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        choicePrompt.setResizable(false);
        choicePrompt.setSize(300, 300);
        choicePrompt.setVisible(true);
        choiceDeclaration.setVisible(true);

        choicePrompt.add(choicePanel);
        choicePanel.add(choiceDeclaration);
        choicePanel.add(rockButton);
        choicePanel.add(scissorsButton);
        choicePanel.add(paperButton);

        choiceDeclaration.setVerticalTextPosition(JLabel.TOP);
        choiceDeclaration.setHorizontalTextPosition(JLabel.CENTER);

        /* ActionListeners for the JButtons */

        rockButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
              /*I have not placed any code in here because I have not gotten that far*/
            }
        });

        scissorsButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
               /*I have not placed any code in here because I have not gotten that far*/
            }               
        });

        paperButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) 
            {
               /*I have not placed any code in here because I have not gotten that far*/
            }   
        }); 
    }
}

我不能在我的 ActionListeners 中使用 setText 方法,如下所示,因为它与我的主类方法冲突,位于我的类声明下方的一行。他们都在参数中使用字符串。

public static void main(String []args) {

    rockButton.addActionListener(new ActionListener() {
        setText(String text) {
        }
    }
}

我的最终想法是创建另一个可以使用 setText 方法来更改该框架上的 JLabel 的类。但是,由于 JLabels 不能像变量或方法一样从一个类调用到另一个类,所以我在尝试实现这个想法时遇到了麻烦。

【问题讨论】:

  • 为它创建一个getter方法。这是oop Programming 101。在编写 GUI 时,程序员应该不需要问这类问题。
  • 将您的问题分解为逻辑单元,围绕这些单元构建工作单元以及支持它的 UI。例如,您可以创建一个 PromptPane 来提供您需要的功能以及围绕它的支持组件

标签: java swing oop jbutton actionlistener


【解决方案1】:

有一个单独的方法...

public static void settext() {
    setText(String text)
}

然后在你的动作监听器中调用它...

rockButton.addActionListener(new ActionListener() { 
            public void actionPerformed(ActionEvent e) { 
                settext();
            } 
        });

我希望这就是你的意思! :)

【讨论】:

  • 这是个好主意;但是,当我将该方法复制并粘贴到我的程序中时,它似乎不起作用
  • 是的,我把它放在main方法之外,一复制粘贴,就显示编译器错误。