gpt4 book ai didi

java - 在 Java 中使用 swing 时出现错误消息的对话框

转载 作者:行者123 更新时间:2023-12-02 06:36:26 24 4
gpt4 key购买 nike

我不是一个好的java程序员。当我使用此编码时,我遇到了一些错误。有人可以帮助我吗?

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.JOptionPane;

public class test4 implements ActionListener

{

public static void main(String[] args)throws Exception

{

JFrame f=new JFrame("test 4");

JButton b=new JButton("gud morning");

JButton b1=new JButton("yes or no");

b.addActionListener(this);

b1.addActionListener(this);

Container content=f.getContentPane();

content.add(b,BorderLayout.NORTH);

content.add(b1,BorderLayout.SOUTH);

f.setVisible(true);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setSize(100,100);

}
}

public void actionPerformed(ActionEvent e)

{

String s=e.getActionCommand();

if(s.equals("gud morning"))

{

JOptionPane.showMessageDialog(f,"gud morning","My Message");

}

else if(s.equals("yes or no"))

{


if(JOptionPane.showConfirmMessageDialog(f,"yes or no","it is also my message",JOptionPane.YES_NO_CANCEL_OPTION)==0);

{

JOptionPane.showMessageDialog(f,"u clicked yes button");

}

else

{

JOptionPane.showMessageDialog(f,"u clicked no button");

}

}

}

我收到此错误:

\ram>javac test4.java test4.java:22: error: class, interface, or enum expected public void actionPerformed(ActionEvent e) ^ test4.java:25: error: class, interface, or enum expected if(s.equals("gud morning")) ^ test4.java:28: error: class, interface, or enum expected } ^ 3 errors

最佳答案

您的 if 语句由 ; 终止,这将使 else 分支不正确。删除;

if(JOptionPane.showConfirmMessageDialog(f,"yes or no","it is also my message",JOptionPane.YES_NO_CANCEL_OPTION)==0);

变成:

if(JOptionPane.showConfirmMessageDialog(f,"yes or no","it is also my message",JOptionPane.YES_NO_CANCEL_OPTION)==0)

actionPerformed 开始之前还有一个额外的花括号 }

关于java - 在 Java 中使用 swing 时出现错误消息的对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19605784/

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