簡體   English   中英

如何在 actionListener(actionEvent e) 中聲明 JLabels 以便在單擊 JButton 時設置 Text

[英]how to declare JLabels within actionListener(actionEvent e) so that Text can be set when JButton is clicked

我正在編寫一個剪刀石頭布游戲,我試圖讓每個 JLabels 在三個按鈕(Rock、Paper、Scissors)中的一個被點擊時更新。

這樣做的問題是每個 JLabels 都在 createWindow() 中定義,我需要它們也在 ActionListener(ActionEvent e) 中定義。

我試圖從 createWindow() 和 JLabels 更新中復制粘貼一些代碼,但唯一的問題是它創建了一個全新的 window 沒有按鈕(僅包含文本)。 如果我嘗試在主要方法之外實例化標簽,則 JLabels 根本不會出現。

感謝任何幫助(無論我是否必須重新編寫整個程序),我是 Java 和 Swing 的新手,任何批評都會以某種方式有所幫助。 (我在 Eclipse IDE 中寫作)。

  import java.awt.*;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import javax.swing.*;

  public class RockPaperScissors extends JPanel implements ActionListener {
public static void main(String[] args) {
    RockPaperScissors sm = new RockPaperScissors();
    sm.createWindow();
}
private void createWindow() {
    // create JFrame/JPanel
    JFrame frame = new JFrame("Rock! Paper! Scissors!");
    JPanel panel = new JPanel();
    JLabel title = new JLabel("Rock! Paper! Scissors!");
    JLabel playerchoice = new JLabel("");
    JLabel computerdecision = new JLabel("");
    JLabel outcome = new JLabel("");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    frame.pack();
    frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    JButton button1 = new JButton("Rock");
    panel.add(button1);
    button1.addActionListener(this);
    button1.setBounds(480, 500, 100, 50);
    JButton button2 = new JButton("Paper");
    panel.add(button2);
    button2.addActionListener(this);
    button2.setBounds(590, 500, 100, 50);
    JButton button3 = new JButton("Scissors");
    panel.add(button3);
    button3.addActionListener(this);
    button3.setBounds(700, 500, 100, 50);
    frame.add(panel);
    panel.setLayout(new BorderLayout());
    GroupLayout layout = new GroupLayout(this);
    this.setLayout(layout);
    panel.add(title);
    panel.add(playerchoice);
    panel.add(computerdecision);
    panel.add(outcome);
    outcome.setBounds(571, 90, 260, 200);
    computerdecision.setBounds(571, 100, 260, 200);
    playerchoice.setBounds(571, 120, 260, 200);
    title.setBounds(571, 0, 260, 200);
}
     public void actionPerformed(ActionEvent e) {
    // JLabels
    int choice = 0;
    int computerchoice = 0;
    int min = -1;
    int max = -3;
    computerchoice = (int) (Math.random() * (max - min - 1) + min);
    if (computerchoice == -1) {
        panel.add(computerdecision);
        computerdecision.setText("The Computer Chose Rock!");
        // for checking
        System.out.println("The Computer Chose Rock!");
    } else if (computerchoice == -2) {
        panel.add(computerdecision);
        computerdecision.setText("The Computer Chose Paper!");
        // for checking
        System.out.println("The Computer Chose Paper!");
    } else if (computerchoice == -3) {
        panel.add(computerdecision);
        computerdecision.setText("The Computer Chose Scissors!");
        // for checking
        System.out.println("The Computer chose Scissors!");
    }
    if (e.getActionCommand().equals("Rock")) {
        choice = 1;
        panel.add(playerchoice);
        playerchoice.setText("You chose Rock!");
        // for checking
        System.out.println("You chose Rock!");
    } else if (e.getActionCommand().equals("Paper")) {
        choice = 2;
        panel.add(playerchoice);
        playerchoice.setText("You chose Paper!");
        // for checking
        System.out.println("You chose Paper!");
    } else if (e.getActionCommand().equals("Scissors")) {
        choice = 3;
        panel.add(playerchoice);
        playerchoice.setText("You chose Scissors!");
        // for checking
        System.out.println("You chose Scissors!");
    }
    if (choice + computerchoice == 0) {
        panel.add(outcome);
        outcome.setText("It is a Tie!");
        // for checking
        System.out.println("It is a Tie!");
    } else if (choice + computerchoice == 1 || choice + computerchoice == -2) {
        panel.add(outcome);
        outcome.setText("You Win!");
        // for checking
        System.out.println("You Win!");
    } else if (choice + computerchoice == 2 || choice + computerchoice == -1) {
        panel.add(outcome);
        outcome.setText("You Lose...");
        // for checking
        System.out.println("You Lose...");
    }
}}

解決你的問題的方法很簡單,只需將outcome class 的panel , playerchoice , computerdecisionRockPaperScissors成員改為像這樣的局部變量

public class RockPaperScissors extends JPanel implements ActionListener {
     private JPanel panel;
     private JLabel computerdecision;
     private JLabel playerchoice;
     private JLabel outcome;

     ...

     private void createWindow() {
         JFrame frame = new JFrame("Rock! Paper! Scissors!");
         panel = new JPanel();
         playerchoice = new JLabel("");
         computerdecision = new JLabel("");
         outcome = new JLabel("");
         JLabel title = new JLabel("Rock! Paper! Scissors!");
         ...
     }
     ...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM