2017-12-06 4 views
0

私は決勝戦に夢中になり、Javaクラスを紹介した後に簡単なゲームを作るべきだと思っていました。 JFramesの使い方を簡単に学びましたので、私はこれを自分のゲームに使いたいと思っていました。私は単純なクリッカーゲームをクッキークリッカーやその同じジャンルのゲームに似ています。今すぐ私のGUIはthisのように見えます。私のGUIは、thisと似ていて、メニューボタンは中央ボタンの異なる側にあります。私は別のレイアウトマネージャを使用しようとしましたが、私がやっていたことのために働いた人はいませんでした。フローレイアウト、ボックスレイアウト、グリッドレイアウトを試しました。グリッド1は私に最も近いが、それでも全てが同じサイズでグリッドなので、うまくいきませんでしたので、その中央ボタンはできませんでした。誰も私のためのヒントを持っていますか?JFrame(JAVA)で特定のレイアウトを作成したい

これは、これはこれは私の変数に値を設定するためのクラスと、そのような

public class Buying { 
      private int firstBuy$PS; 
      private int firstBuyPrice; 
      private int money = 1; 
      private int clickerStrength = 1; 
      private int clickerPrice =10; 

      public int getFirstBuy$PS() { 
       return firstBuy$PS; 
      } 

      public void setFirstBuy$PS(int x) { 
       firstBuy$PS += x; 
      } 

      public int getFirstBuyPrice() { 
       return firstBuyPrice; 
      } 

      public void setFirstBuyPrice(int x) { 
       firstBuyPrice += x; 
      } 
      public void moneyPS() { 
       money += getFirstBuy$PS(); 
      } 
      public int getMoney() { 
       return money; 
      } 
      public void subtractMoney(int x) { 
       money -= x; 
      } 
      public void addMoney(int x) { 
       money += x; 
      } 
      public void setMoney(int x) { 
       money = x; 
      } 
      public void setClickerStrength() { 
       clickerStrength += 1; 
      } 
      public int getClickerStrength() { 
       return clickerStrength; 
      } 
      public int getClickerPrice() { 
       return clickerPrice; 
      } 
      public void setClickerPrice() { 
       clickerPrice*=1.2323452; 
      } 
     } 

編集で私のゲームクラス

