2015-11-12 141 views
-1

我添加了我的按钮到ActionListener,并且按钮似乎没有做任何事情,因为我在按下按钮时分配了一个任务,我甚至要求它打印一些东西。我正在尝试在gui中制作一款股票游戏。我有一本书教我如何做到这一点,我不知道为什么它不工作。按钮没有响应ActionListener

package stock.gui.game; 
import javax.swing.*; 
import java.util.Random; 
import java.lang.Thread; 
import java.awt.Font; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.math.*; 



public class StockGUIGame extends JFrame implements ActionListener{ 

    int linecount = 0; 
    float[] stockpricebefore = {20.00f , 1090.00f , 60.00f , 35.00f , 160.00f, 70.00f, 95.00f, 15.00f }; 
    int[] downupdecider2 = {0, 0, 0, 0, 0, 0, 0, 0}; 
    float[] downup2 = {0, 0, 0, 0, 0, 0, 0, 0}; 
    //int linecount = 0; 
    float netwoth = 0; 
    String[] names = {"Universe Express", "Digital Internet", "Garstump Motors", "Freedom Frinature", "Walter's Waters", "Tiger Toys", "Healthy Tastes", "Niagra Drinks"}; 
    float[] stockpricestart = {20.00f , 1090.00f , 60.00f , 35.00f , 160.00f, 70.00f, 95.00f, 15.00f }; 
    float[] stockprice = {20.00f , 1090.00f , 60.00f , 35.00f , 160.00f, 70.00f, 95.00f, 15.00f }; 
    int stockbuytrack = 0; 
    int[] stocktobuy = {100, 0, 0, 0, 0, 0, 0, 0}; 
    int[] stockbuy = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 
    int stocknow = 0; 
    int days = 0; 
    byte downupcounter[] = {1, 1, 1, 1, 1, 1, 1, 1}; 
    Random ran = new Random(); 
    float[] downup = {0, 0, 0, 0, 0, 0, 0, 0}; 
    float finalstockprice = 0.00f; 
    int[] downupdecider = {0, 0, 0, 0, 0, 0, 0, 0}; 
    float[] downupmax = {6.00f, 4.00f, 4.00f, 4.00f, 0.1f, 8.00f, 10.00f, 4.00f}; 
    float[] downuptwomax = {3.00f, 2.00f, 2.00f, 2.00f, 0f, 4.00f, 5.00f, 2.00f}; 
    float[] downupsmall = {1.50f, 0.5f, 1.50f, 3.5f, 0.20f, 2.50f, 1.50f, 1.50f}; 
    float[] downuptwosmall = {0.75f, 0.25f, 0.50f, 1.50f, 0.10f, 1.00f, 0.50f, 0.50f}; 
    int[] stocknumber; 
    int sharesbuy[] = {0, 0, 0, 0, 0, 0, 0, 0}; 
    static float money = 1000.00f; 
    String[] multiplier = {"1", "10", "100", "1,000", "10,000", "100,000", "1,000,000"}; 
    float assets = 0.00f; 

    //JLabel lab = new JLabel("Universe Express | Digital Internet | Garstump Motors | Freedom Frinature | Walter's Waters | Tiger Toys | Healthy Tastes | Niagra Drinks | Net Worth    | Cash    |"); 
    JSlider stockslider = new JSlider(0, 10); 
    JTextArea ss1 = new JTextArea(30, 216); 
    JScrollPane s1 = new JScrollPane(ss1, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); 
    JComboBox multi = new JComboBox(names); 
    JComboBox mul = new JComboBox(multiplier); 
    JButton buy = new JButton("Buy"); 
    JButton sell = new JButton("Sell"); 
    JCheckBox all = new JCheckBox("All"); 

