Java不断添加按钮! - JFrames -

时间:2014-03-24 21:19:36

标签: java swing model-view-controller jbutton

好吧,所以,我正在尝试使用JFrame制作游戏,当您点击按钮时,会增加资金,并显示更新一些文字(JLabel)。

你看,它应该只更新Windows.Money变量并在屏幕上显示新变量,但是,它会添加更多按钮。请注意:钱部分工作正常。

只是Java不想替换,只添加。

代码:

    package dev.bobdabiulder.classic.main;

import javax.swing.JLabel;

public class Bucket extends Window{

private static final long serialVersionUID = 1L;

public Bucket() throws InterruptedException {

    for(int i = 0; !Window.Paused; ) {
        Window.Money += 2;

            this.wait(1000);

    }

    Window.Buckets++;
    Window.BucketCounter = new JLabel("You have: " + Window.Buckets + " buckets!");


}

}

Windows班级......

package dev.bobdabiulder.classic.main;

import java.awt.Button;
import java.awt.Component;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Window extends JFrame{

public static long Money = 0;
private static final long serialVersionUID = 1L;
private static Thread t1 = new Thread();
private static Thread t2 = new Thread();
private static Thread t3 = new Thread();
private static Thread t4 = new Thread();
private static Thread t5 = new Thread();
public JButton bucket = new JButton("Buy a Bucket!");
public JButton add$ = new JButton("Add Money!");
public JLabel money = new JLabel(Money + "");
public static long Buckets = 0;
public static JLabel BucketCounter = new JLabel("You have: " + Buckets + " buckets!");
public static JPanel buck = new JPanel();
public static boolean Paused = false;
static JFrame jf = new JFrame("Lol!");
//Window Method
public Window() {

    jf.setVisible(true);
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    buck.add(bucket);
    buck.add(money);
    buck.add(add$);
    buck.add(BucketCounter);
    jf.setSize(500, 500);
    jf.add(buck);
    bucket.addActionListener(new ActionListener() {
    @Override
        public void actionPerformed(ActionEvent e) {
            if(e.getSource().equals(bucket)) {
                try {
                    new Bucket();
                } catch (Exception e1) {
                    e1.printStackTrace();
                }

            }
            System.out.println("Action Performed!");


    }
    });
    pack();     

}

//End of ActionPerformed

//Start of start()
public static void start() {
    t1.start();
    t2.start();
    t3.start();
    t4.start();
    t5.start();
}
//
//
//
@SuppressWarnings("deprecation")
public static void stop() {

    t1.stop();
    t2.stop();
    t3.stop();
    t4.stop();
    t5.stop();

}

}

我做了一些请求的编辑,我收到错误,我把错误全部读出来了:

  

Cannot make a static reference to the non-static field Window.(anything)

错误点:

for(int i = 0; !*Window.Paused; ) {
        *Window.Money += 2;

            this.wait(1000);

    }

    *Window.Buckets++;
    *Window.BucketCounter = new JLabel("You have: " + *Window.Buckets + "       buckets!");

2 个答案:

答案 0 :(得分:2)

MVC的简要示例,实际上是模型视图。这样做是使用 Swing Timer 而不是Threads(不是直接)来增加一个不同类中的JLabel。它还使用PropertyChangeListener并支持通知视图(GUI)模型状态的变化。

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.NumberFormat;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.SwingPropertyChangeSupport;

/**
 * http://stackoverflow.com/q/22620807/522444
 * http://stackoverflow.com/a/22621767/522444
 * @author Pete
 *
 */
@SuppressWarnings("serial")
public class ShortMvc extends JPanel {
   private JTextField moneyField = new JTextField(10);
   private JTextField bucketField = new JTextField(10);
   private MoneyModel model = new MoneyModel();
   private Timer timer = new Timer(model.getTimerDelay(), new TimerListener());
   private JButton moneyButton = new JButton("Add Money");
   private JButton bucketButton = new JButton("Add Bucket");

