Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 大括号中的代码未运行_Java - Fatal编程技术网

Java 大括号中的代码未运行

Java 大括号中的代码未运行,java,Java,在我们的代码中,由前一帧中的JButton链接,其中位于大括号内的部分不会运行 我可以判断,因为程序没有终止,所以关闭时的JFrame.EXIT\u无法工作。如果大括号被去掉,则会出现其他错误 我们如何使代码运行 public class PeopleCreator extends PeopleMove { public static final JFrame PeopleFrame = new JFrame("The Lovely Couple"); ImageIcon gir

在我们的代码中,由前一帧中的JButton链接,其中位于大括号内的部分不会运行

我可以判断,因为程序没有终止,所以关闭时的JFrame.EXIT\u无法工作。如果大括号被去掉,则会出现其他错误

我们如何使代码运行

public class PeopleCreator extends PeopleMove {
    public static final JFrame PeopleFrame = new JFrame("The Lovely Couple");
    ImageIcon girl = new ImageIcon();// pic of girl
    ImageIcon boy = new ImageIcon();// pic of boy
    String name = JOptionPane.showInputDialog("What is your name?");
    JButton girlDialogue1 = new JButton("Hey " + name
            + "! Hey can you get something to drink");
    // This code below does not run.
    {
        PeopleMove.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        PeopleMove.add(girlDialogue1, BorderLayout.SOUTH);
        girlDialogue1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent z) {
                JButton boyDialogue1 = new JButton("");// girls first text
                {
                    PeopleMove.add(boyDialogue1);
                    boyDialogue1.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent y) {
                            JButton girlDialogue2 = new JButton("");// boys
                            // first
                            // text
                            PeopleMove.add(girlDialogue2);
                            girlDialogue2
                            .addActionListener(new ActionListener() {
                                public void actionPerformed(
                                        ActionEvent x) {
                                    JButton boyDialogue2 = new JButton(
                                            "");
                                    PeopleMove.add(boyDialogue2);
                                    boyDialogue2
                                    .addActionListener(new ActionListener() {
                                        public void actionPerformed(
                                                ActionEvent v) {
                                            PeopleMove.pack();
                                            PeopleMove
                                            .setVisible(true);
                                        }
                                    });
                                }
                            });
                        }
                    });
                }
            }
        });
    }
}

每次使用new关键字创建PeopleCreator的新实例时(它附加到当前类的每个构造函数中),大括号中的代码都将被执行。我猜您并没有在任何地方创建
newpeoplecreator
,而是在引用静态字段holding
JFrame

你到底在用四个嵌套的匿名类做什么?当然有更好的方法来组织这件事。并将所有代码转储到实例初始值设定项中。。。即使成功了,也会很糟糕。基本上,整个类应该是一个方法(或两个…),synthax非常奇怪。声明一个静态变量,一些具有默认可见性的变量,然后编写一个初始化块而不是构造函数(这似乎不是有意的,因为在变量声明中,您打开了一个OptionPane)。如果删除外括号,则类中只有代码,但代码必须位于某个方法(如初始化块)中,这就是删除括号时出现synthax错误的原因。要想知道错误可能在哪里,你应该添加调用类的main方法。请阅读和一些关于如何改进你的问题的指导。就目前而言,这太可怕了。@每个人-我相信无论你告诉这个家伙什么,他都不会理解,因为我认为他缺乏java的基本知识。所以我建议找一本Java初学者的书来读一读,然后再回到这个问题上来,你会看到如何解决这个问题。