- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
开发一个 swing 应用程序,如果用户单击 JoptionPane.showConfirmDialog
上的 No 按钮
,我需要将用户重定向到另一个 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;
我已经重新编辑了代码,希望这能让你更清楚地了解问题
最佳答案
你的主要问题可能是你设置的
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
这会导致整个应用程序在您关闭框架时退出,无论您在对话框中选择了哪个选项。尽管如此,你shouldn't use multiple JFrames
, 而不是 use CardLayout
如此处所示:
public class Example extends JFrame {
private String redirectedForm = "RedirectedForm";
private String windowClosing = "WindowClosing";
Example() {
CardLayout cl = new CardLayout();
getContentPane().setLayout(cl);
add(new WindowClosing(), windowClosing);
add(new RedirectedForm(), redirectedForm);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
int a = JOptionPane.showConfirmDialog(Example.this, "Did you complete your task?");
if (a == JOptionPane.CANCEL_OPTION) {
return;
}
if (a == JOptionPane.NO_OPTION) {
// cl.next(frame.getContentPane()); // This to show the next card
cl.show(getContentPane(), redirectedForm); // This is to show a specified card
}
else {
dispose();
}
}
});
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE );
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String args[]) {
new Example();
}
}
class WindowClosing extends JPanel {
public WindowClosing() {
JLabel closeLabel = new JLabel("The WindowClosing panel");
add(closeLabel);
}
}
class RedirectedForm extends JPanel {
public RedirectedForm() {
JLabel label = new JLabel("You have been redirected to this Form becuse you have closed the previous one");
add(label);
}
}
通过设置DO_NOTHING_ON_CLOSE
windowClosing
事件不会终止程序。相反,您根据所选选项选择做什么:无,退出 ,或切换面板。
int
值,例如 JOptionPane.NO_OPTION
,而是使用对它的引用。JOptionPane
设置所有者。关于java - 自定义 `JOptionPane.YES_NO_OPTION`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30415044/
我在使用一个非常基础的初级 Java 程序时遇到了一些问题。我有一个函数,它应该根据用户对 JOptionPane YES_NO_OPTION 消息框的响应返回一个值。该程序运行良好,直到您看到重新开
所以我有代码 import javax.swing.JOptionPane; import javafx.application.Application; public abstract class
这是我的问题,我试图让这段代码正常工作,以便在我的 GUI 中,当我单击"is"时,会添加产品(该段代码仍有待开发)并且 addproductwindow 关闭,但当单击 no 时,JOptionPa
开发一个 swing 应用程序,如果用户单击 JoptionPane.showConfirmDialog 上的 No 按钮,我需要将用户重定向到另一个 JFrame。我写了这段代码: private
我编写了一个程序来识别字符串的平方、平方根、数字的立方和反转,但我有一些错误,我不知道如何解决这个问题。 下面是我的代码 import javax.swing.JOptionPane;
是否有人有创建 JOptionPane(例如 YES_NO_OPTION 对话框)并将默认值更改为“NO”同时保留键盘快捷键的经验?我使用对象数组来填充 {"YES", "NO"} 的选项,然后在 J
基本上,我有一个游戏,当发生某些事情时会调出 JOptionPane,我希望能够在用户单击"is"时返回游戏。有点像取消暂停功能 最佳答案 注意:当我发布这个答案时,问题完全不同 - 请阅读评论。 由
我在四个单独的对话框中有一系列四个是/否选择,其累积结果将导致十二个单独链接之一(例如,是/是/是/否 -> 链接 A,是/否/否/是 -> 链接 B,等等)。分支逻辑使用 boolean 值。 这是
我是一名优秀的程序员,十分优秀!