Java 找不到创建加载屏幕的方法

Java 找不到创建加载屏幕的方法,java,swing,Java,Swing,我只是在玩Java中的GUI,并创建了一个小游戏。其中,创建了105个随机放置的按钮,然后弹出一个指示屏幕,告诉用户要查找哪个按钮。我试图找出如何编写一个“加载…”JDialog,当按钮在后台创建时,它会弹出。问题是,当我运行这个程序时,JDialog直到所有按钮都创建好之后才加载,这一点首先违背了这个框的用途。如何在开始创建按钮之前强制加载“加载…”框???提前谢谢。 因为我刚刚进行了修补,我的代码并不完美,但它是: import java.util.Scanner; import j

我只是在玩Java中的GUI,并创建了一个小游戏。其中,创建了105个随机放置的按钮,然后弹出一个指示屏幕,告诉用户要查找哪个按钮。我试图找出如何编写一个“加载…”JDialog,当按钮在后台创建时,它会弹出。问题是,当我运行这个程序时,JDialog直到所有按钮都创建好之后才加载,这一点首先违背了这个框的用途。如何在开始创建按钮之前强制加载“加载…”框???提前谢谢。 因为我刚刚进行了修补,我的代码并不完美,但它是:

    import java.util.Scanner;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Random;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.ProgressMonitor;

public class ButtonGame {

    private static int butNum = 1;
    private static JFrame frame;
    private static ActionListener notIt;
    private static ActionListener it;
    private static Random rand = new Random();
    private static int butToFind = rand.nextInt(105);
    private static JFrame frameToClose;

    //private static int mouseClicks;
    //private static double time;

