简体   繁体   English

Java Swing使用两个JPanel处理变量

[英]Java Swing working with a variable using two JPanels

I have two panels, a control and a Battleship panel. 我有两个面板,一个控制面板和一个战舰面板。 The control panel has a submit button which takes in the location of the position that the user wants to attack. 控制面板上有一个提交按钮,该按钮可以获取用户想要攻击的位置的位置。 The Battleship panel then evaluates the location to see if it's a hit or miss under a method called check. 然后,“战舰”面板通过一种称为“检查”的方法来评估位置,以查看是命中还是未命中。

How do I add in a counter variable that limits the number of times the user can submit a location? 如何添加计数器变量以限制用户可以提交位置的次数? I had a counter variable in Control Panel but I was manipulating it through check method in Battleship. 我在“控制面板”中有一个计数器变量,但是我在Battleship中通过检查方法来操纵它。 However, check was also being called in a TimerListener in Battleship and my counter decemented too quickly because check was constantly being called. 但是,在战舰的TimerListener中也调用了check,并且我的计数器下降得太快,因为不断调用check。

Edit: hopefully cleared up my question 编辑:希望能解决我的问题

Put the counter in control panel. 将计数器放在控制面板中。 Check it's value and decrement it while calling check method when submit button is pressed. 在按下提交按钮时调用检查方法时,检查它的值并减少它。

// Members of control panel class
BattleShipPanel battleShipPanel; // Fetch value through constructor or setter
int counter = 10;

public void submitButtonClicked(Event evt) {
    if (counter > 0) {
        int x, y;

        // Take input in x and y here

        counter--;
        battleShipPanel.check(x, y);
    }
}

If I understood you correctly, then you want your counter variable should be used in two panels. 如果我对您的理解正确,那么您希望在两个面板中使用您的counter变量。

In java you have 4 type of access specifiers 在Java中,您有4种类型的访问说明符

1. default or package level - Accessible in any class within the same package 1.默认或包级别-在同一包内的任何类中均可访问

2. private - Accessible within the class where it has been declared, no where else 2. private-在声明了它的类中可以访问,在其他地方不能访问

3. protected - Accessible in any class within the same package and in any subclass in some other package 3. protected-可在同一包中的任何类中以及在某些其他包中的任何子类中访问

4. public - Accessible in any class in the same or different package. 4. public-在相同或不同包中的任何类中均可访问。

The best options is to make your variables private. 最好的选择是将变量设为私有。

Sample Example 样例

public class ClassName {

   private Object obj;

   public Object getObject(){
        return obj;
   }

   public void setObject(Object obj){
        this.obj = obj;
   }

}

As obj is used in two methods you can use your variable in two panels. 由于在两种方法中都使用了obj因此可以在两个面板中使用变量。

I figured it out! 我想到了! I had the counter variable in my Control Panel and just decremented it under the ButtonListener I had. 我在控制面板中有一个计数器变量,只是在我拥有的ButtonListener下将其递减。 Then it would update every time the user had clicked the submit button. 然后,它将在用户每次单击“提交”按钮时进行更新。 I used what Tanmay Patil had and modified it a bit. 我使用了Tanmay Patil所拥有的东西,并对其进行了一些修改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM