想要在Jframe(Java)中创建特定的布局



所以我正在拖延决赛学习,并认为我应该在参加Java课程的介绍后做一个简单的游戏。我们简要了解了如何使用Jframes,因此我想将其用于游戏。我正在制作类似于Cookie Clicker或同一类型的任何游戏类似的简单点击器游戏。现在我的GUI看起来像这样。我希望我的GUI看起来与此相似,菜单按钮位于中央按钮的不同侧面。我试图使用不同的布局经理,但它们都不适合我正在做的事情。我尝试过流程布局,框布局和网格布局。网格使我最接近,但仍无法正常工作,因为一切都相同,只是一个网格,所以我无法拥有那个中央按钮。有人对我有任何建议吗?

这是我的发射器类

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

的链接

,因为您似乎只是开始与Guis一起玩,所以我强烈建议您使用像Swing这样的GUI经理。

除了我在代码中看到的只是您创建组件并将它们添加到布局中,而无需告诉程序将它们放在哪里。这就是您的游戏看起来像这样的原因。

我认为使用Gridpane可以完全完成这项工作。我唯一的暗示是,网格绝对有一个中央按钮的位置。查看您希望游戏外观并尝试在那里看到网格的图像。

不仅如此,我想我会给你太多喂你,这不好。

最新更新