2016-03-05 51 views
-2

我正在编写一些工作在游戏上,我一直在尝试使按钮设置变量“武器”时遇到了麻烦。如何使一个按钮设置变量

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); 
    } 

我需要时按下按钮,武器变量改为提交按钮之前按下了哪个按钮提交。

回答

1

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

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() { 
相关问题