我的主類代碼,問題是,當變量的變化形成的其他類我的文本字段不會更新,它不斷變化的變量呢,它只是場將不會更新文本字段不會更新
//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謝謝!
你必須從int到字符串顯式轉換? – 2012-07-18 04:24:53
對不起,這是什麼意思...我是一個新手 – 2012-07-18 04:28:52
請調試它 – Conan 2012-07-18 04:29:34