    public StockGUIGame() throws InterruptedException{ 
     super("Stock Game"); 
     setSize(1550, 650); 
     setVisible(true); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     JPanel pane = new JPanel(); 
     stockslider.setMajorTickSpacing(10); 
     stockslider.setMajorTickSpacing(1); 
     stockslider.setPaintTicks(true); 
     stockslider.setPaintLabels(true); 
     Font font = new Font("Courier New", Font.PLAIN, 12); 
     ss1.setFont(font); 
     //pane.add(lab); 
     pane.add(s1); 
     pane.add(buy); 
     pane.add(sell);   
     pane.add(multi); 
     pane.add(mul); 
     pane.add(stockslider); 
     pane.add(all); 
     add(pane); 
     all.addActionListener(this); 
     ss1.append("Universe Express | Digital Internet | Garstump Motors | Freedom Frinature | Walter's Waters | Tiger Toys | Healthy Tastes | Niagra Drinks | Days       | Net Worth    | Cash    |\n"); 

     for (int l = 1; l == 1;){ 

      Thread.sleep(900); 
      days = days + 1; 
      int tu = stockslider.getValue(); 
      String op = String.format("%d", tu); 
      formula(); 
      print(); 
      ss1.setCaretPosition(ss1.getDocument().getLength()); 

     } 

    } 

    public void actionPerformed(ActionEvent evt){ 
     Object source = evt.getSource(); 
     if (source == buy){ 
      if ((stockprice[multi.getSelectedIndex()]) * (((int) Math.pow(10, (mul.getSelectedIndex())/10)) * stockslider.getValue()) < money){ 
       linecount = linecount - 1; 
       ss1.append("Not enough money! Can buy" + Math.round((money - (money % stockprice[multi.getSelectedIndex()]))/stockprice[multi.getSelectedIndex()])); 
      } 
      else if (all.isSelected() == true){ 
       stocktobuy[multi.getSelectedIndex()] = stocktobuy[multi.getSelectedIndex()] + Math.round((money - (money % stockprice[multi.getSelectedIndex()]))/stockprice[multi.getSelectedIndex()]); 
       ss1.append("You bought " + Math.round((money - (money % stockprice[multi.getSelectedIndex()]))/stockprice[multi.getSelectedIndex()]) + " stocks of " + names[multi.getSelectedIndex()] + " at " + stockprice[multi.getSelectedIndex()] + " a share"); 
       linecount = linecount - 1; 
       money = money - (stocktobuy[multi.getSelectedIndex()] * stockprice[multi.getSelectedIndex()]); 
      } 
      else{ 
       ss1.append("You bought " + ((int) Math.pow(10, (mul.getSelectedIndex())/10)) * stockslider.getValue() + " stocks of " + names[multi.getSelectedIndex()] + " at " + stockprice[multi.getSelectedIndex()] + " a share"); 
       stocktobuy[multi.getSelectedIndex()] = ((int) Math.pow(10, (mul.getSelectedIndex())/10)) * stockslider.getValue(); 
       money = money - (stocktobuy[multi.getSelectedIndex()] * stockprice[multi.getSelectedIndex()]); 
       linecount = linecount - 1; 
      } 

     } 
     else if (source == sell){ 
      if (stocktobuy[multi.getSelectedIndex()] + 1 < ((((int) Math.pow(10, (mul.getSelectedIndex())/10)) * stockslider.getValue() * stockprice[multi.getSelectedIndex()]))){ 
       linecount = linecount - 1; 
      } 
      else if (all.isSelected() == true){ 
       ss1.append("You sold " + Math.round((money - (money % stockprice[multi.getSelectedIndex()]))/stockprice[multi.getSelectedIndex()]) + " stocks of " + names[multi.getSelectedIndex()] + " at " + stockprice[multi.getSelectedIndex()] + " a share");  
       money = money + (stocktobuy[multi.getSelectedIndex()] * stockprice[multi.getSelectedIndex()]); 
       stocktobuy[multi.getSelectedIndex()] = 0; 
       linecount = linecount - 1; 
      } 
      else{ 
       ss1.append("You sold " + ((int) Math.pow(10, (mul.getSelectedIndex())/10)) * stockslider.getValue() + " stocks of " + names[multi.getSelectedIndex()] + " at " + stockprice[multi.getSelectedIndex()] + " a share"); 
       money = money + ((((int) Math.pow(10, (mul.getSelectedIndex())/10)) * stockslider.getValue() * stockprice[multi.getSelectedIndex()])); 
       stocktobuy[multi.getSelectedIndex()] = 0; 
       linecount = linecount - 1; 
      } 
     } 
    } 


    private static void setLookAndFeel(){ 
     try{ 
      UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NinbusLookAndFeel"); 
     } 
     catch(Exception exc){ 
      //ignore error 
     } 
    } 

