2014-05-22 86 views
-2

在我們通過前一幀中的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

使它可讀? –

+3

你究竟做了四個嵌套的匿名課程?當然有更好的方法來組織這個。並將所有代碼轉儲到實例初始化程序中...即使它起作用,它仍然很糟糕。基本上這整個班級應該是一種方法(或兩個...) –

+0

合成器是非常奇怪的。你聲明瞭一個靜態變量,一些具有默認可見性的變量,然後你編寫一個初始化塊而不是構造函數(這看起來沒有打算,因爲在變量聲明中,你打開了一個OptionPane)。如果你刪除了外括號,你只需要在類內部添加代碼 - 但是代碼必須位於某個方法內(如初始化塊),這就是刪除方括號時出現合成錯誤的原因。要了解錯誤的位置,應該添加調用類的main方法。 –

回答

0

代碼將每次創建的PeopleCreator新實例new關鍵字(它被附加到當前類的每一個構造)執行。我猜測你並沒有在任何地方創建new peopleCreator,但是你正在引用持有JFrame的靜態字段。