【问题标题】:Java this.dispose not closing window when calledJava this.dispose 调用时不关闭窗口
【发布时间】:2010-03-01 03:18:02
【问题描述】:

我正在编写一个课堂上的程序,我正在尝试对其进行设置,以便创建一个以按钮形式显示搜索结果的窗口。如果没有搜索结果,我希望窗口会调用一个弹出警告,说明这样,然后关闭窗口。

我已经设置好了,每当我想关闭窗口时,我都会调用一个 CloseWindow() 方法,它只包含一个 this.dispose();命令。如果我在按下按钮后从 actionEvent 方法中调用它,窗口会很好地关闭,但如果我尝试在方法中的几乎任何其他位置调用它,它不会关闭窗口。我缺少一些基本的 Java 概念吗?我知道 JFrame 有 Window 类的 dispose 方法,但“this”似乎只在某些条件下有效。

相关代码如下:

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,以确保它正在被访问,并且每次都被访问,它只是不起作用。

对此的任何见解将不胜感激。感谢您的宝贵时间。

【问题讨论】:

  • 在构建 MovieSearch 对象的代码周围中发生了什么,从哪里调用它?通常,您会期望 dipose() 方法,就像一般的 Swing 方法一样,从事件调度线程调用,这实际上通常意味着从事件处理程序,除非您做一些特殊的事情。通常,您会希望在显示窗口后 进行处理(如果您完全显示它),但我看不出您在哪里执行此操作。注:从程序流程的角度来看,我会说在构造函数中打开对话框是一个有点奇怪的设计。

标签: java swing dispose


【解决方案1】:

JOptionPane 创建一个“模态对话框”,这意味着“showMessageDialog”之后的语句在对话框关闭之前不会执行。

你有两个选择:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-25
    • 2012-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多