2010-03-01 85 views
0

我正在从类写入一个程序,并且我正在尝试设置它,因此创建了一个以按钮形式显示搜索结果的窗口。如果没有搜索结果,我希望它会显示窗口会弹出一个警告消息,然后关闭窗口。我有一个设置,只要我想使窗口关闭,我调用一个CloseWindow()方法,只包含一个this.dispose();命令。如果我在按下按钮时从actionEvent方法调用它,窗口会关闭,但如果我尝试在方法的其他任何地方调用它,它将不会关闭窗口。有没有一些基本的Java概念,我错过了?我知道JFrame具有Window类中的dispose方法,但“this”似乎只在某些条件下才起作用。Java this.dispose调用时没有关闭窗口

相关的代码是下面:

public class MovieSearch extends JFrame implements ActionListener, Serializable{ 

private static final long serialVersionUID = 7526471155622776147L; 

private Container con = getContentPane(); 

int llSize, searchResults = 0; 
MovieNode currentNode; 

String searchText; 

JPanel listPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); 

JScrollPane scrollPane = new JScrollPane(listPanel); 

public MovieSearch(String searchText){ 
    super("Search Results"); 

    this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); 

    this.searchText = searchText; 

    con.add(scrollPane); 

    currentNode = MovieView.firstNode; 

    for(int i = 0; i < llSize; i++){ 
     if (currentNode.getTitle().indexOf(searchText) != -1) { 

      BufferedImage Thumbnail = new BufferedImage(200, 300, BufferedImage.TYPE_INT_ARGB); 
      Thumbnail.getGraphics().drawImage(currentNode.getImage().getImage(), 0, 0, 200, 300, null); 
      ImageIcon icon = new ImageIcon(Thumbnail); 

      JButton button = new JButton("Go to " + currentNode.getTitle()); 
      button.addActionListener(this); 
      button.setVerticalTextPosition(AbstractButton.BOTTOM); 
      button.setHorizontalTextPosition(AbstractButton.CENTER); 
      button.setIcon(icon); 
      listPanel.add(button); 

      searchResults++; 

      currentNode = currentNode.getLink(); 
     } else { 
      System.out.println("String " + currentNode.getTitle() + " does not contain String " + searchText); 
      currentNode = currentNode.getLink(); 
     } 
    } 

    if(searchResults == 0){ 
     int messageType = JOptionPane.ERROR_MESSAGE; 
     JOptionPane.showMessageDialog(null, "No results match that query.", "NO RESULTS!", messageType); 
     CloseWindow(); 

    }else{ 
     currentNode = MovieView.firstNode; 
     repaint(); 
    } 
} 

public void actionPerformed(ActionEvent e){ 
    Object source = e.getSource(); 

    for(int i = 0; i < llSize; i++){ 
     JButton button; 

     button = (JButton) source; 

     if(button.getText().equals(("Go to " + currentNode.getTitle()))){ 
      MovieView.currentNode = currentNode; 
      MovieView.searchTextField.setText(""); 
      CloseWindow(); 
     } 

     System.out.println("button is " + button.getText()); 
     System.out.println("text is: " + "Go to " + currentNode.getTitle()); 
     currentNode = currentNode.getLink(); 
    } 

} 


private void CloseWindow(){ 
    System.out.println("Closing Window"); 
    this.dispose(); 
} 

}

同样,CloseWindow()方法[并因此this.dispose()方法]工作称为形式时的ActionEvent方法而不是从其他地方。 [我已经将它插入到其他地方进行测试并且已经到达,但它仍然没有关闭窗口。]

正如您所看到的,我在CloseWindow()方法中放置了一个println以确保它是到达并且每次都到达,它只是不工作。

任何洞察到这一点将不胜感激。感谢您的时间。

+0

在构建MovieSearch对象的代码* around *中发生了什么以及它从哪里调用?通常,你会希望dipose()方法,就像一般的Swing方法一样,可以从事件派发线程调用,事实上这通常意味着从事件处理程序中除非你做了一些特殊的事情。通常情况下,你会希望在你展示窗口之后处理*(如果你展示的话),但是我不知道你在做什么。注:从程序流的角度来看,在构造函数中打开一个对话框是一个有点奇怪的设计,我会说。 – 2010-03-01 04:45:39

回答

2

JOptionPane创建一个“模式对话框”,这意味着“showMessageDialog”之后的语句直到对话框关闭之后才会执行。

你有两个选择:

一)创建你自己定制的“非模态对话框”,显示你的消息,然后关闭。 b)阅读JOptionPane API。它向您展示了如何手动访问由JOptionPane类创建的对话框,以便您有对该对话框的引用。

在这两种情况下,您都需要在显示对话框之前启动Swing Timer。然后,当Timer触发时,您可以处理该对话框。