如何使按钮设置变量

时间:2016-03-05 23:44:50

标签: java swing

我正在为游戏编写一些代码,而且我一直在努力让按钮设置变量"武器"。

import static java.lang.System.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class UntitledProject {
    private JFrame mainFrame;
    private JLabel headerLabel;
    private JLabel statusLabel;
    private JPanel controlPanel;

    public UntitledProject() {
        prepareGUI();
    }

    public static void main(String[] args) {
        UntitledProject prepare = new UntitledProject();
        String weapon = prepare.weapon();
    }

    private void prepareGUI() {
        mainFrame = new JFrame("Untitled Project");
        mainFrame.setSize(400,400);
        mainFrame.setLayout(new GridLayout(3, 1));
        mainFrame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent windowEvent) {
                exit(0);
            }
        });
        headerLabel = new JLabel("", JLabel.CENTER);
        statusLabel = new JLabel("", JLabel.CENTER);

        statusLabel.setSize(350, 100);

        controlPanel = new JPanel();
        controlPanel.setLayout(new FlowLayout());

        mainFrame.add(headerLabel);
        mainFrame.add(controlPanel);
        mainFrame.add(statusLabel);
        mainFrame.setVisible(true);
    }
    public String weapon() {
        headerLabel.setText("Pick Weapon");

        JButton button1 = new JButton("Sword");
        JButton button2 = new JButton("Lance");
        JButton button3 = new JButton("Axe");
        JButton submitButton = new JButton("Submit");

        String weapon = "";

        button1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                statusLabel.setText("Sword selected.");
                submitButton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        weapon = "Sword";
                        exit(0);
                    }
                });
            }
        });
        button2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                statusLabel.setText("Lance selected.");
                submitButton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        weapon = "Lance";
                        exit(0);
                    }
                });
            }
        });
        button3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                statusLabel.setText("Axe selected.");
                submitButton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        weapon = "Axe";
                        exit(0);
                    }
                });
            }
        });

        controlPanel.add(button1);
        controlPanel.add(button2);
        controlPanel.add(button3);
        controlPanel.add(submitButton);

        mainFrame.setVisible(true);
    }

我需要在按下提交按钮时将武器变量更改为在提交按钮之前按下的按钮。

2 个答案:

答案 0 :(得分:2)

制作一个实例变量。

private JPanel controlPanel; // under this line 
private String weapon;

然后在其他地方删除weapon前面的字符串,因为这会生成新的本地变量。

然后,您可以删除此行

String weapon = "";

例如,要设置剑,请使用UntitledProject.this.weapon显式设置实例变量。

button1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            statusLabel.setText("Sword selected.");
            UntitledProject.this.weapon = "sword";

对其他按钮执行相同操作。

然后,另外,提交按钮只需要设置一次,而不是每次设置武器。

    button3.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            statusLabel.setText("Axe selected.");
            UntitledProject.this.weapon = "Axe";
        }
    });
    submitButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            exit(0);
        }
     });

并且,这个应用程序只挑选一种武器,然后退出吗?

exit(0);

这会告诉您的整个应用以0(成功)代码退出,而不仅仅是关闭当前窗口

由于武器被移动到实例变量,您可以将其更改为void方法(无论如何你都没有返回任何内容)......更改

public String weapon() {

public void weapon() {

答案 1 :(得分:1)

您需要创建一个临时武器变量来记录之前选择的选项。一旦用户选择武器更新武器变量和临时。点击提交后,将temp分配给武器变量,反之亦然