对话框在操作条件下不起作用

Dialog box not working on action condition

我是一名 IT 学生,我一直在努力弄清楚为什么它不起作用,我不知道该怎么做,所以我决定寻求帮助。 主要问题是我的对话框在有按钮的情况下无法工作这里是操作代码:

class Main extends JFrame implements ActionListener {
  JButton submit;
submit = new JButton("Submit");
    submit.setBounds(480, 400, 90, 25);
    submit.addActionListener(this);
public void actionPerformed(ActionEvent e){
    if (submit.isSelected()){
        JOptionPane.showMessageDialog(this, "Done!");
    }
  }

这里是程序的完整代码以获取更多详细信息 --

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

class Main extends JFrame implements ActionListener {
  JButton submit;

  Main(){
    JFrame t = new JFrame("Parin_DCIT50Finals");
    t.setBackground(Color.black);

  JPanel mult = new JPanel();
    mult.setBackground(Color.cyan);
    mult.setBounds(5, 5, 250, 130);
    JLabel mul = new JLabel("MULTIPLE CHOICE:");
    JLabel qu = new JLabel("What is your favotite food?");
    JRadioButton a = new JRadioButton("Burger");
    JRadioButton b = new JRadioButton("Pizza");
    JRadioButton c = new JRadioButton("Adobo");
    JRadioButton d = new JRadioButton("Me");
    mult.add(mul);
    mult.add(qu);
    mult.add(a);
    mult.add(b);
    mult.add(c);
    mult.add(d);

    JPanel mult1 = new JPanel();
    mult1.setBackground(Color.yellow);
    mult1.setBounds(5, 130, 250, 130);
    JLabel qu1 = new JLabel("Best programming laguage?");
    JCheckBox a1 = new JCheckBox("Java");
    JCheckBox b1 = new JCheckBox("Python");
    JCheckBox c1 = new JCheckBox("C++");
    JCheckBox d1 = new JCheckBox("English");
    mult1.add(qu1);
    mult1.add(a1);
    mult1.add(b1);
    mult1.add(c1);
    mult1.add(d1);

    JPanel tf = new JPanel();
    tf.setBackground(Color.cyan);
    tf.setBounds(5, 260, 250, 130);
    JLabel tof = new JLabel("TRUE OR FALSE:");
    JLabel qu2 = new JLabel("Edward is very handsome & cute");
    JCheckBox t1 = new JCheckBox("True");
    JCheckBox f1 = new JCheckBox("False");

    tf.add(tof);
    tf.add(qu2);
    tf.add(t1);
    tf.add(f1);

    JPanel tf1 = new JPanel();
    tf1.setBackground(Color.yellow);
    tf1.setBounds(5, 390, 250, 130);
    JLabel qu3 = new JLabel("Math is a difficult subject");
    JCheckBox t2 = new JCheckBox("True");
    JCheckBox f2 = new JCheckBox("False");

    tf1.add(qu3);
    tf1.add(t2);
    tf1.add(f2);

    JPanel id = new JPanel();
    id.setBackground(Color.cyan);
    id.setBounds(260, 5, 500, 130);
    JLabel identif = new JLabel("IDENTIFICATION:");
    JLabel qu4 = new JLabel("What is my full name?");
    JTextField ans = new JTextField(20);
    
    id.add(identif);
    id.add(qu4);
    id.add(ans);

    JPanel id1 = new JPanel();
    id1.setBackground(Color.cyan);
    id1.setBounds(260, 130, 500, 130);
    JLabel qu5 = new JLabel("'OOP' stands for?");
    JTextField ans1 = new JTextField(20);
    
    id1.add(qu5);
    id1.add(ans1);

    JPanel essay = new JPanel();
    essay.setBackground(Color.yellow);
    essay.setBounds(260, 260, 500, 130);
    JLabel tit = new JLabel("ESSAY:");
    JLabel qu6 = new JLabel("What are your thoughts about COVID-19 during the whole school year?");
    JTextArea ans2 = new JTextArea();
    ans2.setBounds(20,75,500,200);
    
    essay.add(tit);
    essay.add(qu6);
    essay.add(ans2);

    submit = new JButton("Submit");
    submit.setBounds(480, 400, 90, 25);
    submit.addActionListener(this);

    t.add(id);
    t.add(id1);
    t.add(essay);
    t.add(mult);
    t.add(mult1);
    t.add(tf);
    t.add(tf1);
    t.add(submit);
    t.setLayout(null);
    t.setSize(500,500);
    t.setVisible(true);
}

  public void actionPerformed(ActionEvent e){
    if (submit.isSelected()){
        JOptionPane.showMessageDialog(this, "Done!");
    }
  }
  public static void main(String[] args) {
    new Main();
  }
  }

非常感谢你们,我期待着向你们学习更多。

if (submit.isSelected()){

https://docs.oracle.com/javase/7/docs/api/javax/swing/AbstractButton.html#isSelected()

isSelected 检查用于切换按钮。这些按钮的行为类似于复选框,您可以单击它们来打开或关闭它们。你的按钮不是其中之一。

尝试移除条件。目前唯一为该 actionPerformed 注册的是一个按钮,因此不需要条件(无论如何它应该做什么?)。

例如,如果您想对多项操作使用 actionPerformed 方法,并且想检查传入事件是否来自您的按钮,那么您可以使用类似的方法:

  public void actionPerformed(ActionEvent e){
    if (e.getSource() == submit){
   ...