    public static void main(String[] args) throws InterruptedException { 
     setLookAndFeel(); 
     StockGUIGame sguig = new StockGUIGame(); 

    } 

    public void formula(){ 

     for (int b = 0; b < 8; b++){ 
      if (downupcounter[b] < 1){ 
       stockpricebefore[b] = stockprice[b]; 
      } 
      downupdecider[b] = ran.nextInt(20); 
      if (stockprice[b] > stockpricestart[b] * (1 + (days/400.00f)) * 1.75f){ 
       downupdecider[b] = 10; 
      } 
      if (downupdecider[b] == 10){ 
       downup[b] = ((ran.nextFloat() * downupmax[b]) * ((stockpricestart[b] * (1 + (days/400.00f)))/stockprice[b])) - downuptwomax[b]; 
       if (downup[b] > 1.5f * downupmax[b]){ 
        downup[b] = (float)downupmax[b]; 
       } 
      } 

      if (downupcounter[b] == 1){   
       downup[b] = (ran.nextFloat() * downupmax[b]) - downuptwomax[b] ; 
       downupcounter[b] = 0; 
      } 

      downupdecider2[b] = ran.nextInt(4); 

      if (downupdecider2[b] == 2){ 
       downup2[b] = (ran.nextFloat() * (downupmax[b] * 2.00f)) - (downuptwomax[b] * 2) ; 
      } 

      if (downupcounter[b] == 1){   
       downup2[b] = (ran.nextFloat() * (downupmax[b] * 2.00f)) - (downuptwomax[b] * 2); 
       downupcounter[b] = 0; 
      } 




      finalstockprice = ((((downup[b] + downup2[b]) + (ran.nextFloat() * (downupsmall[b]) - downuptwosmall[b])) * stockprice[b])/100.00f) + stockprice[b]; 
      stockprice[b] = Math.round(finalstockprice * 100)/100.00f ; 

      if (ran.nextInt(99) == 59){ 
       stockprice[b] = Math.round((stockprice[b] - ((ran.nextFloat()/4.00f) * stockprice[b])) * 100)/100.00f; 
      } 
      if (ran.nextInt(99) == 45){ 
       stockprice[b] = Math.round((stockprice[b] + ((ran.nextFloat()/4.00f) * stockprice[b])) * 100)/100.00f; 
      } 


     } 



    } 

