2012-07-18 102 views
1

我的主类代码,问题是,当变量的变化形成的其他类我的文本字段不会更新,它不断变化的变量呢,它只是场将不会更新文本字段不会更新

//frame set up 
    super("Calculator"); 
    setSize(500, 500); 
    setResizable(false); 
    super.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
    super.setVisible(true); 

    JPanel main = new JPanel(); 
    JPanel stats = new JPanel(); 

    //layouts 

    GridLayout mainV = new GridLayout(5, 5); 
    GridLayout statsV = new GridLayout(2, 3); 
    setLayout(mainV); 

    //setting up textArea 

    JTextField health = new JTextField("Health: " + var.health); //WONT UPDATE!!!!!!!!!!!!!(But varibles do, not the textField) 
    JTextField energy = new JTextField("Energy: " + var.energy); //WONT UPDATE!!!!!!!!!!!!!(But varibles do, not the textField) 
    JTextField land = new JTextField("Land: " + var.land); //WONT UPDATE!!!!!!!!!!!!!(But varibles do, not the textField) 
    JTextField cash = new JTextField("Cash: " + var.cash); //WONT UPDATE!!!!!!!!!!!!!(But varibles do, not the textField) 
    JTextField bullets = new JTextField("Bullets " + var.bullets); //WONT UPDATE!!!!!!!!!!!!!(But varibles do, not the textField) 
    JTextField update = new JTextField(); //UNUSED... 

    //setting up editible 

    update.setEditable(false); 
    health.setEditable(false); 
    energy.setEditable(false); 
    land.setEditable(false); 
    cash.setEditable(false); 
    bullets.setEditable(false); 
    update.setEditable(false); 

    //setting up the text 

    JButton Attack = new JButton("Attack"); 
    Attack.addActionListener(new ActionListener() { 
     Attack attack = new Attack(); 

     public void actionPerformed(ActionEvent e) { 
      Attack(); 
     } 

     private void Attack() { 
      if (var.energy >= 5) { 
       var.energy -= 5; 
       var.cash += 100; 
       var.bullets -= 1; 
      } else { 
       var.health -= 1; 
      } 
     } 
    }); 
    JButton Rest = new JButton("Rest"); 
    Rest.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      Rest(); 
     } 

     private void Rest() { 
      var.energy += 10; 
      var.cash -= 50; 
     } 
    }); 
    JButton SellLand = new JButton("Sell Land"); 
    SellLand.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      sellLand(); 
     } 

     private void sellLand() { 
      var.land -= 1; 
      var.cash += 500; 
      System.out.println("Working"); 
      System.out.println(var.cash + " " + var.land); 
     } 
    }); 
    JButton BuyBullets = new JButton("Buy Bullets"); 
    BuyBullets.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      BuyBullets(); 
     } 

     private void BuyBullets() { 
      var.cash -= 100; 
     } 
    }); 

    ButtonGroup actions = new ButtonGroup(); 

    add(health); 
    add(energy); 
    add(bullets); 
    add(land); 
    add(cash); 
    add(Attack); 
    add(Rest); 
    add(SellLand); 
    add(BuyBullets); 
    setVisible(true); 

    String SHealth = "Health: " + var.health; 
    String SEnergy = "Energy: " + var.energy; 
    String SLand = "Land: " + var.land; 
    String SCash = "Cash: " + var.cash; 
    String SBullets = "Bullet(s): " + var.bullets; 

    land.setText(SLand); 
    cash.setText(SCash); 
    bullets.setText(SBullets); 
} 

public static void main(String[] args) { 
    new Main(); 
} 

可变因素CLASS

int health = 3; 
int energy = 15; 
int land = 1; 
int cash = 100; 
int bullets = 5; 

请帮助我的文本字段将不会更新,但变量发生变化,文本字段不会改变(在编程即时通讯新,所以请说明):d谢谢!

+0

你必须从int到字符串显式转换? – 2012-07-18 04:24:53

+0

对不起,这是什么意思...我是一个新手 – 2012-07-18 04:28:52

+0

请调试它 – Conan 2012-07-18 04:29:34

回答

2

这是完全预期的。 JTextField对你的变量一无所知。当你构建JTextField的:

new JTextField("Health: " + var.health) 

你只是传递一个字符串,它由“生”加var.health的当前值的JTextField的大小。如果var.health稍后改变,那么JTextField不知道。

如果你想要的文字更新,它是由你来监测变量,而当他们改变,更新JTextField中的文本:

// Call this whenever the variables change 
private void updateText() { 
    health.setText("Health: " + var.health); 
    ... 
} 
2

你需要更新你的文本框的文本使用settext方法。

更新类

import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.ButtonGroup; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 
import javax.swing.SwingUtilities; 
import javax.swing.WindowConstants; 

