简体   繁体   English

显示JOptionPane后JButton保持按下状态

[英]JButton stays pressed after a JOptionPane is displayed

Ok, I am sorry I am repeating questions that have already been asked but I have searched and searched and searched and nobody's answers seemed to have helped me... I tried the following questions: 好的,很抱歉,我要重复已经提出的问题,但是我一直在搜索,没有人的答案似乎对我有帮助...我尝试了以下问题:

JButton "stay pressed" after click in Java Applet 单击Java Applet后,JButton“保持按下状态”

JButton stays pressed when focus stolen by JOptionPane 当JOptionPane窃取焦点时,JButton保持按下状态

(I apologize if I'm just being dumb.. it is hard to relate to my code) (很抱歉,我很抱歉。很难与我的代码联系起来)

I have tried everything: using another thread to handle all the stuff, changing the JFrame to a JDialog as apparently they are "modal" so it would work independently. 我已经尝试了所有方法:使用另一个线程来处理所有内容,将JFrame更改为JDialog因为它们显然是“模态”的,因此它将独立工作。 But that didn't seem to work either. 但这似乎也不起作用。 I am stuck now so I am using my last resource (asking Stack Overflow). 我现在被困住了,所以我正在使用我的最后一个资源(请求堆栈溢出)。

What I am trying to do is get the user to enter some numbers in a textfield (4,2,7) then they press a JButton "Calculate Mean" and it finds the mean of the numbers and displays it in a JOptionPane message. 我正在尝试做的是让用户在文本字段(4,2,7)中输入一些数字,然后他们按下JButton “计算均值”,它会找到数字的均值并将其显示在JOptionPane消息中。 When the user closes the JOptionPane dialog box they should be able to edit the numbers and do it again but the "Calculate Mean" button stays pressed and the user can't do anything but close the window. 当用户关闭JOptionPane对话框时,他们应该能够编辑数字并再次进行操作,但是“ Calculate Mean”按钮将保持按下状态,并且用户只能关闭窗口。 Even pressing the Tab key doesn't change anything. 即使按Tab键也不会更改任何内容。 Does anyone know why this is? 有人知道为什么是这样吗? My code is down below: 我的代码如下:

PLEASE FORGIVE ME IF MY CODE IS HARD TO READ! 如果我的代码很难阅读,请原谅我! I spent a very long time trying to indent it all correctly and I also tried to make it as short as possible by taking out any bits unrelated to the question. 我花了很长时间尝试正确地缩进所有内容,并且通过去除与该问题无关的任何内容来使它尽可能短。 I was unsure which bits to take out so there still might be some unnecessary bits... I am sorry for my messy code but this is the code: 我不确定要取出哪些位,所以可能仍然有一些不必要的位...对我的凌乱代码感到抱歉,但这是代码:

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. Don't remove the panel from your mainframe. 不要从主机上卸下面板。
  2. Don't use "synchronized" on your "calculateMean()" method. 不要在您的“ calculateMean()”方法上使用“已同步”。 The code is executed on the Event Dispatch Thread (EDT) so it will be single threaded. 该代码在事件调度线程(EDT)上执行,因此它将是单线程的。
  3. Don't use a Thread.sleep() on the EDT. 不要在EDT上使用Thread.sleep()。 This will prevent the GUI from repainting itself. 这将防止GUI重新绘制自身。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM