显示JOptionPane后JButton保持按下状态 [英] JButton stays pressed after a JOptionPane is displayed

查看:97
本文介绍了显示JOptionPane后JButton保持按下状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,很抱歉我要重复已经提出的问题,但是我进行了搜索,没有人的答案似乎对我有帮助...我尝试了以下问题:

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保持按下"状态;在Java Applet中单击后

当JOptionPane窃取焦点时,JButton保持按下状态

(很抱歉,我很难与我的代码联系起来)

(I apologize if I'm just being dumb.. it is hard to relate to my code)

我已经尝试了一切:使用另一个线程来处理所有内容,将JFrame更改为JDialog,因为它们显然是模态"的,因此它将独立工作.但这似乎也不起作用.我现在被困住了,所以我正在使用我的最后一个资源(请求堆栈溢出).

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. But that didn't seem to work either. I am stuck now so I am using my last resource (asking Stack Overflow).

我想做的是让用户在文本字段(4,2,7)中输入一些数字,然后他们按JButton计算均值",它会找到数字的均值并将其显示在JOptionPane消息.当用户关闭JOptionPane对话框时,他们应该能够编辑数字并再次进行操作,但是"Calculate Mean"按钮仍然保持按下状态,并且用户除了关闭窗口之外什么也不能做.即使按Tab键也不会更改任何内容.有人知道为什么吗?我的代码如下:

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. 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. Even pressing the Tab key doesn't change anything. 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. 请勿从主机上卸下面板.
  2. 请勿在您的"calculateMean()"方法上使用已同步".该代码在事件调度线程(EDT)上执行,因此它将是单线程的.
  3. 不要在EDT上使用Thread.sleep().这样可以防止GUI重新绘制自身.

这篇关于显示JOptionPane后JButton保持按下状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