    public static void main(String[] args) {

    //actionlistener for all incorrect buttons (buttons that are "not it")
    notIt = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Component component = (Component) e.getSource();
            JFrame frame5 = (JFrame) SwingUtilities.getRoot(component);
            frame5.dispose();
        }
        };

    //actionlistener for the correct button (the button that's "it")
    it = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JFrame youWin = new JFrame("YOU WON!");

            //removes all panels to begin game again
            JButton again = new JButton("Play again");
            again.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                java.awt.Window windows[] = java.awt.Window.getWindows(); 
                for(int i=0;i<windows.length;i++){
                    if (windows[i] != frame) { windows[i].dispose(); }
                    butToFind = rand.nextInt(105);
                    butNum = 1;
                    youWin.dispose();
                }
                frame.setVisible(true);
                }
            });

            //quits game
            JButton win = new JButton("Quit");
            win.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                System.exit(0);
                }
            });

            //layout
            youWin.setSize(775, 300);
            youWin.setLayout(new FlowLayout());

            JLabel label1 = new JLabel("Fantastic!");
            Font font1 = new Font("Courier", Font.BOLD,120);
            label1.setFont(font1);
            label1.setHorizontalAlignment(JLabel.CENTER);

            JLabel label2 = new JLabel("You beat the game!");
            Font font2 = new Font("Courier", Font.BOLD,60);
            label2.setFont(font2);
            label2.setHorizontalAlignment(JLabel.CENTER);

            youWin.add(label1);
            youWin.add(label2);

            JPanel panel = new JPanel();
            youWin.add(panel);
            panel.add(again);
            panel.add(win);
            youWin.setLocation(260, 100);
            youWin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            youWin.setVisible(true);
            java.awt.Window windows[] = java.awt.Window.getWindows(); 
        }
        };

    //start window
    frame = new JFrame("Window");

    frame.setLocation(400, 200);

    JButton button1 = new JButton("Click to begin");

    //button to begin game
    button1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
        // JDialog load = new JDialog();
        // load.setAlwaysOnTop(true);
        // load.setSize(500,500);
        // load.setVisible(true);
        //     load.add(new Label("Loading..."));
        //     load.pack();
        frame.setVisible(false);  // "start" window's visibility
        // try {
        //     Thread.sleep(100000);
        // } catch (Exception t) {
        // }

        // creates buttons
        for (int i = 0; i < 105; i++) {
            JFrame nextFrame = newFrame(butNum);
            nextFrame.setVisible(true);
            butNum++;
        }
        //creates instructions and tells user what button to find
        JFrame instructions = new JFrame("How to play");
        instructions.setSize(300,175);
        instructions.setLayout(new GridLayout(4,1));
        JPanel instPanel = new JPanel();

        //button to remove instruction panel
        JButton ok = new JButton("Ok");
        ok.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                instructions.dispose();
            }
            });
        instPanel.add(ok);
        instructions.setLocation(400,200);

        //layout of instruction panel
        JLabel find = new JLabel("Your goal is to find Button " + butToFind + ".");
        find.setHorizontalAlignment(JLabel.CENTER);

        JLabel find2 = new JLabel("Click a button to make it disappear.");
        find2.setHorizontalAlignment(JLabel.CENTER);

        JLabel find3 = new JLabel("Good luck!");
        find3.setHorizontalAlignment(JLabel.CENTER);

        instructions.add(find);
        instructions.add(find2);
        instructions.add(find3);
        instructions.add(instPanel);
        instructions.setVisible(true);
        }
    });

    frame.add(button1);
    frame.setSize(150,100);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    }

    //creates frame with button in it
    public static JFrame newFrame(int num) {
    JFrame frame2 = new JFrame();
    JButton button = new JButton("Button " + num);
    if (num == butToFind) {
        button.addActionListener(it);
        frameToClose = frame2;
    } else {
        button.addActionListener(notIt);
    }
    frame2.add(button);
    frame2.setSize(randNum(90,200), randNum(50,100));
    frame2.setLocation(rand.nextInt(1200), rand.nextInt(800));
    frame2.getContentPane().setBackground(new Color(rand.nextInt(255),
                            rand.nextInt(255),
                            rand.nextInt(255)));
    frame2.setVisible(true);
    return frame2;
    }

    //provides random number between high and low
    public static int randNum(int low, int high) {
    int result = -1;
    while (result < low || result > high) {
        result = rand.nextInt(high);
    }
    return result;
    }
}
import java.util.Scanner;
导入java.awt.*;
导入javax.swing.*;
导入java.awt.event.*;
导入java.util.Random;
导入java.awt.Color;
导入javax.swing.JButton;
导入javax.swing.ProgressMonitor;
公共类按钮名称{
私有静态int-butNum=1;
私有静态JFrame;
私有静态ActionListener notIt;
私有静态侦听器;
私有静态随机兰德=新随机();
私有静态int butToFind=rand.nextInt(105);
私有静态JFrame框架关闭;
//私有静态int鼠标;
//私有静态双时间;
公共静态void main(字符串[]args){
//actionlistener用于所有不正确的按钮(不是它的按钮)
notIt=newactionlistener(){
已执行的公共无效操作(操作事件e){
组件=(组件)e.getSource();
JFrame frame5=(JFrame)SwingUtilities.getRoot(component);
框架5.dispose();
}
};
//actionlistener以获取正确的按钮(即“it”按钮)
it=newactionlistener(){
已执行的公共无效操作(操作事件e){
JFrame youWin=新的JFrame(“你赢了!”);
//移除所有面板,重新开始游戏
JButton再次=新JButton(“再次播放”);
同样,addActionListener(新的ActionListener(){
已执行的公共无效操作(操作事件e){
java.awt.Window窗口[]=java.awt.Window.getWindows();

对于(int i=0;i回答您的旁白问题:

  • 静态方法只能接受静态全局变量
  • 您可以将所有代码放在构造函数中,并使用main仅运行程序
建造商:

public ButtonGame() {
 // All of your code goes here - except the static methods
}
您还应该使所有其他方法成为非静态的

要运行该程序,请执行以下操作:

public static void main(String[] args) {
    new ButtonGame();
}
首先看看并理解为什么我在运行你的代码时会发疯

与其创建一堆框架,不如在另一个
JPanel
上使用类似
JButton
的东西,并将其添加到当前框架中(这对于
CardLayout
也很有用)


你可以使用SwingWorker,但你需要小心,因为你真的不应该在EDT的上下文之外创建或修改ui组件。我明白你说的拥有多个JFrames的意思。但是为了练习,我该如何“加载…”在创建按钮之前加载JFrame?我尝试过使用不同的线程并使用Thread.sleep()函数(我不太熟悉这个函数),但是我所做的一切都不允许加载“加载…”JFrame在开始创建按钮之前。问题-Swing是单线程的,并且不是线程安全的,因此您真的不应该从EDT的上下文之外创建或修改UI…这是否使使用我的方法不可能做到?这会使使用SwingWorker变得危险和困难,但JFrame再次出现bl直到加载所有按钮。为什么会发生这种情况?在我看来,SwingWorker/Thread影响类没有做任何事情…这只是编码的一个更微妙和高级的方面吗?
JPanel panel = new JPanel(new GridLayout(10, 0));
Random rnd = new Random();
// creates buttons
for (int i = 0; i < 105; i++) {
    JButton btn = new JButton(String.valueOf(i));
    panel.add(btn);
    //JFrame nextFrame = newFrame(butNum);
    //nextFrame.setVisible(true);
    //butNum++;
}

frame.getContentPane().removeAll();
frame.add(panel);
frame.revalidate();
frame.pack();
public class ButtonGame {

    private int butNum = 1;
    private JFrame frame;
    private ActionListener notIt;
    private ActionListener it;
    private Random rand = new Random();
    private int butToFind = rand.nextInt(105);
    private JFrame frameToClose;

    //private static int mouseClicks;
    //private static double time;
    public static void main(String[] args) {
        ButtonGame game = new ButtonGame();
        game.start();
    }

    public ButtonGame() {
        //... All the code that was once in main...
        frame.add(button1);
        frame.setSize(150, 100);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void start() {
        frame.setVisible(true);     
    }