我是一名 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();
}
}
非常感謝你們,我期待著向你們學習更多。
uj5u.com熱心網友回復:
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){
...
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/407100.html
標籤:
上一篇:Java系結雙向