   public ShortMvc() {
      moneyField.setEditable(false);
      moneyField.setFocusable(false);

      bucketField.setEditable(false);
      bucketField.setFocusable(false);

      bucketField.setText(String.valueOf(model.getBuckets()));

      add(new JLabel("Money:"));
      add(moneyField);
      add(moneyButton);
      add(new JLabel("Buckets:"));
      add(bucketField);
      add(bucketButton);

      moneyButton.getModel().addChangeListener(new MoneyBtnModelListener());
      moneyButton.setMnemonic(KeyEvent.VK_M);
      bucketButton.addActionListener(new BucketButtonListener());
      bucketButton.setMnemonic(KeyEvent.VK_B);

      model.addPropertyChangeListener(new ModelListener());
      timer.setInitialDelay(0);
   }

   private class BucketButtonListener implements ActionListener {
      @Override
      public void actionPerformed(ActionEvent e) {
         model.incrementBuckets();
      }
   }

   private class MoneyBtnModelListener implements ChangeListener {
      private boolean pressed = false;

      @Override
      public void stateChanged(ChangeEvent e) {
         ButtonModel model = (ButtonModel) e.getSource();
         if (pressed == model.isPressed()) {
            return;
         }
         pressed = model.isPressed();
         if (pressed) {
            timer.start();
         } else {
            timer.stop();
         }
      }
   }

   private class TimerListener implements ActionListener {
      @Override
      public void actionPerformed(ActionEvent e) {
         model.incrementMoney(model.getMoneyIncrementAmount());
      }
   }

   private class ModelListener implements PropertyChangeListener {
      private NumberFormat moneyFormat = NumberFormat.getCurrencyInstance();

      public ModelListener() {
         moneyField.setText(moneyFormat.format(model.getMoney()));
      }

      @Override
      public void propertyChange(PropertyChangeEvent pcEvt) {
         if (MoneyModel.MONEY.equals(pcEvt.getPropertyName())) {
            moneyField.setText(moneyFormat.format(model.getMoney()));
         }
         else if (MoneyModel.BUCKETS.equals(pcEvt.getPropertyName())) {
            int buckets = model.getBuckets();
            bucketField.setText(String.valueOf(buckets));
            timer.setDelay(model.getTimerDelay());
         }
      }
   }

