【发布时间】:2015-05-23 16:31:31
【问题描述】:
开发一个摇摆应用程序,如果他单击JoptionPane.showConfirmDialog 上的No button,我需要将用户重定向到另一个JFrame。我写了这段代码:
private void formWindowClosing(java.awt.event.WindowEvent evt) {
int a=JOptionPane.showConfirmDialog(this, "Did you complete your task?");
JOptionPane.showMessageDialog(null, a);
if(a==1){
RedirectedForm rf=new RedirectedForm();
rf.setVisible(true);
}
}
但if(a==1){...} 部分似乎不起作用。即使我创建了对要重定向到的 JFrame-RedirectedForm 的引用,窗口也会始终被释放。任何人都可以解释原因并提出解决方案吗?
感谢任何帮助,谢谢!!!
提供以下两个 java 类:
WindowClosing.java
import javax.swing.JOptionPane;
public class WindowClosing extends javax.swing.JFrame {
public WindowClosing() {
initComponents();
}
private void initComponents() {
window_close_label = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
formWindowClosing(evt);
}
});
window_close_label.setText("Window Close Form");
pack();
}
private void formWindowClosing(java.awt.event.WindowEvent evt) {
int a=JOptionPane.showConfirmDialog(this, "Did you complete your task?");
JOptionPane.showMessageDialog(null, a);
if(a==1){
RedirectedForm rf=new RedirectedForm();
rf.setVisible(true);
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new WindowClose().setVisible(true);
}
});
}
private javax.swing.JLabel window_close_label;
}
RedirectedForm.java
public class RedirectedForm extends javax.swing.JFrame {
public RedirectedForm() {
initComponents();
}
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("You have been redirected to this Form becuse you have closed the previous one");
pack();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new RedirectForm().setVisible(true);
}
});
}
private javax.swing.JLabel jLabel1;
}
我再次编辑了代码,希望这可以更清楚地了解问题
【问题讨论】:
-
反馈类有什么作用?这就是我们需要查看的代码,以及使用 formWindowClosing 方法的代码。创建一个简短的、自包含的、可运行 Java 应用程序来说明问题,然后我们可以帮助您.
-
反馈类只是另一种形式,它有一些
JLabel、JtextField和JButtons。一点都不花哨。 在我的问题中发布代码 -
@GilbertLeBlanc :希望代码符合您的要求,因为我删除了声明以及其他布局和图像设置
-
@GilbertLeBlanc :我非常确定
Feedback.java不会在任何地方自行处理/或自动处理它的代码中存在任何错误 -
是的,代码没有混淆,只是没有编译。此外,您还需要解释什么是“页面”,因为这不是浏览器。如果您想要一个框架,当您单击按钮时会更改其内容,然后发布尝试执行此操作且仅此操作的代码(另请参阅
CardLayout)..
标签: java swing joptionpane custom-component