import javax.swing.*; 
    import java.awt.*; 
    import java.awt.event.ActionEvent; 
    import java.awt.event.ActionListener; 
    import java.io.File; 
    import java.io.FileNotFoundException; 
    import java.io.PrintWriter; 
    import java.util.*; 
    import java.util.Timer; 

    public class Game extends JFrame { 
     Buying buy = new Buying(); 
     //creates an instance of SMUQuest 
     private int roundNumber = 1; 

     //widgets 
     JLabel money; 
     JButton clicker; 
     JButton upgradeClicker; 
     JButton firstPurchase; 
     JButton secondPurchase; 
     JButton thirdPurchase; 
     JButton fourthPurchase; 
     JButton fifthPurchase; 
     JButton sixthPurchase; 
     JLabel firstPurchase$PS; 
     JLabel firstPurchasePrice; 
     JLabel firstPerSecond; 



     public Game() { 
      super("Clicker Game"); 
      checkSaveFile(); 
      setLayout(new FlowLayout()); 
      clicker = new JButton("CLICK ME FOR 1 POINT!"); 
      add(clicker); 
      clicker.addActionListener(new MyInner()); 
      upgradeClicker = new JButton("Add 1 to your clicking power for $" + buy.getClickerPrice()); 
      add(upgradeClicker); 
      upgradeClicker.addActionListener(new MyInner()); 
      money = new JLabel("" + buy.getMoney()); 
      add(money); 
      firstPurchasePrice = new JLabel("" + buy.getFirstBuyPrice()); 
      add(firstPurchasePrice); 
      firstPurchase = new JButton("First Property!"); 
      add(firstPurchase); 
      firstPurchase.addActionListener(new MyInner()); 
      firstPurchase$PS = new JLabel("" + buy.getFirstBuy$PS()); 
      add(firstPurchase$PS); 



      //timer to add money every second 
      Timer t = new Timer(); 
      t.schedule(new TimerTask() { 
       @Override 
       public void run() { 
        buy.moneyPS(); 
        money.setText("You have $" + buy.getMoney()); 
       } 
      }, 0, 1000); 
     } 

     private class MyInner implements ActionListener { 
      public void actionPerformed(ActionEvent e) { 
       if (e.getSource() == firstPurchase) { 
        if(buy.getMoney() >= buy.getFirstBuyPrice()) { 
        buy.subtractMoney(buy.getFirstBuyPrice()); 
        buy.setFirstBuy$PS(1); 
        buy.setFirstBuyPrice(2); 
        firstPurchase$PS.setText("$" + buy.getFirstBuy$PS());  
        firstPurchasePrice.setText("$" + buy.getFirstBuyPrice()); 
        money.setText("You have $" + buy.getMoney()); 
        } 
       } 
       if (e.getSource() == clicker) { 
        buy.addMoney(buy.getClickerStrength()); 
        money.setText("You have $" + buy.getMoney()); 
       } 
       if (e.getSource() == upgradeClicker) { 
        if(buy.getMoney() >= buy.getClickerPrice()) { 
        buy.subtractMoney(buy.getClickerPrice()); 
        buy.setClickerPrice(); 
        upgradeClicker.setText("Add 1 to your clicking power for $" + buy.getClickerPrice()); 
        buy.setClickerStrength(); 
        } 
       } 
      } 
     } 
     public void checkSaveFile() { 
      File inputFile = new File("SaveGame.txt"); 
      try { 
       Scanner input = new Scanner(inputFile); 
        int line = input.nextInt(); 
        if(line > 0) 
         buy.setMoney(line); 
      } 
      catch(FileNotFoundException exp) { 
       System.out.println("File not Found Exception. Make sure the input file exists."); 
      } 
     } 
     public void createSaveFile() { 
      File outputFile = new File("SaveGame.txt");//creates a new file 
      try { 
       PrintWriter pWriter = new PrintWriter(outputFile); 
       pWriter.println(buy.getMoney()); 
       pWriter.close(); 
      } 
      catch(FileNotFoundException exp) { 
       System.out.println("File not found."); 
      } 
     } 
    } 

である私のランチャークラス

import java.awt.event.ComponentAdapter; 
import java.awt.event.ComponentEvent; 

import javax.swing.JFrame; 

public class Launcher { 
    public static void main(String[] args) { 
     Game theGame = new Game(); 
     theGame.setSize(650,500); 
     theGame.setVisible(true); 
     theGame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); 
     theGame.addComponentListener(new ComponentAdapter() { 
      @Override 
      public void componentHidden(ComponentEvent e) { 
       theGame.createSaveFile(); 
       System.exit(1); 
      } 
     }); 
    } 
} 

です:コードを入れてください質問には私のギブスへのリンクではなく、

+0

あなたのコードをコピーしてあなたの質問に貼り付け、それをハイライトして 'ctrl + k'を押してください – JackVanier

+0

また、あなたはもっと具体的にする必要があります。あなたのレイアウトを違ったものにしようとしていますか?どのレイアウトマネージャを使用しましたか?どちらがあなたに最も近いのですか?あなたは特定のレイアウトマネージャを好みますか? – roelofs

答えて

0

あなたはGUIで遊ぶことを始めているようだから、スイングのようなGUIマネージャを使うことを強くお勧めします。

あなたのコードで私が見たこと以外は、コンポーネントを作成し、どこに配置するかをプログラムに知らせることなくレイアウトに追加することです。それがあなたのゲームのように見える理由です。

私はGridPaneを使って完全に仕事をすることができると思います。私の唯一のヒントは、グリッドには確実に中央ボタン用の場所があるということです。どのようにゲームを見て、そこにグリッドを表示しようとしているかのイメージを見てください。

これ以上のことは、私はスプーンがあなたにあまりにも多くの餌を与えてくれると思います。

+0

ヒント@BarriaKarlありがとうございます。私は自分自身のためにこれを理解したいコードを私にスプーンしてもらうことは間違いありません。私はスイングとGridPaneを調べます。私が添付した絵のグリッドレイアウトに気付きました。ただ今レイアウトする方法を理解する必要があります。いくつかのより多くのものを学ぶことを開始する時間は笑:) – Rkennedy11

関連する問題