   private static void createAndShowGui() {
      JFrame frame = new JFrame("Short Mvc");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(new ShortMvc());
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

class MoneyModel {
   public static final String MONEY = "money";
   public static final String BUCKETS = "buckets";
   private static final int INIT_TIMER_DELAY = 500;
   public static final long MONEY_INCREMENT_AMOUNT = 2L;
   private long money = 0L;
   private int buckets = 1;
   private int timerDelay = INIT_TIMER_DELAY;
   private SwingPropertyChangeSupport pcSupport = new SwingPropertyChangeSupport(
         this);

   public void setMoney(long money) {
      long oldValue = this.money;
      long newValue = money;
      this.money = money;
      pcSupport.firePropertyChange(MONEY, oldValue, newValue);
   }

   public long getMoneyIncrementAmount() {
      return MONEY_INCREMENT_AMOUNT;
   }

   public void incrementMoney(long addToMoney) {
      long oldValue = this.money;
      long newValue = money + addToMoney;
      this.money = newValue;
      pcSupport.firePropertyChange(MONEY, oldValue, newValue);
   }

   public long getMoney() {
      return money;
   }

   public void setBuckets(int buckets) {
      int oldValue = this.buckets;
      int newValue = buckets;
      this.buckets = newValue;
      timerDelay = INIT_TIMER_DELAY / buckets;
      pcSupport.firePropertyChange(BUCKETS, oldValue, newValue);
   }

   public void incrementBuckets(int incrementAmount) {
      int newValue = this.buckets + incrementAmount;
      setBuckets(newValue);
   }

   // increment by one
   public void incrementBuckets() {
      incrementBuckets(1);
   }

   public int getBuckets() {
      return buckets;
   }

   public int getTimerDelay() {
      return timerDelay;
   }

   public void addPropertyChangeListener(PropertyChangeListener listener) {
      pcSupport.addPropertyChangeListener(listener);
   }

   public void removePropertyChangeListener(PropertyChangeListener listener) {
      pcSupport.removePropertyChangeListener(listener);
   }

}

请注意,我无法使用ActionListener来实现此功能,因为只有在释放按钮时才会激活ActionListener。我假设你想按下按钮时累积钱,然后在释放时停止累积。为此,您必须提取JButton的模型,向其添加ChangeListener,然后对模型的isPressed()方法的更改做出反应。我用它来启动和停止使模型递增的Swing Timer。


修改
下一次迭代,更好的MVC(模型 - 视图 - 控制)分离:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.NumberFormat;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.SwingPropertyChangeSupport;
import javax.swing.text.JTextComponent;

/**
 * http://stackoverflow.com/q/22620807/522444
 * http://stackoverflow.com/a/22621767/522444
 * @author Pete
 *
 */
public class ShortMvc {

   private static void createAndShowGui() {
      ShortView view = new ShortView();
      MoneyModel model = new MoneyModel();
      ShortControl control = new ShortControl(model, view);
      control.init();

      JFrame frame = new JFrame("Short MVC");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(view.getMainPanel());
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

class ShortView {
   private JTextField moneyField = new JTextField(10);
   private JTextField bucketField = new JTextField(10);
   private JButton moneyButton = new JButton();
   private JButton bucketButton = new JButton();
   private JPanel mainPanel = new JPanel();

   public ShortView() {
      moneyField.setEditable(false);
      moneyField.setFocusable(false);

      bucketField.setEditable(false);
      bucketField.setFocusable(false);

      mainPanel.add(new JLabel("Money:"));
      mainPanel.add(moneyField);
      mainPanel.add(moneyButton);
      mainPanel.add(new JLabel("Buckets:"));
      mainPanel.add(bucketField);
      mainPanel.add(bucketButton);
   }

   public JComponent getMainPanel() {
      return mainPanel;
   }

   public JTextComponent getMoneyField() {
      return moneyField;
   }

   public JTextComponent getBucketField() {
      return bucketField;
   }

   public AbstractButton getMoneyButton() {
      return moneyButton;
   }

   public AbstractButton getBucketButton() {
      return bucketButton;
   }   
}

@SuppressWarnings("serial")
class ShortControl {
   private MoneyModel model;
   private ShortView view;
   private Timer timer;
   private MoneyBtnAction moneyBtnAction = new MoneyBtnAction("Add Money", KeyEvent.VK_M);
   private BucketButtonAction bucketAction = new BucketButtonAction("Add Buckets", KeyEvent.VK_B);

   public ShortControl(MoneyModel model, ShortView view) {
      this.model = model;
      this.view = view;
      timer = new Timer(model.getTimerDelay(), new TimerListener());
   }

   public void init() {
      timer.setInitialDelay(0);
      view.getBucketField().setText(String.valueOf(model.getBuckets()));

      view.getMoneyButton().setAction(moneyBtnAction);
      view.getMoneyButton().getModel().addChangeListener(moneyBtnAction);
      view.getBucketButton().setAction(bucketAction);

      model.addPropertyChangeListener(new ModelListener());
   }

   private class BucketButtonAction extends AbstractAction {
      public BucketButtonAction(String name, int mnemonic) {
         super(name);
         putValue(MNEMONIC_KEY, mnemonic);
      }

      @Override
      public void actionPerformed(ActionEvent e) {
         model.incrementBuckets();
      }
   }

   private class MoneyBtnAction extends AbstractAction implements ChangeListener {
      private boolean pressed = false;

      public MoneyBtnAction(String name, int mnemonic) {
         super(name);
         putValue(MNEMONIC_KEY, mnemonic);
      }

      @Override
      public void actionPerformed(ActionEvent e) {
         // empty
      }

      @Override
      public void stateChanged(ChangeEvent e) {
         ButtonModel model = (ButtonModel) e.getSource();
         if (pressed == model.isPressed()) {
            return;
         }
         pressed = model.isPressed();
         if (pressed) {
            timer.start();
         } else {
            timer.stop();
         }
      }
   }

   private class ModelListener implements PropertyChangeListener {
      private NumberFormat moneyFormat = NumberFormat.getCurrencyInstance();

      public ModelListener() {
         view.getMoneyField().setText(moneyFormat.format(model.getMoney()));
      }

      @Override
      public void propertyChange(PropertyChangeEvent pcEvt) {
         if (MoneyModel.MONEY.equals(pcEvt.getPropertyName())) {
            view.getMoneyField().setText(moneyFormat.format(model.getMoney()));
         }
         else if (MoneyModel.BUCKETS.equals(pcEvt.getPropertyName())) {
            int buckets = model.getBuckets();
            view.getBucketField().setText(String.valueOf(buckets));
            timer.setDelay(model.getTimerDelay());
         }
      }
   }

   private class TimerListener implements ActionListener {
      @Override
      public void actionPerformed(ActionEvent e) {
         model.incrementMoney(model.getMoneyIncrementAmount());
      }
   }

}

class MoneyModel {
   public static final String MONEY = "money";
   public static final String BUCKETS = "buckets";
   private static final int INIT_TIMER_DELAY = 500;
   public static final long MONEY_INCREMENT_AMOUNT = 2L;
   private long money = 0L;
   private int buckets = 1;
   private int timerDelay = INIT_TIMER_DELAY;
   private SwingPropertyChangeSupport pcSupport = new SwingPropertyChangeSupport(
         this);

   public void setMoney(long money) {
      long oldValue = this.money;
      long newValue = money;
      this.money = money;
      pcSupport.firePropertyChange(MONEY, oldValue, newValue);
   }

   public long getMoneyIncrementAmount() {
      return MONEY_INCREMENT_AMOUNT;
   }

   public void incrementMoney(long addToMoney) {
      long oldValue = this.money;
      long newValue = money + addToMoney;
      this.money = newValue;
      pcSupport.firePropertyChange(MONEY, oldValue, newValue);
   }

   public long getMoney() {
      return money;
   }

   public void setBuckets(int buckets) {
      int oldValue = this.buckets;
      int newValue = buckets;
      this.buckets = newValue;
      timerDelay = INIT_TIMER_DELAY / buckets;
      pcSupport.firePropertyChange(BUCKETS, oldValue, newValue);
   }

   public void incrementBuckets(int incrementAmount) {
      int newValue = this.buckets + incrementAmount;
      setBuckets(newValue);
   }

   // increment by one
   public void incrementBuckets() {
      incrementBuckets(1);
   }

   public int getBuckets() {
      return buckets;
   }

   public int getTimerDelay() {
      return timerDelay;
   }

   public void addPropertyChangeListener(PropertyChangeListener listener) {
      pcSupport.addPropertyChangeListener(listener);
   }

   public void removePropertyChangeListener(PropertyChangeListener listener) {
      pcSupport.removePropertyChangeListener(listener);
   }

}

答案 1 :(得分:0)

好的,这应该有点帮助...缺少Timer但你可以了解如何管理ActionListeners:

主要课程

public class WindowInit{
    public static void main(String args[]){
        WindowHelp wh = new WindowHelp();
    }
}

WindowHelp(这个有魔力)

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import javax.swing.event.*;

public class WindowHelp{
    JButton b1;
    JButton b2;
    JLabel label;
    long money;
    long buckets;
    JFrame jf;
    Timer timer;
    public WindowHelp(){
        money = 0;
        buckets = 0;
        b1 = new JButton("Add buckets");
        b2 = new JButton("Add money");
        label = new JLabel("");
        jf = new JFrame("My Game");
        b1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                buckets += 2;
                label.setText("Buckets: " + buckets);
            }
        });
        b1.setBounds(50,50,100,30);
        b2.setBounds(200,50,100,30);
        label.setBounds(300,50,200,30);
        jf.add(b1);
        jf.add(b2);
        jf.add(label);
        jf.setSize(500,600);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

是的,一个是你编译“WindowInit”并运行那个的主类。

对于计时器,我实施时会给我一些时间。