想在JFrame(JAVA)中创建特定的布局

时间:2017-12-06 00:20:10

标签: java jframe

所以我拖延学习决赛,并认为我应该在参加Java课程后做一个简单的游戏。我们简要介绍了如何使用JFrame,因此我想将它用于我的游戏。我正在制作一个类似于cookie Clicker的简单点击器游戏或同一类型的任何游戏。现在我的GUI看起来像this。我希望我的GUI看起来类似于this,其中菜单按钮位于中央按钮的不同侧面。我尝试使用不同的布局管理器,但没有一个能够用于我正在做的事情。我尝试了流程布局,框布局和网格布局。网格让我最接近,但仍然没有很好地工作,因为一切都是相同的大小,只是一个网格,所以我没有那个中央按钮。有没有人对我有任何提示?

这是我的发射器类

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);
            }
        });
    }
}

这是我的游戏类

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.");
            }
        }
    }

这是我将类设置为变量等的类

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;
            }
        }

编辑:将代码放入问题而不是指向我的Github的链接

1 个答案:

答案 0 :(得分:0)

由于您似乎刚刚开始使用GUI,我强烈建议您使用像swing这样的GUI管理器。

除了我在你的代码中看到的所有内容之外,你创建组件并将它们添加到你的布局中而不告诉程序把它们放在哪里。这就是你的游戏看起来像这样的原因。

我认为使用GridPane可以完全胜任。我唯一的提示是网格肯定有一个中央按钮的位置。查看您希望游戏看起来如何的图像,并尝试在那里看到网格。

不仅如此,我想我会用勺子喂你太多而且这不好。