    public void print(){ 


     ss1.append("$"); 
     if (stockprice[0] < stockpricebefore[0]) 
      ss1.append(stockprice[0] + "-"); 
     else if (stockprice[0] > stockpricebefore[0]){ 
      ss1.append(stockprice[0] + "+"); 
     } 
     else{ 
      ss1.append(stockprice[0] + "="); 
     } 
     for (int c = 0; c < 16 - (Float.toString(stockprice[0]).length()) - 1; c++){ 
      ss1.append(" "); 
     } 
     ss1.append("|"); 
     ss1.append("$"); 
     if (stockprice[1] < stockpricebefore[1]) 
      ss1.append(stockprice[1] + "-"); 
     else if (stockprice[1] > stockpricebefore[1]){ 
      ss1.append(stockprice[1] + "+"); 
     } 
     else{ 
      ss1.append(stockprice[1] + "="); 
     } 
     for (int c = 0; c < 17 - (Float.toString(stockprice[1]).length()) - 1; c++){ 
      ss1.append(" "); 
     } 
     ss1.append("|"); 
     ss1.append("$"); 
     if (stockprice[2] < stockpricebefore[2]) 
      ss1.append(stockprice[2] + "-"); 
     else if (stockprice[2] > stockpricebefore[2]){ 
      ss1.append(stockprice[2] + "+"); 
     } 
     else{ 
      ss1.append(stockprice[2] + "="); 
     } 
     for (int c = 0; c < 16 - (Float.toString(stockprice[2]).length()) - 1; c++){ 
      ss1.append(" "); 
     } 
     ss1.append("|"); 
     ss1.append("$"); 
     if (stockprice[3] < stockpricebefore[3]) 
      ss1.append(stockprice[3] + "-"); 
     else if (stockprice[3] > stockpricebefore[3]){ 
      ss1.append(stockprice[3] + "+"); 
     } 
     else{ 
      ss1.append(stockprice[3] + "="); 
     } 
     for (int c = 0; c < 18 - (Float.toString(stockprice[3]).length()) - 1; c++){ 
      ss1.append(" "); 
     } 
     ss1.append("|"); 
     ss1.append("$"); 
     if (stockprice[4] < stockpricebefore[4]) 
      ss1.append(stockprice[4] + "-"); 
     else if (stockprice[4] > stockpricebefore[4]){ 
      ss1.append(stockprice[4] + "+"); 
     } 
     else{ 
      ss1.append(stockprice[4] + "="); 
     } 
     for (int c = 0; c < 16 - (Float.toString(stockprice[4]).length()) - 1; c++){ 
      ss1.append(" "); 
     } 
     ss1.append("|"); 
     ss1.append("$"); 
     if (stockprice[5] < stockpricebefore[5]) 
      ss1.append(stockprice[5] + "-"); 
     else if (stockprice[5] > stockpricebefore[5]){ 
      ss1.append(stockprice[5] + "+"); 
     } 
     else{ 
      ss1.append(stockprice[5] + "="); 
     } 
     for (int c = 0; c < 11 - (Float.toString(stockprice[5]).length()) - 1; c++){ 
      ss1.append(" "); 
     } 
     ss1.append("|"); 
     ss1.append("$"); 
     if (stockprice[6] < stockpricebefore[6]) 
      ss1.append(stockprice[6] + "-"); 
     else if (stockprice[6] > stockpricebefore[6]){ 
      ss1.append(stockprice[6] + "+"); 
     } 
     else{ 
      ss1.append(stockprice[6] + "="); 
     } 
     for (int c = 0; c < 15 - (Float.toString(stockprice[6]).length()) - 1; c++){ 
      ss1.append(" "); 
     } 
     ss1.append("|"); 
     ss1.append("$"); 
     if (stockprice[7] < stockpricebefore[7]) 
      ss1.append(stockprice[7] + "-"); 
     else if (stockprice[7] > stockpricebefore[7]){ 
      ss1.append(stockprice[7] + "+"); 
     } 
     else{ 
      ss1.append(stockprice[7] + "="); 
     } 
     for (int c = 0; c < 14 - (Float.toString(stockprice[7]).length()) - 1; c++){ 
      ss1.append(" "); 
     } 
     ss1.append("|"); 
     ss1.append(" "); 
     ss1.append("Day: " + days); 
     for (int c = 0; c < 30 - (Float.toString(days).length()) - 4; c++){ 
      ss1.append(" "); 
     } 
     ss1.append("|"); 
     ss1.append(" "); 
     ss1.append("$"); 
     assets = (money + (stocktobuy[0] * stockprice[0]) + (stocktobuy[1] * stockprice[1]) + (stocktobuy[2] * stockprice[2]) + (stocktobuy[3] * stockprice[3]) + (stocktobuy[4] * stockprice[4]) + (stocktobuy[5] * stockprice[5]) + (stocktobuy[6] * stockprice[6]) + (stocktobuy[7] * stockprice[7])); 
     String str1 = Float.toString(money); 
     String str2 = Float.toString(assets); 
     ss1.append(str2); 
     for (int c = 0; c < 22 - (Float.toString(assets).length()) - 1; c++){ 
      ss1.append(" "); 
     } 
     ss1.append("|"); 
     ss1.append(" "); 
     ss1.append("$"); 
     ss1.append(str1); 
     for (int c = 0; c < 20 - (Float.toString(assets).length()) - 1; c++){ 
      ss1.append(" "); 
     } 
     ss1.append("|"); 
     linecount = linecount + 1; 
     ss1.append("\n"); 
     if (linecount == 28){ 
      linecount = 0; 
      ss1.append("Universe Express | Digital Internet | Garstump Motors | Freedom Frinature | Walter's Waters | Tiger Toys | Healthy Tastes | Niagra Drinks | Days       | Net Worth    | Cash    |\n"); 
     } 

    } 



} 
+0

一本书教你的语法'为(int类型l = 1,L == 1)'?这与'while(true)'是一样的,因为你不更新循环中的变量。 –

回答

0

您需要将ActionListener添加到按钮。

你做all.addActionListener(this)但这是一个复选框,你不检查actionPerformed中的来源。

我想你需要

buy.addActionListener(this); 
sell.addActionListener(this);