gpt4 book ai didi

Java this.dispose 调用时不关闭窗口

转载 作者:行者123 更新时间:2023-12-02 08:34:29 25 4
gpt4 key购买 nike

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

我设置了每当我想要关闭窗口时,我都会调用一个只包含 this.dispose(); 的 CloseWindow() 方法。命令。如果我在按下按钮后从 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 以确保它被达到并且每次都达到,但它不起作用。

任何对此的见解都将非常感激。感谢您抽出时间。

最佳答案

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

您有两个选择:

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

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

关于Java this.dispose 调用时不关闭窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2353702/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com