我正在编写一个Cookie Clicker游戏翻拍,当我缩放JFrame窗口时,出现白色。只要将鼠标悬停在按钮上(刷新时),它就会消失,而我需要解决这个问题,因为启动游戏时它的作用甚至相同。

这是屏幕截图(未缩放|已缩放):http://s3.postimg.org/xomifomhf/bandicam_19.png

这是这个游戏的全部代码:

package cookieclicker.tominocz;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main {

    public static int num1;

    static Icon icon1 = new ImageIcon("cookie.png");
    static JButton b1 = new JButton(icon1);
    static JButton b2 = new JButton("You got " + num1 + " Cookies!");

    @SuppressWarnings("serial")
    public static void main(String[] args) {

        File save = new File(".\\gamesave.cookieclicker");
        if (save.exists()) {
            loadGame();
        }

        JFrame f = new JFrame("Cookie Clicker Beta v0.1");
        b2.setBackground(Color.cyan);
        b2.setPreferredSize(new Dimension(10000, 14));

        JPanel buttonPanel1 = new JPanel(new GridLayout(1, 1));
        buttonPanel1.setBackground(Color.DARK_GRAY);
        b2.setEnabled(false);
        b2.setBorder(null);
        buttonPanel1.add(b2);

        JPanel buttonPanel2 = new JPanel(new GridLayout(1000, 1));
        buttonPanel2.setBackground(Color.DARK_GRAY);
        buttonPanel2.setEnabled(false);
        buttonPanel2.add(new JButton("Grandma"));
        buttonPanel2.add(new JButton(""));
        buttonPanel2.add(new JButton(""));

        JPanel east = new JPanel(new GridBagLayout());
        JPanel north = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.NORTH;
        gbc.weighty = 1;

        north.add(buttonPanel1, gbc);
        east.add(buttonPanel2, gbc);

        JPanel center = new JPanel() {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(200, 200);
            }
        };
        center.setBorder(BorderFactory.createLineBorder(Color.BLACK));

        f.add(east, BorderLayout.EAST);
        f.add(north, BorderLayout.NORTH);
        f.add(center);

        f.pack();

        f.setSize(600, 400);
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        b1.setBackground(Color.lightGray);
        b1.setBorder(null);
        f.add(b1);
        b1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Object source = e.getSource();
                if (source instanceof JButton) {
                    addCookies();
                }
            }
        });
    }

    public static void addCookies() {

        saveGame();

        b2.setText("You got " + ++num1 + " Cookies!");

        if (num1 == 1) {
            b2.setText(" You got " + 1 + " Cookie! ");
        } else {
            b2.setText("You got " + num1 + " Cookies!");
        }
        System.out.println(num1);
    }

    public static void saveGame() {
        try {

            BufferedWriter writer = new BufferedWriter(new FileWriter(
                    ".\\gamesave.cookieclicker"));
            writer.write(String.valueOf(1 + num1));

            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void loadGame() {
        try (BufferedReader br = new BufferedReader(new FileReader(
                ".\\gamesave.cookieclicker"))) {
            String SavedGame;

            while ((SavedGame = br.readLine()) != null) {
                num1 = Integer.parseInt(SavedGame);
                b2.setText("You got " + num1 + " Cookies!");
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


还有另一件事是,深灰色的条带隐藏了3个按钮。
不要算第四个,那是显示您拥有的Cookie数量的:)。

现在问题可能在哪里?

最佳答案

您有两个组件共享框架CENTREBorderLayout位置,centerb1

b1是添加的最后一个组件,正在引起布局管理器的注意,并在调整框架大小时进行布局,center不是,并且保持在设置的最后一个大小/位置(因为您称为pack,这会强制框架对其子组件进行布局,但随后在其后添加了b1

BorderLayout只能在五个预定位置的每个位置管理一个组件

决定谁应该在中心...

您还应该阅读Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?并停止弄乱组件的首选大小,让它们与适当的布局管理器结合起来做出自己的决定

我也鼓励您将main方法的内容移到其他地方(可能是类的构造函数),这样,您的字段就不必是static,它可以解决很多其他问题将来可能会有

关于java - 为什么我的JFrame窗口会这样做?(检查说明),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30813828/

10-14 12:26