大括号中的代码未运行

时间:2014-05-22 15:28:55

标签: java

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

我可以判断,因为JFrame.EXIT_ON_CLOSE无效,因为程序没有终止。如果括号被带走,则会出现其他错误。

我们如何制作以便运行此代码?

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

1 个答案:

答案 0 :(得分:0)

每次使用PeopleCreator关键字创建new的新实例时,都会执行大括号中的代码(它附加到当前类的每个构造函数)。我猜你没有在任何地方创建new peopleCreator,但你正在引用静态字段持有JFrame