public class TestFrame extends JFrame { 

    private static final long serialVersionUID = 1L; 
    private Var var = new Var(); 

    JTextField health; 
    JTextField energy; 
    JTextField land; 
    JTextField cash; 
    JTextField bullets; 
    JTextField update ; 
    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       TestFrame thisClass = new TestFrame(); 
       thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       thisClass.setVisible(true); 
      } 
     }); 
    } 

    /** 
    * This is the default constructor 
    */ 
    public TestFrame() { 
     super("Calculator"); 
     setSize(500, 500); 
     setResizable(false); 
     super.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
     super.setVisible(true); 

     JPanel main = new JPanel(); 
     JPanel stats = new JPanel(); 

     //layouts 

     GridLayout mainV = new GridLayout(5, 5); 
     GridLayout statsV = new GridLayout(2, 3); 
     setLayout(mainV); 

     //setting up textArea 

     health = new JTextField("Health: " + var.health); //WONT UPDATE!!!!!!!!!!!!!(But varibles do, not the textField) 
     energy = new JTextField("Energy: " + var.energy); //WONT UPDATE!!!!!!!!!!!!!(But varibles do, not the textField) 
     land = new JTextField("Land: " + var.land); //WONT UPDATE!!!!!!!!!!!!!(But varibles do, not the textField) 
     cash = new JTextField("Cash: " + var.cash); //WONT UPDATE!!!!!!!!!!!!!(But varibles do, not the textField) 
     bullets = new JTextField("Bullets " + var.bullets); //WONT UPDATE!!!!!!!!!!!!!(But varibles do, not the textField) 
     update = new JTextField(); //UNUSED... 

     //setting up editible 

     update.setEditable(false); 
     health.setEditable(false); 
     energy.setEditable(false); 
     land.setEditable(false); 
     cash.setEditable(false); 
     bullets.setEditable(false); 
     update.setEditable(false); 

     //setting up the text 

     JButton Attack = new JButton("Attack"); 
     Attack.addActionListener(new ActionListener() { 
      // Attack attack = new Attack(); 

      public void actionPerformed(ActionEvent e) { 
       Attack(); 
      } 

      private void Attack() { 
       if (var.energy >= 5) { 
        var.energy -= 5; 
        var.cash += 100; 
        var.bullets -= 1; 
       } else { 
        var.health -= 1; 
       } 
       updateTextFields(); 
      } 
     }); 
     JButton Rest = new JButton("Rest"); 
     Rest.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       Rest(); 
      } 

      private void Rest() { 
       var.energy += 10; 
       var.cash -= 50; 
       updateTextFields(); 
      } 
     }); 
     JButton SellLand = new JButton("Sell Land"); 
     SellLand.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       sellLand(); 
      } 

      private void sellLand() { 
       var.land -= 1; 
       var.cash += 500; 
       System.out.println("Working"); 
       System.out.println(var.cash + " " + var.land); 
       updateTextFields(); 
      } 
     }); 
     JButton BuyBullets = new JButton("Buy Bullets"); 
     BuyBullets.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       BuyBullets(); 
      } 

      private void BuyBullets() { 
       var.cash -= 100; 
       updateTextFields(); 
      } 
     }); 

     ButtonGroup actions = new ButtonGroup(); 

     add(health); 
     add(energy); 
     add(bullets); 
     add(land); 
     add(cash); 
     add(Attack); 
     add(Rest); 
     add(SellLand); 
     add(BuyBullets); 
     setVisible(true); 

     String SHealth = "Health: " + var.health; 
     String SEnergy = "Energy: " + var.energy; 
     String SLand = "Land: " + var.land; 
     String SCash = "Cash: " + var.cash; 
     String SBullets = "Bullet(s): " + var.bullets; 

     land.setText(SLand); 
     cash.setText(SCash); 
     bullets.setText(SBullets); 
    } 

    private void updateTextFields() 
    { 
     health.setText("Health: " + var.health); //WONT UPDATE!!!!!!!!!!!!!(But varibles do, not the textField) 
     energy.setText("Energy: " + var.energy); //WONT UPDATE!!!!!!!!!!!!!(But varibles do, not the textField) 
     land.setText("Land: " + var.land); //WONT UPDATE!!!!!!!!!!!!!(But varibles do, not the textField) 
     cash.setText("Cash: " + var.cash); //WONT UPDATE!!!!!!!!!!!!!(But varibles do, not the textField) 
     bullets.setText("Bullets " + var.bullets); //WONT UPDATE!!!!!!!!!!!!!(But varibles do, not the textField) 
     update.setText(""); //UNUSED... 
    } 




} 

您瓦尔类

public class Var { 
    int health = 3; 
    int energy = 15; 
    int land = 1; 
    int cash = 100; 
    int bullets = 5; 
}