簡體   English   中英

顯示JOptionPane后JButton保持按下狀態

[英]JButton stays pressed after a JOptionPane is displayed

好的,很抱歉,我要重復已經提出的問題,但是我一直在搜索,沒有人的答案似乎對我有幫助...我嘗試了以下問題:

單擊Java Applet后,JButton“保持按下狀態”

當JOptionPane竊取焦點時,JButton保持按下狀態

(很抱歉,我很抱歉。很難與我的代碼聯系起來)

我已經嘗試了所有方法:使用另一個線程來處理所有內容,將JFrame更改為JDialog因為它們顯然是“模態”的,因此它將獨立工作。 但這似乎也不起作用。 我現在被困住了,所以我正在使用我的最后一個資源(請求堆棧溢出)。

我正在嘗試做的是讓用戶在文本字段(4,2,7)中輸入一些數字,然后他們按下JButton “計算均值”,它會找到數字的均值並將其顯示在JOptionPane消息中。 當用戶關閉JOptionPane對話框時,他們應該能夠編輯數字並再次進行操作,但是“ Calculate Mean”按鈕將保持按下狀態,並且用戶只能關閉窗口。 即使按Tab鍵也不會更改任何內容。 有人知道為什么是這樣嗎? 我的代碼如下:

如果我的代碼很難閱讀,請原諒我! 我花了很長時間嘗試正確地縮進所有內容,並且通過去除與該問題無關的任何內容來使它盡可能短。 我不確定要取出哪些位,所以可能仍然有一些不必要的位...對我的凌亂代碼感到抱歉,但這是代碼:

package MathsProgram_II;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;

public class Mean implements Runnable {
    JFrame meanFrame = new JFrame(); //I tried changing this to dialog
    JPanel meanPanel = new JPanel(new GridBagLayout());
    JLabel enterNums = new JLabel("Enter Numbers: ");
    JTextField txtNums = new JTextField(20);
    JButton calculate = new JButton("Calculate Mean");

    boolean valid = true;
    double answer = 0;
    ButtonListener bl = new ButtonListener();


    public synchronized double[] getArray() {
        String nums = txtNums.getText();
        String[] numsArray = nums.split(",");
        double[] doubleArray = new double[numsArray.length];
        if (nums.isEmpty() == true) {
            JOptionPane.showMessageDialog(meanFrame, "You did not enter     anything!",
                    "Fail", JOptionPane.ERROR_MESSAGE);

            valid = false;
            calculate.setEnabled(false);
        } else {
            for (int i = 0; i < numsArray.length; i++) {
                try {
                    doubleArray[i] = Double.parseDouble(numsArray[i]);
                } catch (NumberFormatException nfe) {
                    JOptionPane.showMessageDialog(meanFrame, "Error getting numbers!",
                            "Error", JOptionPane.ERROR_MESSAGE);
                    valid = false;
                }
            }
        }
        return doubleArray;
    }


    public synchronized void calculateMean() {
        ArrayList<Double> numbersList = new ArrayList<Double>(20);
        double[] theNumbers = getArray();
        double tempAnswer = 0;
        if (valid == true) {
            int length = theNumbers.length;
            for (int i = 0; i < theNumbers.length; i++) {
                numbersList.add(theNumbers[i]);
            }
            for (int i = 0; i < length; i++) {
                double y = numbersList.get(i);
                tempAnswer = tempAnswer + y;
            }
            this.answer = tempAnswer / length;
            //I ALSO TRIED DOING THIS:
            txtNums.requestFocus();
            calculate.setEnabled(false);

            showMean();
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
            }

        }

    }

    public void showMean() {
        JOptionPane.showMessageDialog(meanFrame, "The Mean: " + answer, "The Mean of      Your Numbers", JOptionPane.INFORMATION_MESSAGE);
    }

    private class ButtonListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == calculate) {
                meanFrame.remove(meanPanel);
                meanFrame.setVisible(true);
                calculateMean();
            }

        }

    }
}
  1. 不要從主機上卸下面板。
  2. 不要在您的“ calculateMean()”方法上使用“已同步”。 該代碼在事件調度線程(EDT)上執行,因此它將是單線程的。
  3. 不要在EDT上使用Thread.sleep()。 這將防止GUI重新繪制自身。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM